RemoveRedundantMaterials: avoid crash when all materials are redundant

pull/5439/head
Stephen Gold 2024-01-25 15:05:17 -08:00
parent ac29847d56
commit cbe3bbd723
1 changed files with 30 additions and 28 deletions

View File

@ -3,7 +3,7 @@
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
Copyright (c) 2006-2022, assimp team
Copyright (c) 2006-2024, assimp team
All rights reserved.
@ -153,11 +153,12 @@ void RemoveRedundantMatsProcess::Execute( aiScene* pScene) {
}
// If the new material count differs from the original,
// we need to rebuild the material list and remap mesh material indexes.
if(iNewNum < 1)
throw DeadlyImportError("No materials remaining");
if (iNewNum != pScene->mNumMaterials) {
ai_assert(iNewNum > 0);
aiMaterial** ppcMaterials = new aiMaterial*[iNewNum];
aiMaterial** ppcMaterials;
if (iNewNum == 0) {
ppcMaterials = nullptr;
} else {
ppcMaterials = new aiMaterial*[iNewNum];
::memset(ppcMaterials,0,sizeof(void*)*iNewNum);
for (unsigned int p = 0; p < pScene->mNumMaterials;++p)
{
@ -184,6 +185,7 @@ void RemoveRedundantMatsProcess::Execute( aiScene* pScene) {
ai_assert(nullptr != mesh);
mesh->mMaterialIndex = aiMappingTable[mesh->mMaterialIndex];
}
}
// delete the old material list
delete[] pScene->mMaterials;
pScene->mMaterials = ppcMaterials;