From 36d3a60c403d2930d2e83d1c6758bd5b919648b8 Mon Sep 17 00:00:00 2001 From: aramis_acg Date: Sat, 7 May 2011 11:14:06 +0000 Subject: [PATCH] # fix obj crashes due to out-of-bounds indices in the input files. This fixes models/invalid/malformed.obj and models/invalid/malformed2.obj. git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@982 67173fc5-114c-0410-ac8e-9d2fd5bffc1f --- code/ObjFileImporter.cpp | 16 ++++++++++++---- code/ObjFileParser.cpp | 1 + 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/code/ObjFileImporter.cpp b/code/ObjFileImporter.cpp index d40550b36..a3610c7b9 100644 --- a/code/ObjFileImporter.cpp +++ b/code/ObjFileImporter.cpp @@ -335,14 +335,20 @@ void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel, for ( size_t vertexIndex = 0; vertexIndex < pSourceFace->m_pVertices->size(); vertexIndex++ ) { const unsigned int vertex = pSourceFace->m_pVertices->at( vertexIndex ); - ai_assert( vertex < pModel->m_Vertices.size() ); + if (vertex >= pModel->m_Vertices.size()) { + throw DeadlyImportError("OBJ: vertex index out of range"); + } + pMesh->mVertices[ newIndex ] = pModel->m_Vertices[ vertex ]; // Copy all normals if ( !pSourceFace->m_pNormals->empty() ) { const unsigned int normal = pSourceFace->m_pNormals->at( vertexIndex ); - ai_assert( normal < pModel->m_Normals.size() ); + if (normal >= pModel->m_Normals.size()) { + throw DeadlyImportError("OBJ: vertex normal index out of range"); + } + pMesh->mNormals[ newIndex ] = pModel->m_Normals[ normal ]; } @@ -401,8 +407,10 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc const unsigned int numMaterials = (unsigned int) pModel->m_MaterialLib.size(); pScene->mNumMaterials = 0; - if ( pModel->m_MaterialLib.empty() ) + if ( pModel->m_MaterialLib.empty() ) { + DefaultLogger::get()->debug("OBJ: no materials specified"); return; + } pScene->mMaterials = new aiMaterial*[ numMaterials ]; for ( unsigned int matIndex = 0; matIndex < numMaterials; matIndex++ ) @@ -435,7 +443,7 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc break; default: sm = aiShadingMode_Gouraud; - DefaultLogger::get()->error("OBJ/MTL: Unexpected illumination model (0-2 recognized)"); + DefaultLogger::get()->error("OBJ: unexpected illumination model (0-2 recognized)"); } mat->AddProperty( &sm, 1, AI_MATKEY_SHADING_MODEL); diff --git a/code/ObjFileParser.cpp b/code/ObjFileParser.cpp index 17dcf339b..8dfa9b9e7 100644 --- a/code/ObjFileParser.cpp +++ b/code/ObjFileParser.cpp @@ -388,6 +388,7 @@ void ObjFileParser::getMaterialDesc() { // Not found, use default material m_pModel->m_pCurrentMaterial = m_pModel->m_pDefaultMaterial; + DefaultLogger::get()->error("OBJ: failed to locate material " + strName + ", skipping"); } else {