From aaae3e3a10902cd3c3eec18e18233ac6b6f77185 Mon Sep 17 00:00:00 2001 From: RichardTea <31507749+RichardTea@users.noreply.github.com> Date: Mon, 8 Nov 2021 15:05:20 +0000 Subject: [PATCH] size_t is 32bit on some platforms Also assert if size_t is smaller than uint32_t (probably not necessary) Note: 32bit builds will crash OOM if a really large model is loaded, as cannot allocate that much in total, let alone contiguously. --- code/AssetLib/glTF/glTFAsset.inl | 3 ++- code/AssetLib/glTF2/glTF2Asset.inl | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/code/AssetLib/glTF/glTFAsset.inl b/code/AssetLib/glTF/glTFAsset.inl index 896a9f16c..10be7c78e 100644 --- a/code/AssetLib/glTF/glTFAsset.inl +++ b/code/AssetLib/glTF/glTFAsset.inl @@ -1147,6 +1147,7 @@ inline void Asset::ReadBinaryHeader(IOStream &stream) { AI_SWAP4(header.length); AI_SWAP4(header.sceneLength); + static_assert(std::numeric_limits::max() <= std::numeric_limits::max(), "size_t must be at least 32bits"); mSceneLength = static_cast(header.sceneLength); // Can't be larger than 4GB (max. uint32_t) mBodyOffset = sizeof(header) + mSceneLength; @@ -1184,7 +1185,7 @@ inline void Asset::Load(const std::string &pFile, bool isBinary) { } // Binary format only supports up to 4GB of JSON so limit it there to avoid extreme memory allocation - if (mSceneLength > std::numeric_limits::max()) { + if (mSceneLength >= std::numeric_limits::max()) { throw DeadlyImportError("GLTF: JSON size greater than 4GB"); } diff --git a/code/AssetLib/glTF2/glTF2Asset.inl b/code/AssetLib/glTF2/glTF2Asset.inl index 479749523..3fc238208 100644 --- a/code/AssetLib/glTF2/glTF2Asset.inl +++ b/code/AssetLib/glTF2/glTF2Asset.inl @@ -1777,7 +1777,7 @@ inline void Asset::ReadBinaryHeader(IOStream &stream, std::vector &sceneDa } // read the scene data, ensure null termination - + static_assert(std::numeric_limits::max() <= std::numeric_limits::max(), "size_t must be at least 32bits"); mSceneLength = chunk.chunkLength; // Can't be larger than 4GB (max. uint32_t) sceneData.resize(mSceneLength + 1); sceneData[mSceneLength] = '\0'; @@ -1836,7 +1836,7 @@ inline void Asset::Load(const std::string &pFile, bool isBinary) { mBodyLength = 0; // Binary format only supports up to 4GB of JSON, use that as a maximum - if (mSceneLength > std::numeric_limits::max()) { + if (mSceneLength >= std::numeric_limits::max()) { throw DeadlyImportError("GLTF: JSON size greater than 4GB"); }