From 6a9df80f688ffd7d6af0562e71299aadc2b50d5f Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Thu, 28 Dec 2017 14:52:41 +0100 Subject: [PATCH 1/5] closes https://github.com/assimp/assimp/issues/1655: add typedef for android NDK. --- contrib/zlib/zlib.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/contrib/zlib/zlib.h b/contrib/zlib/zlib.h index 577d81e3b..2d6bb2976 100644 --- a/contrib/zlib/zlib.h +++ b/contrib/zlib/zlib.h @@ -77,6 +77,9 @@ extern "C" { the consistency of the compressed data, so the library should never crash even in the case of corrupted input. */ +#ifdef __ANDROID__ +using zcrc_t = unsigned_long; +#endif typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size)); typedef void (*free_func) OF((voidpf opaque, voidpf address)); From 20ef807982a8ebe970dcd135d7d0631010421bf1 Mon Sep 17 00:00:00 2001 From: Alexandre Avenel Date: Fri, 29 Dec 2017 17:56:30 +0100 Subject: [PATCH 2/5] Remove assertion in ObjFileImporter This assertion is already handled by an exception, and could lead to a crash when parsing an ill-formed OBJ file. --- code/ObjFileImporter.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/code/ObjFileImporter.cpp b/code/ObjFileImporter.cpp index 10c463c36..11f4b9487 100644 --- a/code/ObjFileImporter.cpp +++ b/code/ObjFileImporter.cpp @@ -474,7 +474,6 @@ void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel, if ( !pModel->m_TextureCoord.empty() && vertexIndex < pSourceFace->m_texturCoords.size()) { const unsigned int tex = pSourceFace->m_texturCoords.at( vertexIndex ); - ai_assert( tex < pModel->m_TextureCoord.size() ); if ( tex >= pModel->m_TextureCoord.size() ) throw DeadlyImportError("OBJ: texture coordinate index out of range"); From 7de7c548fb5e49a2881a3899570e137c19796ee3 Mon Sep 17 00:00:00 2001 From: Alexandre Avenel Date: Fri, 29 Dec 2017 18:06:20 +0100 Subject: [PATCH 3/5] Add unit test to OBj importer for homogeneous coord This test triggers an assertion when we divide by zero in homogeneous coordinates. --- test/unit/utObjImportExport.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/unit/utObjImportExport.cpp b/test/unit/utObjImportExport.cpp index dd8adcd80..a6c91fed6 100644 --- a/test/unit/utObjImportExport.cpp +++ b/test/unit/utObjImportExport.cpp @@ -330,6 +330,18 @@ TEST_F(utObjImportExport, homogeneous_coordinates_Test) { EXPECT_EQ(vertice.z, 0.8f); } +TEST_F(utObjImportExport, homogeneous_coordinates_divide_by_zero_Test) { + static const std::string ObjModel = + "v -0.500000 0.000000 0.400000 0.\n" + "v -0.500000 0.000000 -0.800000 1.00000\n" + "v 0.500000 1.000000 -0.800000 0.5000\n" + "f 1 2 3\nB"; + + Assimp::Importer myimporter; + const aiScene *scene = myimporter.ReadFileFromMemory(ObjModel.c_str(), ObjModel.size(), aiProcess_ValidateDataStructure); + EXPECT_EQ(nullptr, scene); +} + TEST_F(utObjImportExport, 0based_array_Test) { static const std::string ObjModel = "v -0.500000 0.000000 0.400000\n" From 606fd6b1a1e4b1726981ca9f0b4a87458e474f3f Mon Sep 17 00:00:00 2001 From: Alexandre Avenel Date: Fri, 29 Dec 2017 18:07:41 +0100 Subject: [PATCH 4/5] Replace assertion by an exception in Obj parsing This crash could happen when the user try to parse an ill-formed obj file. --- code/ObjFileParser.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/code/ObjFileParser.cpp b/code/ObjFileParser.cpp index d89d52977..a8d92497d 100644 --- a/code/ObjFileParser.cpp +++ b/code/ObjFileParser.cpp @@ -353,7 +353,8 @@ void ObjFileParser::getHomogeneousVector3( std::vector &point3d_arra copyNextWord( m_buffer, Buffersize ); w = ( ai_real ) fast_atof( m_buffer ); - ai_assert( w != 0 ); + if (w == 0) + throw DeadlyImportError("OBJ: Invalid component in homogeneous vector (Division by zero)"); point3d_array.push_back( aiVector3D( x/w, y/w, z/w ) ); m_DataIt = skipLine( m_DataIt, m_DataItEnd, m_uiLine ); From ced6e8ce43f0efd5af30032e4dfbccdd973472df Mon Sep 17 00:00:00 2001 From: Alexandre Avenel Date: Sat, 30 Dec 2017 13:22:30 +0100 Subject: [PATCH 5/5] Use std::unique_ptr in ObjFileParser --- code/ObjFileParser.cpp | 14 +++++--------- code/ObjFileParser.h | 3 ++- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/code/ObjFileParser.cpp b/code/ObjFileParser.cpp index a8d92497d..2b9873bc1 100644 --- a/code/ObjFileParser.cpp +++ b/code/ObjFileParser.cpp @@ -60,7 +60,7 @@ const std::string ObjFileParser::DEFAULT_MATERIAL = AI_DEFAULT_MATERIAL_NAME; ObjFileParser::ObjFileParser() : m_DataIt() , m_DataItEnd() -, m_pModel( NULL ) +, m_pModel( nullptr ) , m_uiLine( 0 ) , m_pIO( nullptr ) , m_progress( nullptr ) @@ -73,7 +73,7 @@ ObjFileParser::ObjFileParser( IOStreamBuffer &streamBuffer, const std::str const std::string &originalObjFileName) : m_DataIt(), m_DataItEnd(), - m_pModel(NULL), + m_pModel(nullptr), m_uiLine(0), m_pIO( io ), m_progress(progress), @@ -82,7 +82,7 @@ ObjFileParser::ObjFileParser( IOStreamBuffer &streamBuffer, const std::str std::fill_n(m_buffer,Buffersize,0); // Create the model instance to store all the data - m_pModel = new ObjFile::Model(); + m_pModel.reset(new ObjFile::Model()); m_pModel->m_ModelName = modelName; // create default material and store it @@ -96,8 +96,6 @@ ObjFileParser::ObjFileParser( IOStreamBuffer &streamBuffer, const std::str } ObjFileParser::~ObjFileParser() { - delete m_pModel; - m_pModel = NULL; } void ObjFileParser::setBuffer( std::vector &buffer ) { @@ -106,7 +104,7 @@ void ObjFileParser::setBuffer( std::vector &buffer ) { } ObjFile::Model *ObjFileParser::GetModel() const { - return m_pModel; + return m_pModel.get(); } void ObjFileParser::parseFile( IOStreamBuffer &streamBuffer ) { @@ -479,8 +477,6 @@ void ObjFileParser::getFace( aiPrimitiveType type ) { } else { //On error, std::atoi will return 0 which is not a valid value delete face; - delete m_pModel; - m_pModel = nullptr; throw DeadlyImportError("OBJ: Invalid face indice"); } @@ -642,7 +638,7 @@ void ObjFileParser::getMaterialLib() { m_pIO->Close( pFile ); // Importing the material library - ObjFileMtlImporter mtlImporter( buffer, strMatName, m_pModel ); + ObjFileMtlImporter mtlImporter( buffer, strMatName, m_pModel.get() ); } // ------------------------------------------------------------------- diff --git a/code/ObjFileParser.h b/code/ObjFileParser.h index fa5b3ca31..15332a1df 100644 --- a/code/ObjFileParser.h +++ b/code/ObjFileParser.h @@ -44,6 +44,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include #include #include @@ -145,7 +146,7 @@ private: //! Iterator to end position of buffer DataArrayIt m_DataItEnd; //! Pointer to model instance - ObjFile::Model *m_pModel; + std::unique_ptr m_pModel; //! Current line (for debugging) unsigned int m_uiLine; //! Helper buffer