From 01b2088dd3a4fea90d472061fc18ef202d9b1031 Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Thu, 5 Nov 2020 15:02:41 +0000 Subject: [PATCH 1/8] A missing bufferview was causing a crash. --- code/AssetLib/glTF2/glTF2Asset.inl | 33 ++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/code/AssetLib/glTF2/glTF2Asset.inl b/code/AssetLib/glTF2/glTF2Asset.inl index badf60f5c..396b1adf3 100644 --- a/code/AssetLib/glTF2/glTF2Asset.inl +++ b/code/AssetLib/glTF2/glTF2Asset.inl @@ -289,7 +289,8 @@ Ref LazyDict::Retrieve(unsigned int i) { // Unique ptr prevents memory leak in case of Read throws an exception auto inst = std::unique_ptr(new T()); - inst->id = std::string(mDictId) + "_" + to_string(i); + // Try to make this human readable so it can be used in error messages. + inst->id = std::string(mDictId) + "[" + to_string(i) + "]"; inst->oIndex = i; ReadMember(obj, "name", inst->name); inst->Read(obj, mAsset); @@ -637,15 +638,18 @@ inline void Accessor::Read(Value &obj, Asset &r) { const char *typestr; type = ReadMember(obj, "type", typestr) ? AttribType::FromString(typestr) : AttribType::SCALAR; - // Check length - unsigned long long byteLength = (unsigned long long)GetBytesPerComponent() * (unsigned long long)count; - if ((byteOffset + byteLength) > bufferView->byteLength || (bufferView->byteOffset + byteOffset + byteLength) > bufferView->buffer->byteLength) { - const uint8_t val_size = 64; + if (bufferView) + { + // Check length + unsigned long long byteLength = (unsigned long long)GetBytesPerComponent() * (unsigned long long)count; + if ((byteOffset + byteLength) > bufferView->byteLength || (bufferView->byteOffset + byteOffset + byteLength) > bufferView->buffer->byteLength) { + const uint8_t val_size = 64; - char val[val_size]; + char val[val_size]; - ai_snprintf(val, val_size, "%llu, %llu", (unsigned long long)byteOffset, (unsigned long long)byteLength); - throw DeadlyImportError("GLTF: Accessor with offset/length (", val, ") is out of range."); + ai_snprintf(val, val_size, "%llu, %llu", (unsigned long long)byteOffset, (unsigned long long)byteLength); + throw DeadlyImportError("GLTF: Accessor with offset/length (", val, ") is out of range."); + } } if (Value *sparseValue = FindObject(obj, "sparse")) { @@ -737,13 +741,24 @@ inline void CopyData(size_t count, } } } + +inline std::string getContextForErrorMessages(const std::string& id, const std::string& name) +{ + std::string context = id; + if (!name.empty()) + { + context += " (\"" + name + "\")"; + } + return context; +} + } // namespace template void Accessor::ExtractData(T *&outData) { uint8_t *data = GetPointer(); if (!data) { - throw DeadlyImportError("GLTF2: data is nullptr."); + throw DeadlyImportError("GLTF2: data is nullptr when extracting data from ", getContextForErrorMessages(id, name)); } const size_t elemSize = GetElementSize(); From 0af05e7a6053babfa8fabbd8ac09299c965cb0c7 Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Thu, 5 Nov 2020 15:10:52 +0000 Subject: [PATCH 2/8] Better message --- code/AssetLib/glTF2/glTF2Asset.inl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/AssetLib/glTF2/glTF2Asset.inl b/code/AssetLib/glTF2/glTF2Asset.inl index 396b1adf3..106c1fd51 100644 --- a/code/AssetLib/glTF2/glTF2Asset.inl +++ b/code/AssetLib/glTF2/glTF2Asset.inl @@ -758,7 +758,7 @@ template void Accessor::ExtractData(T *&outData) { uint8_t *data = GetPointer(); if (!data) { - throw DeadlyImportError("GLTF2: data is nullptr when extracting data from ", getContextForErrorMessages(id, name)); + throw DeadlyImportError("GLTF2: data is null when extracting data from ", getContextForErrorMessages(id, name)); } const size_t elemSize = GetElementSize(); From 34e3e6293ae0f4d36b93e472fba44a16a574f44d Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Fri, 6 Nov 2020 09:57:48 +0000 Subject: [PATCH 3/8] Style --- code/AssetLib/glTF2/glTF2Asset.inl | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/code/AssetLib/glTF2/glTF2Asset.inl b/code/AssetLib/glTF2/glTF2Asset.inl index 106c1fd51..cfa9cb142 100644 --- a/code/AssetLib/glTF2/glTF2Asset.inl +++ b/code/AssetLib/glTF2/glTF2Asset.inl @@ -638,8 +638,7 @@ inline void Accessor::Read(Value &obj, Asset &r) { const char *typestr; type = ReadMember(obj, "type", typestr) ? AttribType::FromString(typestr) : AttribType::SCALAR; - if (bufferView) - { + if (bufferView) { // Check length unsigned long long byteLength = (unsigned long long)GetBytesPerComponent() * (unsigned long long)count; if ((byteOffset + byteLength) > bufferView->byteLength || (bufferView->byteOffset + byteOffset + byteLength) > bufferView->buffer->byteLength) { @@ -742,11 +741,9 @@ inline void CopyData(size_t count, } } -inline std::string getContextForErrorMessages(const std::string& id, const std::string& name) -{ +inline std::string getContextForErrorMessages(const std::string& id, const std::string& name) { std::string context = id; - if (!name.empty()) - { + if (!name.empty()) { context += " (\"" + name + "\")"; } return context; From 0f246edb97d1f557bf3307f16be85a88377dab82 Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Fri, 6 Nov 2020 13:43:16 +0000 Subject: [PATCH 4/8] Prevent GetValue from corrupting memory --- code/AssetLib/glTF2/glTF2Asset.inl | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/code/AssetLib/glTF2/glTF2Asset.inl b/code/AssetLib/glTF2/glTF2Asset.inl index cfa9cb142..b4fbc3fad 100644 --- a/code/AssetLib/glTF2/glTF2Asset.inl +++ b/code/AssetLib/glTF2/glTF2Asset.inl @@ -835,9 +835,11 @@ template T Accessor::Indexer::GetValue(int i) { ai_assert(data); ai_assert(i * stride < accessor.bufferView->byteLength); + // Ensure that the memcpy doesn't overwrite the local. + const size_t sizeToCopy = std::min(elemSize, sizeof(T)); T value = T(); - memcpy(&value, data + i * stride, elemSize); - //value >>= 8 * (sizeof(T) - elemSize); + // Assume platform endianness matches GLTF binary data (which is little-endian). + memcpy(&value, data + i * stride, sizeToCopy); return value; } @@ -866,6 +868,14 @@ inline void Image::Read(Value &obj, Asset &r) { } } else if (Value *bufferViewVal = FindUInt(obj, "bufferView")) { this->bufferView = r.bufferViews.Retrieve(bufferViewVal->GetUint()); + if (Value *mtype = FindString(obj, "mimeType")) { + this->mimeType = mtype->GetString(); + } + if (!this->bufferView || this->mimeType.empty()) + { + throw DeadlyImportError("GLTF2: ", getContextForErrorMessages(id, name), " does not have a URI, so it must have a valid bufferView and mimetype"); + } + Ref buffer = this->bufferView->buffer; this->mDataLength = this->bufferView->byteLength; @@ -873,10 +883,10 @@ inline void Image::Read(Value &obj, Asset &r) { this->mData.reset(new uint8_t[this->mDataLength]); memcpy(this->mData.get(), buffer->GetPointer() + this->bufferView->byteOffset, this->mDataLength); - - if (Value *mtype = FindString(obj, "mimeType")) { - this->mimeType = mtype->GetString(); - } + } + else + { + throw DeadlyImportError("GLTF2: ", getContextForErrorMessages(id, name), " should have either a URI of a bufferView and mimetype" ); } } } From fa0951012ff31bfd3c4caeca586c4a440a365731 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Wed, 11 Nov 2020 20:32:55 +0100 Subject: [PATCH 5/8] closes https://github.com/assimp/assimp/issues/1044: fix envelope handling in lightintensity. --- code/AssetLib/LWS/LWSLoader.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/code/AssetLib/LWS/LWSLoader.cpp b/code/AssetLib/LWS/LWSLoader.cpp index 7d67c86d6..2a5cbeb8d 100644 --- a/code/AssetLib/LWS/LWSLoader.cpp +++ b/code/AssetLib/LWS/LWSLoader.cpp @@ -750,12 +750,17 @@ void LWSImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSy } // 'LightIntensity': set intensity of currently active light else if ((*it).tokens[0] == "LightIntensity" || (*it).tokens[0] == "LgtIntensity") { - if (nodes.empty() || nodes.back().type != LWS::NodeDesc::LIGHT) + if (nodes.empty() || nodes.back().type != LWS::NodeDesc::LIGHT) { ASSIMP_LOG_ERROR("LWS: Unexpected keyword: \'LightIntensity\'"); - - else - fast_atoreal_move(c, nodes.back().lightIntensity); - + } else { + const std::string env = "(envelope)"; + if (0 == strncmp(c, env.c_str(), env.size())) { + ASSIMP_LOG_ERROR("LWS: envelopes for LightIntensity not supported, set to 1.0"); + nodes.back().lightIntensity = (ai_real)1.0; + } else { + fast_atoreal_move(c, nodes.back().lightIntensity); + } + } } // 'LightType': set type of currently active light else if ((*it).tokens[0] == "LightType") { From 75818f26ebf7d2a641eef3262b26b80f48d1b137 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Wed, 11 Nov 2020 21:13:17 +0100 Subject: [PATCH 6/8] closes https://github.com/assimp/assimp/issues/3187 Do not use pthread for android --- test/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7f6d2ac86..df58846ec 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -232,6 +232,8 @@ SET_PROPERTY( TARGET assimp PROPERTY DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX} ) IF( WIN32 ) SET( platform_libs ) +ELSEIF(ANDROID) + SET( platform_libs ) ELSE() SET( platform_libs pthread ) ENDIF() From 09ca11d0638839e2fb4bd3777235e0a64067112c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Verdon?= Date: Mon, 16 Nov 2020 09:19:13 +0100 Subject: [PATCH 7/8] Fixing issue 3500, invalid outer cone angle readed from gltf2 file on machines which defines M_PI as a double value --- code/AssetLib/glTF2/glTF2Asset.inl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/AssetLib/glTF2/glTF2Asset.inl b/code/AssetLib/glTF2/glTF2Asset.inl index 2dfe2f41e..bebd19de6 100644 --- a/code/AssetLib/glTF2/glTF2Asset.inl +++ b/code/AssetLib/glTF2/glTF2Asset.inl @@ -1230,7 +1230,7 @@ inline void Light::Read(Value &obj, Asset & /*r*/) { Value *spot = FindObject(obj, "spot"); if (!spot) throw DeadlyImportError("GLTF: Light missing its spot parameters"); innerConeAngle = MemberOrDefault(*spot, "innerConeAngle", 0.0f); - outerConeAngle = MemberOrDefault(*spot, "outerConeAngle", M_PI / 4.0f); + outerConeAngle = MemberOrDefault(*spot, "outerConeAngle", static_cast(M_PI / 4.0f)); } } From 3221522f9549fa6fae2ee932af8c1a274ce2998e Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Mon, 16 Nov 2020 17:29:19 +0000 Subject: [PATCH 8/8] Prevent crash with malformed texture reference --- code/AssetLib/FBX/FBXConverter.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/code/AssetLib/FBX/FBXConverter.cpp b/code/AssetLib/FBX/FBXConverter.cpp index c7bc57d97..a7f9e6832 100644 --- a/code/AssetLib/FBX/FBXConverter.cpp +++ b/code/AssetLib/FBX/FBXConverter.cpp @@ -3481,10 +3481,11 @@ void FBXConverter::ConvertOrphanedEmbeddedTextures() { const char *obtype = key.begin(); const size_t length = static_cast(key.end() - key.begin()); if (strncmp(obtype, "Texture", length) == 0) { - const Texture *texture = static_cast(object->Get()); - if (texture->Media() && texture->Media()->ContentLength() > 0) { - realTexture = texture; - } + if (const Texture *texture = static_cast(object->Get())) { + if (texture->Media() && texture->Media()->ContentLength() > 0) { + realTexture = texture; + } + } } } catch (...) { // do nothing