Pārlūkot izejas kodu

Obj: Fix Sonarcube findings (#5873)

* Obj: Fix Sonarcube findings
Kim Kulling 10 mēneši atpakaļ
vecāks
revīzija
9723b3554b
1 mainītis faili ar 24 papildinājumiem un 19 dzēšanām
  1. 24 19
      code/AssetLib/Obj/ObjFileImporter.cpp

+ 24 - 19
code/AssetLib/Obj/ObjFileImporter.cpp

@@ -67,7 +67,7 @@ static constexpr aiImporterDesc desc = {
     "obj"
 };
 
-static const unsigned int ObjMinSize = 16;
+static constexpr unsigned int ObjMinSize = 16u;
 
 namespace Assimp {
 
@@ -163,7 +163,7 @@ void ObjFileImporter::InternReadFile(const std::string &file, aiScene *pScene, I
 // ------------------------------------------------------------------------------------------------
 //  Create the data from parsed obj-file
 void ObjFileImporter::CreateDataFromImport(const ObjFile::Model *pModel, aiScene *pScene) {
-    if (nullptr == pModel) {
+    if (pModel == nullptr) {
         return;
     }
 
@@ -178,7 +178,6 @@ void ObjFileImporter::CreateDataFromImport(const ObjFile::Model *pModel, aiScene
     }
 
     if (!pModel->mObjects.empty()) {
-
         unsigned int meshCount = 0;
         unsigned int childCount = 0;
 
@@ -258,8 +257,7 @@ void ObjFileImporter::CreateDataFromImport(const ObjFile::Model *pModel, aiScene
 aiNode *ObjFileImporter::createNodes(const ObjFile::Model *pModel, const ObjFile::Object *pObject,
         aiNode *pParent, aiScene *pScene,
         std::vector<std::unique_ptr<aiMesh>> &MeshArray) {
-    ai_assert(nullptr != pModel);
-    if (nullptr == pObject) {
+    if (nullptr == pObject || pModel == nullptr) {
         return nullptr;
     }
 
@@ -311,16 +309,13 @@ aiNode *ObjFileImporter::createNodes(const ObjFile::Model *pModel, const ObjFile
 // ------------------------------------------------------------------------------------------------
 //  Create topology data
 std::unique_ptr<aiMesh> ObjFileImporter::createTopology(const ObjFile::Model *pModel, const ObjFile::Object *pData, unsigned int meshIndex) {
-    // Checking preconditions
-    ai_assert(nullptr != pModel);
-
-    if (nullptr == pData) {
+    if (nullptr == pData || pModel == nullptr) {    
         return nullptr;
     }
 
     // Create faces
     ObjFile::Mesh *pObjMesh = pModel->mMeshes[meshIndex];
-    if (!pObjMesh) {
+    if (pObjMesh == nullptr) {
         return nullptr;
     }
 
@@ -335,7 +330,10 @@ std::unique_ptr<aiMesh> ObjFileImporter::createTopology(const ObjFile::Model *pM
 
     for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++) {
         const ObjFile::Face *inp = pObjMesh->m_Faces[index];
-
+        if (inp == nullptr) {
+            continue;
+        }
+        
         if (inp->mPrimitiveType == aiPrimitiveType_LINE) {
             pMesh->mNumFaces += static_cast<unsigned int>(inp->m_vertices.size() - 1);
             pMesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
@@ -352,14 +350,14 @@ std::unique_ptr<aiMesh> ObjFileImporter::createTopology(const ObjFile::Model *pM
         }
     }
 
-    unsigned int uiIdxCount(0u);
+    unsigned int uiIdxCount = 0u;
     if (pMesh->mNumFaces > 0) {
         pMesh->mFaces = new aiFace[pMesh->mNumFaces];
         if (pObjMesh->m_uiMaterialIndex != ObjFile::Mesh::NoMaterial) {
             pMesh->mMaterialIndex = pObjMesh->m_uiMaterialIndex;
         }
 
-        unsigned int outIndex(0);
+        unsigned int outIndex = 0u;
 
         // Copy all data from all stored meshes
         for (auto &face : pObjMesh->m_Faces) {
@@ -403,11 +401,14 @@ void ObjFileImporter::createVertexArray(const ObjFile::Model *pModel,
         aiMesh *pMesh,
         unsigned int numIndices) {
     // Checking preconditions
-    ai_assert(nullptr != pCurrentObject);
+    if (pCurrentObject == nullptr) {
+        return;
+    }     
 
     // Break, if no faces are stored in object
-    if (pCurrentObject->m_Meshes.empty())
+    if (pCurrentObject->m_Meshes.empty()) {
         return;
+    }
 
     // Get current mesh
     ObjFile::Mesh *pObjMesh = pModel->mMeshes[uiMeshIndex];
@@ -586,11 +587,12 @@ void ObjFileImporter::createMaterials(const ObjFile::Model *pModel, aiScene *pSc
         it = pModel->mMaterialMap.find(pModel->mMaterialLib[matIndex]);
 
         // No material found, use the default material
-        if (pModel->mMaterialMap.end() == it)
+        if (pModel->mMaterialMap.end() == it) {
             continue;
+        }
 
         aiMaterial *mat = new aiMaterial;
-        ObjFile::Material *pCurrentMaterial = (*it).second;
+        ObjFile::Material *pCurrentMaterial = it->second;
         mat->AddProperty(&pCurrentMaterial->MaterialName, AI_MATKEY_NAME);
 
         // convert illumination model
@@ -777,8 +779,11 @@ void ObjFileImporter::createMaterials(const ObjFile::Model *pModel, aiScene *pSc
 //  Appends this node to the parent node
 void ObjFileImporter::appendChildToParentNode(aiNode *pParent, aiNode *pChild) {
     // Checking preconditions
-    ai_assert(nullptr != pParent);
-    ai_assert(nullptr != pChild);
+    if (pParent == nullptr || pChild == nullptr) {
+        ai_assert(nullptr != pParent);
+        ai_assert(nullptr != pChild);
+        return;
+    }
 
     // Assign parent to child
     pChild->mParent = pParent;