Browse Source

Merge pull request #1656 from turol/misc

Miscellaneous memory management fixes
turol 7 năm trước cách đây
mục cha
commit
bc541e56af

+ 10 - 12
code/MDLMaterialLoader.cpp

@@ -56,6 +56,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <assimp/Defines.h>
 #include <assimp/Defines.h>
 #include "qnan.h"
 #include "qnan.h"
 
 
+#include <memory>
+
 
 
 using namespace Assimp;
 using namespace Assimp;
 static aiTexel* const bad_texel = reinterpret_cast<aiTexel*>(SIZE_MAX);
 static aiTexel* const bad_texel = reinterpret_cast<aiTexel*>(SIZE_MAX);
@@ -489,7 +491,7 @@ void MDLImporter::ParseSkinLump_3DGS_MDL7(
     unsigned int iWidth,
     unsigned int iWidth,
     unsigned int iHeight)
     unsigned int iHeight)
 {
 {
-    aiTexture* pcNew = nullptr;
+    std::unique_ptr<aiTexture> pcNew;
 
 
     // get the type of the skin
     // get the type of the skin
     unsigned int iMasked = (unsigned int)(iType & 0xF);
     unsigned int iMasked = (unsigned int)(iType & 0xF);
@@ -509,7 +511,7 @@ void MDLImporter::ParseSkinLump_3DGS_MDL7(
                 "but texture height is not equal to 1, which is not supported by MED");
                 "but texture height is not equal to 1, which is not supported by MED");
         }
         }
 
 
-        pcNew = new aiTexture();
+        pcNew.reset(new aiTexture());
         pcNew->mHeight = 0;
         pcNew->mHeight = 0;
         pcNew->mWidth = iWidth;
         pcNew->mWidth = iWidth;
 
 
@@ -546,7 +548,7 @@ void MDLImporter::ParseSkinLump_3DGS_MDL7(
     }
     }
     else if (iMasked || !iType || (iType && iWidth && iHeight))
     else if (iMasked || !iType || (iType && iWidth && iHeight))
     {
     {
-        pcNew = new aiTexture();
+        pcNew.reset(new aiTexture());
         if (!iHeight || !iWidth)
         if (!iHeight || !iWidth)
         {
         {
             DefaultLogger::get()->warn("Found embedded texture, but its width "
             DefaultLogger::get()->warn("Found embedded texture, but its width "
@@ -577,7 +579,7 @@ void MDLImporter::ParseSkinLump_3DGS_MDL7(
             pcNew->mHeight = iHeight;
             pcNew->mHeight = iHeight;
 
 
             unsigned int iSkip = 0;
             unsigned int iSkip = 0;
-            ParseTextureColorData(szCurrent,iMasked,&iSkip,pcNew);
+            ParseTextureColorData(szCurrent,iMasked,&iSkip,pcNew.get());
 
 
             // skip length of texture data
             // skip length of texture data
             szCurrent += iSkip;
             szCurrent += iSkip;
@@ -588,7 +590,7 @@ void MDLImporter::ParseSkinLump_3DGS_MDL7(
     // texture instead of material colors ... posssible they have
     // texture instead of material colors ... posssible they have
     // been converted to MDL7 from other formats, such as MDL5
     // been converted to MDL7 from other formats, such as MDL5
     aiColor4D clrTexture;
     aiColor4D clrTexture;
-    if (pcNew)clrTexture = ReplaceTextureWithColor(pcNew);
+    if (pcNew)clrTexture = ReplaceTextureWithColor(pcNew.get());
     else clrTexture.r = get_qnan();
     else clrTexture.r = get_qnan();
 
 
     // check whether a material definition is contained in the skin
     // check whether a material definition is contained in the skin
@@ -680,8 +682,7 @@ void MDLImporter::ParseSkinLump_3DGS_MDL7(
     // we don't need the texture anymore
     // we don't need the texture anymore
     if (is_not_qnan(clrTexture.r))
     if (is_not_qnan(clrTexture.r))
     {
     {
-        delete pcNew;
-        pcNew = NULL;
+        pcNew.reset();
     }
     }
 
 
     // If an ASCII effect description (HLSL?) is contained in the file,
     // If an ASCII effect description (HLSL?) is contained in the file,
@@ -716,7 +717,7 @@ void MDLImporter::ParseSkinLump_3DGS_MDL7(
         {
         {
             pScene->mNumTextures = 1;
             pScene->mNumTextures = 1;
             pScene->mTextures = new aiTexture*[1];
             pScene->mTextures = new aiTexture*[1];
-            pScene->mTextures[0] = pcNew;
+            pScene->mTextures[0] = pcNew.release();
         }
         }
         else
         else
         {
         {
@@ -726,16 +727,13 @@ void MDLImporter::ParseSkinLump_3DGS_MDL7(
                 pScene->mTextures[i] = pc[i];
                 pScene->mTextures[i] = pc[i];
             }
             }
 
 
-            pScene->mTextures[pScene->mNumTextures] = pcNew;
+            pScene->mTextures[pScene->mNumTextures] = pcNew.release();
             pScene->mNumTextures++;
             pScene->mNumTextures++;
             delete[] pc;
             delete[] pc;
         }
         }
     }
     }
     VALIDATE_FILE_SIZE(szCurrent);
     VALIDATE_FILE_SIZE(szCurrent);
     *szCurrentOut = szCurrent;
     *szCurrentOut = szCurrent;
-    if ( nullptr != pcNew ) {
-        delete pcNew;
-    }
 }
 }
 
 
 // ------------------------------------------------------------------------------------------------
 // ------------------------------------------------------------------------------------------------

+ 0 - 2
code/MMDImporter.cpp

@@ -141,8 +141,6 @@ void MMDImporter::CreateDataFromImport(const pmx::PmxModel *pModel,
   aiNode *pNode = new aiNode;
   aiNode *pNode = new aiNode;
   if (!pModel->model_name.empty()) {
   if (!pModel->model_name.empty()) {
     pNode->mName.Set(pModel->model_name);
     pNode->mName.Set(pModel->model_name);
-  } else {
-    ai_assert(false);
   }
   }
 
 
   pScene->mRootNode = pNode;
   pScene->mRootNode = pNode;

+ 12 - 0
code/OpenGEXImporter.cpp

@@ -778,10 +778,22 @@ static void fillColor4( aiColor4D *col4, Value *vals ) {
     Value *next( vals );
     Value *next( vals );
     col4->r = next->getFloat();
     col4->r = next->getFloat();
     next = next->m_next;
     next = next->m_next;
+    if (!next) {
+        throw DeadlyImportError( "OpenGEX: Not enough values to fill 4-element color, only 1" );
+    }
+
     col4->g = next->getFloat();
     col4->g = next->getFloat();
     next = next->m_next;
     next = next->m_next;
+    if (!next) {
+        throw DeadlyImportError( "OpenGEX: Not enough values to fill 4-element color, only 2" );
+    }
+
     col4->b = next->getFloat();
     col4->b = next->getFloat();
     next = next->m_next;
     next = next->m_next;
+    if (!next) {
+        throw DeadlyImportError( "OpenGEX: Not enough values to fill 4-element color, only 3" );
+    }
+
     col4->a = next->getFloat();
     col4->a = next->getFloat();
 }
 }
 
 

+ 1 - 0
code/Q3BSPZipArchive.cpp

@@ -43,6 +43,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 
 #include "Q3BSPZipArchive.h"
 #include "Q3BSPZipArchive.h"
 #include <cassert>
 #include <cassert>
+#include <cstdlib>
 #include <assimp/ai_assert.h>
 #include <assimp/ai_assert.h>
 
 
 namespace Assimp {
 namespace Assimp {

+ 4 - 1
code/RawLoader.cpp

@@ -243,8 +243,11 @@ void RAWImporter::InternReadFile( const std::string& pFile,
     {
     {
         cc = &pScene->mRootNode;
         cc = &pScene->mRootNode;
         pScene->mRootNode->mNumChildren = 0;
         pScene->mRootNode->mNumChildren = 0;
+    } else {
+        cc = new aiNode*[pScene->mRootNode->mNumChildren];
+        memset(cc, 0, sizeof(aiNode*) * pScene->mRootNode->mNumChildren);
+        pScene->mRootNode->mChildren = cc;
     }
     }
-    else cc = pScene->mRootNode->mChildren = new aiNode*[pScene->mRootNode->mNumChildren];
 
 
     pScene->mNumMaterials = pScene->mNumMeshes;
     pScene->mNumMaterials = pScene->mNumMeshes;
     aiMaterial** mats = pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
     aiMaterial** mats = pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];

+ 4 - 0
code/RemoveComments.cpp

@@ -66,6 +66,10 @@ void CommentRemover::RemoveLineComments(const char* szComment,
         if (!strncmp(szBuffer,szComment,len)) {
         if (!strncmp(szBuffer,szComment,len)) {
             while (!IsLineEnd(*szBuffer))
             while (!IsLineEnd(*szBuffer))
                 *szBuffer++ = chReplacement;
                 *szBuffer++ = chReplacement;
+
+            if (!*szBuffer) {
+                break;
+            }
         }
         }
         ++szBuffer;
         ++szBuffer;
     }
     }

+ 5 - 6
contrib/irrXML/CXMLReaderImpl.h

@@ -9,6 +9,8 @@
 #include "irrString.h"
 #include "irrString.h"
 #include "irrArray.h"
 #include "irrArray.h"
 
 
+#include <cassert>
+
 using namespace Assimp;
 using namespace Assimp;
 
 
 #ifdef _DEBUG
 #ifdef _DEBUG
@@ -664,12 +666,9 @@ private:
 			TextData = new char_type[sizeWithoutHeader];
 			TextData = new char_type[sizeWithoutHeader];
 
 
 			// MSVC debugger complains here about loss of data ...
 			// MSVC debugger complains here about loss of data ...
-
-
-			// FIXME - gcc complains about 'shift width larger than width of type'
-			// for T == unsigned long. Avoid it by messing around volatile ..
-			volatile unsigned int c = 3;
-			const src_char_type cc = (src_char_type)((((uint64_t)1u << (sizeof( char_type)<<c)) - 1));
+			size_t numShift = sizeof( char_type) * 8;
+			assert(numShift < 64);
+			const src_char_type cc = (src_char_type)(((uint64_t(1u) << numShift) - 1));
 			for (int i=0; i<sizeWithoutHeader; ++i)
 			for (int i=0; i<sizeWithoutHeader; ++i)
 				TextData[i] = char_type( source[i] & cc); 
 				TextData[i] = char_type( source[i] & cc);