From 2be6bac4b022232058008658726bb2a240f3fc8a Mon Sep 17 00:00:00 2001 From: kovacsv Date: Tue, 7 Dec 2021 20:42:43 +0100 Subject: [PATCH] Bug: Export crashes when any of the meshes contains texture coordinate names #4243 --- code/Common/SceneCombiner.cpp | 20 ++++++++++++++++++++ include/assimp/SceneCombiner.h | 1 + test/unit/Common/uiScene.cpp | 17 +++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/code/Common/SceneCombiner.cpp b/code/Common/SceneCombiner.cpp index 8f10d6308..7dbbf8860 100644 --- a/code/Common/SceneCombiner.cpp +++ b/code/Common/SceneCombiner.cpp @@ -1101,6 +1101,14 @@ void SceneCombiner::Copy(aiMesh **_dest, const aiMesh *src) { // make a deep copy of all blend shapes CopyPtrArray(dest->mAnimMeshes, dest->mAnimMeshes, dest->mNumAnimMeshes); + + // make a deep copy of all texture coordinate names + if (src->mTextureCoordsNames != nullptr) { + dest->mTextureCoordsNames = new aiString *[AI_MAX_NUMBER_OF_TEXTURECOORDS] {}; + for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) { + Copy(&dest->mTextureCoordsNames[i], src->mTextureCoordsNames[i]); + } + } } // ------------------------------------------------------------------------------------------------ @@ -1348,6 +1356,18 @@ void SceneCombiner::Copy(aiMetadata **_dest, const aiMetadata *src) { } } +// ------------------------------------------------------------------------------------------------ +void SceneCombiner::Copy(aiString **_dest, const aiString *src) { + if (nullptr == _dest || nullptr == src) { + return; + } + + aiString *dest = *_dest = new aiString(); + + // get a flat copy + *dest = *src; +} + #if (__GNUC__ >= 8 && __GNUC_MINOR__ >= 0) #pragma GCC diagnostic pop #endif diff --git a/include/assimp/SceneCombiner.h b/include/assimp/SceneCombiner.h index 809ac30e4..874a885a5 100644 --- a/include/assimp/SceneCombiner.h +++ b/include/assimp/SceneCombiner.h @@ -361,6 +361,7 @@ public: static void Copy(aiNodeAnim **dest, const aiNodeAnim *src); static void Copy(aiMeshMorphAnim **dest, const aiMeshMorphAnim *src); static void Copy(aiMetadata **dest, const aiMetadata *src); + static void Copy(aiString **dest, const aiString *src); // recursive, of course static void Copy(aiNode **dest, const aiNode *src); diff --git a/test/unit/Common/uiScene.cpp b/test/unit/Common/uiScene.cpp index 1c531b042..af0b6ca01 100644 --- a/test/unit/Common/uiScene.cpp +++ b/test/unit/Common/uiScene.cpp @@ -42,6 +42,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "UnitTestPCH.h" #include +#include using namespace Assimp; @@ -88,5 +89,21 @@ TEST_F(utScene, getShortFilenameTest) { EXPECT_NE(nullptr, name2); } +TEST_F(utScene, deepCopyTest) { + scene->mRootNode = new aiNode(); + + scene->mNumMeshes = 1; + scene->mMeshes = new aiMesh *[scene->mNumMeshes] (); + scene->mMeshes[0] = new aiMesh (); + + scene->mMeshes[0]->SetTextureCoordsName (0, aiString ("test")); + + { + aiScene* copied = nullptr; + SceneCombiner::CopyScene(&copied,scene); + delete copied; + } +} + TEST_F(utScene, getEmbeddedTextureTest) { }