Browse Source

# 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
aramis_acg 14 years ago
parent
commit
36d3a60c40
2 changed files with 13 additions and 4 deletions
  1. 12 4
      code/ObjFileImporter.cpp
  2. 1 0
      code/ObjFileParser.cpp

+ 12 - 4
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++ )
 		for ( size_t vertexIndex = 0; vertexIndex < pSourceFace->m_pVertices->size(); vertexIndex++ )
 		{
 		{
 			const unsigned int vertex = pSourceFace->m_pVertices->at( 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 ];
 			pMesh->mVertices[ newIndex ] = pModel->m_Vertices[ vertex ];
 			
 			
 			// Copy all normals 
 			// Copy all normals 
 			if ( !pSourceFace->m_pNormals->empty() )
 			if ( !pSourceFace->m_pNormals->empty() )
 			{
 			{
 				const unsigned int normal = pSourceFace->m_pNormals->at( vertexIndex );
 				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 ];
 				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();
 	const unsigned int numMaterials = (unsigned int) pModel->m_MaterialLib.size();
 	pScene->mNumMaterials = 0;
 	pScene->mNumMaterials = 0;
-	if ( pModel->m_MaterialLib.empty() )
+	if ( pModel->m_MaterialLib.empty() ) {
+		DefaultLogger::get()->debug("OBJ: no materials specified");
 		return;
 		return;
+	}
 	
 	
 	pScene->mMaterials = new aiMaterial*[ numMaterials ];
 	pScene->mMaterials = new aiMaterial*[ numMaterials ];
 	for ( unsigned int matIndex = 0; matIndex < numMaterials; matIndex++ )
 	for ( unsigned int matIndex = 0; matIndex < numMaterials; matIndex++ )
@@ -435,7 +443,7 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc
 			break;
 			break;
 		default:
 		default:
 			sm = aiShadingMode_Gouraud;
 			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<int>( &sm, 1, AI_MATKEY_SHADING_MODEL);
 		mat->AddProperty<int>( &sm, 1, AI_MATKEY_SHADING_MODEL);
 
 

+ 1 - 0
code/ObjFileParser.cpp

@@ -388,6 +388,7 @@ void ObjFileParser::getMaterialDesc()
 	{
 	{
 		// Not found, use default material
 		// Not found, use default material
 		m_pModel->m_pCurrentMaterial = m_pModel->m_pDefaultMaterial;
 		m_pModel->m_pCurrentMaterial = m_pModel->m_pDefaultMaterial;
+		DefaultLogger::get()->error("OBJ: failed to locate material " + strName + ", skipping");
 	}
 	}
 	else
 	else
 	{
 	{