Jelajahi Sumber

3DSLoader: exception-safety

Fixes memory leaks and/or crashes on malformed input.
Martin Jerabek 9 tahun lalu
induk
melakukan
a7cbb4386c
1 mengubah file dengan 8 tambahan dan 9 penghapusan
  1. 8 9
      code/3DSLoader.cpp

+ 8 - 9
code/3DSLoader.cpp

@@ -161,19 +161,21 @@ void Discreet3DSImporter::InternReadFile( const std::string& pFile,
     aiScene* pScene, IOSystem* pIOHandler)
     aiScene* pScene, IOSystem* pIOHandler)
 {
 {
     StreamReaderLE stream(pIOHandler->Open(pFile,"rb"));
     StreamReaderLE stream(pIOHandler->Open(pFile,"rb"));
-    this->stream = &stream;
 
 
     // We should have at least one chunk
     // We should have at least one chunk
     if (stream.GetRemainingSize() < 16) {
     if (stream.GetRemainingSize() < 16) {
         throw DeadlyImportError("3DS file is either empty or corrupt: " + pFile);
         throw DeadlyImportError("3DS file is either empty or corrupt: " + pFile);
     }
     }
+    this->stream = &stream;
 
 
     // Allocate our temporary 3DS representation
     // Allocate our temporary 3DS representation
-    mScene = new D3DS::Scene();
+    D3DS::Scene _scene;
+    mScene = &_scene;
 
 
     // Initialize members
     // Initialize members
+    D3DS::Node _rootNode("UNNAMED");
     mLastNodeIndex             = -1;
     mLastNodeIndex             = -1;
-    mCurrentNode               = new D3DS::Node("UNNAMED");
+    mCurrentNode               = &_rootNode;
     mRootNode                  = mCurrentNode;
     mRootNode                  = mCurrentNode;
     mRootNode->mHierarchyPos   = -1;
     mRootNode->mHierarchyPos   = -1;
     mRootNode->mHierarchyIndex = -1;
     mRootNode->mHierarchyIndex = -1;
@@ -193,7 +195,6 @@ void Discreet3DSImporter::InternReadFile( const std::string& pFile,
     // file.
     // file.
     for (auto &mesh : mScene->mMeshes) {
     for (auto &mesh : mScene->mMeshes) {
         if (mesh.mFaces.size() > 0 && mesh.mPositions.size() == 0)  {
         if (mesh.mFaces.size() > 0 && mesh.mPositions.size() == 0)  {
-            delete mScene;
             throw DeadlyImportError("3DS file contains faces but no vertices: " + pFile);
             throw DeadlyImportError("3DS file contains faces but no vertices: " + pFile);
         }
         }
         CheckIndices(mesh);
         CheckIndices(mesh);
@@ -201,7 +202,7 @@ void Discreet3DSImporter::InternReadFile( const std::string& pFile,
         ComputeNormalsWithSmoothingsGroups<D3DS::Face>(mesh);
         ComputeNormalsWithSmoothingsGroups<D3DS::Face>(mesh);
     }
     }
 
 
-    // Replace all occurrences of the default material with a
+    // Replace all occurences of the default material with a
     // valid material. Generate it if no material containing
     // valid material. Generate it if no material containing
     // DEFAULT in its name has been found in the file
     // DEFAULT in its name has been found in the file
     ReplaceDefaultMaterial();
     ReplaceDefaultMaterial();
@@ -218,10 +219,8 @@ void Discreet3DSImporter::InternReadFile( const std::string& pFile,
     // Now apply the master scaling factor to the scene
     // Now apply the master scaling factor to the scene
     ApplyMasterScale(pScene);
     ApplyMasterScale(pScene);
 
 
-    // Delete our internal scene representation and the root
-    // node, so the whole hierarchy will follow
-    delete mRootNode;
-    delete mScene;
+    // Our internal scene representation and the root
+    // node will be automatically deleted, so the whole hierarchy will follow
 
 
     AI_DEBUG_INVALIDATE_PTR(mRootNode);
     AI_DEBUG_INVALIDATE_PTR(mRootNode);
     AI_DEBUG_INVALIDATE_PTR(mScene);
     AI_DEBUG_INVALIDATE_PTR(mScene);