From 0590a39159beabd8263e891390997f752478bd25 Mon Sep 17 00:00:00 2001 From: kovacsv Date: Tue, 24 Aug 2021 07:26:20 +0200 Subject: [PATCH] Fix M3D import crash and memory leak. The same default material pointer was assigned to all the materials in the scene, so poor destructor tried to free the same pointer multiple times. --- code/AssetLib/M3D/M3DImporter.cpp | 14 +++++----- test/unit/utM3DImportExport.cpp | 43 ++++++++++++++----------------- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/code/AssetLib/M3D/M3DImporter.cpp b/code/AssetLib/M3D/M3DImporter.cpp index 4b2f9bd77..efa1d5475 100644 --- a/code/AssetLib/M3D/M3DImporter.cpp +++ b/code/AssetLib/M3D/M3DImporter.cpp @@ -233,12 +233,12 @@ void M3DImporter::importMaterials(const M3DWrapper &m3d) { ASSIMP_LOG_DEBUG("M3D: importMaterials ", mScene->mNumMaterials); // add a default material as first - aiMaterial *mat = new aiMaterial; - mat->AddProperty(&name, AI_MATKEY_NAME); + aiMaterial *defaultMat = new aiMaterial; + defaultMat->AddProperty(&name, AI_MATKEY_NAME); c.a = 1.0f; c.b = c.g = c.r = 0.6f; - mat->AddProperty(&c, 1, AI_MATKEY_COLOR_DIFFUSE); - mScene->mMaterials[0] = mat; + defaultMat->AddProperty(&c, 1, AI_MATKEY_COLOR_DIFFUSE); + mScene->mMaterials[0] = defaultMat; if (!m3d->nummaterial || !m3d->material) { return; @@ -300,12 +300,12 @@ void M3DImporter::importMaterials(const M3DWrapper &m3d) { m->prop[j].value.textureid < m3d->numtexture && m3d->texture[m->prop[j].value.textureid].name) { name.Set(std::string(std::string(m3d->texture[m->prop[j].value.textureid].name) + ".png")); - mat->AddProperty(&name, aiTxProps[k].pKey, aiTxProps[k].type, aiTxProps[k].index); + newMat->AddProperty(&name, aiTxProps[k].pKey, aiTxProps[k].type, aiTxProps[k].index); n = 0; - mat->AddProperty(&n, 1, _AI_MATKEY_UVWSRC_BASE, aiProps[k].type, aiProps[k].index); + newMat->AddProperty(&n, 1, _AI_MATKEY_UVWSRC_BASE, aiProps[k].type, aiProps[k].index); } } - mScene->mMaterials[i + 1] = mat; + mScene->mMaterials[i + 1] = newMat; } } diff --git a/test/unit/utM3DImportExport.cpp b/test/unit/utM3DImportExport.cpp index bd9fca168..24359d319 100644 --- a/test/unit/utM3DImportExport.cpp +++ b/test/unit/utM3DImportExport.cpp @@ -50,35 +50,32 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. using namespace Assimp; -class utM3DImportExport : public AbstractImportExportBase { -public: - bool importerTest() override { - Assimp::Importer importer; - const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/M3D/cube_normals.m3d", aiProcess_ValidateDataStructure); +TEST(utM3DImportExport, import_cube_normals) { + Assimp::Importer importer; + const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/M3D/cube_normals.m3d", aiProcess_ValidateDataStructure); #ifndef ASSIMP_BUILD_NO_M3D_IMPORTER - return nullptr != scene; + ASSERT_NE(nullptr, scene); #else - return nullptr == scene; + ASSERT_EQ(nullptr, scene); #endif // ASSIMP_BUILD_NO_M3D_IMPORTER - } +} -#ifndef ASSIMP_BUILD_NO_EXPORT - bool exporterTest() override { - Assimp::Importer importer; - const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/M3D/cube_normals.m3d", aiProcess_ValidateDataStructure); - Exporter exporter; - aiReturn ret = exporter.Export(scene, "m3d", ASSIMP_TEST_MODELS_DIR "/M3D/cube_normals_out.m3d"); - return ret == AI_SUCCESS; - } -#endif -}; - -TEST_F(utM3DImportExport, importM3DFromFileTest) { - EXPECT_TRUE(importerTest()); +TEST(utM3DImportExport, import_cube_usemtl) { + Assimp::Importer importer; + const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/M3D/cube_usemtl.m3d", aiProcess_ValidateDataStructure); +#ifndef ASSIMP_BUILD_NO_M3D_IMPORTER + ASSERT_NE(nullptr, scene); +#else + ASSERT_EQ(nullptr, scene); +#endif // ASSIMP_BUILD_NO_M3D_IMPORTER } #ifndef ASSIMP_BUILD_NO_EXPORT -TEST_F(utM3DImportExport, exportM3DFromFileTest) { - EXPECT_TRUE(exporterTest()); +TEST(utM3DImportExport, export_cube_normals) { + Assimp::Importer importer; + const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/M3D/cube_normals.m3d", aiProcess_ValidateDataStructure); + Exporter exporter; + aiReturn ret = exporter.Export(scene, "m3d", ASSIMP_TEST_MODELS_DIR "/M3D/cube_normals_out.m3d"); + ASSERT_EQ(AI_SUCCESS, ret); } #endif // ASSIMP_BUILD_NO_EXPORT