From a66e644bf3f975fe8b962b8852700740fa7724ab Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Mon, 29 Aug 2016 15:24:24 +0200 Subject: [PATCH] Fix coverity finding: fix possible use after free.. --- code/XGLLoader.cpp | 83 ++++++++++++++++++++++++---------------------- code/XGLLoader.h | 9 ++--- 2 files changed, 46 insertions(+), 46 deletions(-) diff --git a/code/XGLLoader.cpp b/code/XGLLoader.cpp index fd079ef14..2df72849b 100644 --- a/code/XGLLoader.cpp +++ b/code/XGLLoader.cpp @@ -102,12 +102,16 @@ static const aiImporterDesc desc = { // ------------------------------------------------------------------------------------------------ // Constructor to be privately used by Importer XGLImporter::XGLImporter() -{} +: m_reader( nullptr ) +, m_scene( nullptr ) { + // empty +} // ------------------------------------------------------------------------------------------------ // Destructor, private as well -XGLImporter::~XGLImporter() -{} +XGLImporter::~XGLImporter() { + // empty +} // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. @@ -149,7 +153,7 @@ void XGLImporter::InternReadFile( const std::string& pFile, free_it free_it_really(dest); #endif - scene = pScene; + m_scene = pScene; std::shared_ptr stream( pIOHandler->Open( pFile, "rb")); // check whether we can read from the file @@ -211,14 +215,13 @@ void XGLImporter::InternReadFile( const std::string& pFile, // construct the irrXML parser CIrrXML_IOStreamReader st(stream.get()); - std::unique_ptr read( createIrrXMLReader((IFileReadCallBack*) &st) ); - reader = read.get(); + m_reader.reset( createIrrXMLReader( ( IFileReadCallBack* ) &st ) ); // parse the XML file TempScope scope; while (ReadElement()) { - if (!ASSIMP_stricmp(reader->getNodeName(),"world")) { + if (!ASSIMP_stricmp(m_reader->getNodeName(),"world")) { ReadWorld(scope); } } @@ -231,21 +234,21 @@ void XGLImporter::InternReadFile( const std::string& pFile, } // copy meshes - scene->mNumMeshes = static_cast(meshes.size()); - scene->mMeshes = new aiMesh*[scene->mNumMeshes](); - std::copy(meshes.begin(),meshes.end(),scene->mMeshes); + m_scene->mNumMeshes = static_cast(meshes.size()); + m_scene->mMeshes = new aiMesh*[m_scene->mNumMeshes](); + std::copy(meshes.begin(),meshes.end(),m_scene->mMeshes); // copy materials - scene->mNumMaterials = static_cast(materials.size()); - scene->mMaterials = new aiMaterial*[scene->mNumMaterials](); - std::copy(materials.begin(),materials.end(),scene->mMaterials); + m_scene->mNumMaterials = static_cast(materials.size()); + m_scene->mMaterials = new aiMaterial*[m_scene->mNumMaterials](); + std::copy(materials.begin(),materials.end(),m_scene->mMaterials); if (scope.light) { - scene->mNumLights = 1; - scene->mLights = new aiLight*[1]; - scene->mLights[0] = scope.light; + m_scene->mNumLights = 1; + m_scene->mLights = new aiLight*[1]; + m_scene->mLights[0] = scope.light; - scope.light->mName = scene->mRootNode->mName; + scope.light->mName = m_scene->mRootNode->mName; } scope.dismiss(); @@ -254,8 +257,8 @@ void XGLImporter::InternReadFile( const std::string& pFile, // ------------------------------------------------------------------------------------------------ bool XGLImporter::ReadElement() { - while(reader->read()) { - if (reader->getNodeType() == EXN_ELEMENT) { + while(m_reader->read()) { + if (m_reader->getNodeType() == EXN_ELEMENT) { return true; } } @@ -265,11 +268,11 @@ bool XGLImporter::ReadElement() // ------------------------------------------------------------------------------------------------ bool XGLImporter::ReadElementUpToClosing(const char* closetag) { - while(reader->read()) { - if (reader->getNodeType() == EXN_ELEMENT) { + while(m_reader->read()) { + if (m_reader->getNodeType() == EXN_ELEMENT) { return true; } - else if (reader->getNodeType() == EXN_ELEMENT_END && !ASSIMP_stricmp(reader->getNodeName(),closetag)) { + else if (m_reader->getNodeType() == EXN_ELEMENT_END && !ASSIMP_stricmp(m_reader->getNodeName(),closetag)) { return false; } } @@ -280,11 +283,11 @@ bool XGLImporter::ReadElementUpToClosing(const char* closetag) // ------------------------------------------------------------------------------------------------ bool XGLImporter::SkipToText() { - while(reader->read()) { - if (reader->getNodeType() == EXN_TEXT) { + while(m_reader->read()) { + if (m_reader->getNodeType() == EXN_TEXT) { return true; } - else if (reader->getNodeType() == EXN_ELEMENT || reader->getNodeType() == EXN_ELEMENT_END) { + else if (m_reader->getNodeType() == EXN_ELEMENT || m_reader->getNodeType() == EXN_ELEMENT_END) { ThrowException("expected text contents but found another element (or element end)"); } } @@ -294,7 +297,7 @@ bool XGLImporter::SkipToText() // ------------------------------------------------------------------------------------------------ std::string XGLImporter::GetElementName() { - const char* s = reader->getNodeName(); + const char* s = m_reader->getNodeName(); size_t len = strlen(s); std::string ret; @@ -328,7 +331,7 @@ void XGLImporter::ReadWorld(TempScope& scope) nd->mName.Set("WORLD"); } - scene->mRootNode = nd; + m_scene->mRootNode = nd; } // ------------------------------------------------------------------------------------------------ @@ -586,29 +589,29 @@ bool XGLImporter::ReadMesh(TempScope& scope) ReadMaterial(scope); } else if (s == "p") { - if (!reader->getAttributeValue("ID")) { + if (!m_reader->getAttributeValue("ID")) { LogWarn("no ID attribute on

, ignoring"); } else { - int id = reader->getAttributeValueAsInt("ID"); + int id = m_reader->getAttributeValueAsInt("ID"); t.points[id] = ReadVec3(); } } else if (s == "n") { - if (!reader->getAttributeValue("ID")) { + if (!m_reader->getAttributeValue("ID")) { LogWarn("no ID attribute on , ignoring"); } else { - int id = reader->getAttributeValueAsInt("ID"); + int id = m_reader->getAttributeValueAsInt("ID"); t.normals[id] = ReadVec3(); } } else if (s == "tc") { - if (!reader->getAttributeValue("ID")) { + if (!m_reader->getAttributeValue("ID")) { LogWarn("no ID attribute on , ignoring"); } else { - int id = reader->getAttributeValueAsInt("ID"); + int id = m_reader->getAttributeValueAsInt("ID"); t.uvs[id] = ReadVec2(); } } @@ -828,10 +831,10 @@ void XGLImporter::ReadFaceVertex(const TempMesh& t, TempFace& out) // ------------------------------------------------------------------------------------------------ unsigned int XGLImporter::ReadIDAttr() { - for(int i = 0, e = reader->getAttributeCount(); i < e; ++i) { + for(int i = 0, e = m_reader->getAttributeCount(); i < e; ++i) { - if(!ASSIMP_stricmp(reader->getAttributeName(i),"id")) { - return reader->getAttributeValueAsInt(i); + if(!ASSIMP_stricmp(m_reader->getAttributeName(i),"id")) { + return m_reader->getAttributeValueAsInt(i); } } return ~0u; @@ -844,7 +847,7 @@ float XGLImporter::ReadFloat() LogError("unexpected EOF reading float element contents"); return 0.f; } - const char* s = reader->getNodeData(), *se; + const char* s = m_reader->getNodeData(), *se; if(!SkipSpaces(&s)) { LogError("unexpected EOL, failed to parse float"); @@ -869,7 +872,7 @@ unsigned int XGLImporter::ReadIndexFromText() LogError("unexpected EOF reading index element contents"); return ~0u; } - const char* s = reader->getNodeData(), *se; + const char* s = m_reader->getNodeData(), *se; if(!SkipSpaces(&s)) { LogError("unexpected EOL, failed to parse index element"); return ~0u; @@ -894,7 +897,7 @@ aiVector2D XGLImporter::ReadVec2() LogError("unexpected EOF reading vec2 contents"); return vec; } - const char* s = reader->getNodeData(); + const char* s = m_reader->getNodeData(); for(int i = 0; i < 2; ++i) { if(!SkipSpaces(&s)) { @@ -923,7 +926,7 @@ aiVector3D XGLImporter::ReadVec3() LogError("unexpected EOF reading vec3 contents"); return vec; } - const char* s = reader->getNodeData(); + const char* s = m_reader->getNodeData(); for(int i = 0; i < 3; ++i) { if(!SkipSpaces(&s)) { diff --git a/code/XGLLoader.h b/code/XGLLoader.h index 757f9d564..c8a68ba31 100644 --- a/code/XGLLoader.h +++ b/code/XGLLoader.h @@ -51,6 +51,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include struct aiNode; @@ -203,12 +204,8 @@ private: unsigned int ResolveMaterialRef(TempScope& scope); private: - - -private: - - irr::io::IrrXMLReader* reader; - aiScene* scene; + std::shared_ptr m_reader; + aiScene* m_scene; }; } // end of namespace Assimp