From bb62249f0c76124a7fd44398b90a76009182476c Mon Sep 17 00:00:00 2001 From: Yingying Wang Date: Wed, 4 Mar 2020 14:52:26 -0800 Subject: [PATCH 1/2] fix gltf2 exporter memory crash --- code/glTF2/glTF2Asset.inl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/glTF2/glTF2Asset.inl b/code/glTF2/glTF2Asset.inl index 35ecfa62d..2535207c4 100644 --- a/code/glTF2/glTF2Asset.inl +++ b/code/glTF2/glTF2Asset.inl @@ -535,7 +535,7 @@ inline void Buffer::Grow(size_t amount) // Shift operation is standard way to divide integer by 2, it doesn't cast it to float back and forth, also works for odd numbers, // originally it would look like: static_cast(capacity * 1.5f) - capacity = std::max(capacity + (capacity >> 1), byteLength + amount); + capacity = byteLength + amount; //wangyi fix crash std::max(capacity + (capacity >> 1), byteLength + amount); uint8_t* b = new uint8_t[capacity]; if (mData) memcpy(b, mData.get(), byteLength); From c21a1ffffaf7a9fec6955ee436f8d69ba7d68c7a Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Wed, 1 Apr 2020 11:16:39 +0200 Subject: [PATCH 2/2] Remove comments to increase readability --- code/glTF2/glTF2Asset.inl | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/code/glTF2/glTF2Asset.inl b/code/glTF2/glTF2Asset.inl index 781267e7e..a41e62e5c 100644 --- a/code/glTF2/glTF2Asset.inl +++ b/code/glTF2/glTF2Asset.inl @@ -508,18 +508,23 @@ inline size_t Buffer::AppendData(uint8_t *data, size_t length) { } inline void Buffer::Grow(size_t amount) { - if (amount <= 0) return; + if (amount <= 0) { + return; + } + + // Capacity is big enough if (capacity >= byteLength + amount) { byteLength += amount; return; } - // Shift operation is standard way to divide integer by 2, it doesn't cast it to float back and forth, also works for odd numbers, - // originally it would look like: static_cast(capacity * 1.5f) - capacity = byteLength + amount; //wangyi fix crash std::max(capacity + (capacity >> 1), byteLength + amount); + // Just allocate data which we need + capacity = byteLength + amount; uint8_t *b = new uint8_t[capacity]; - if (mData) memcpy(b, mData.get(), byteLength); + if (nullptr != mData) { + memcpy(b, mData.get(), byteLength); + } mData.reset(b, std::default_delete()); byteLength += amount; }