Skip to content

Commit 9723b35

Browse files
authored
Obj: Fix Sonarcube findings (assimp#5873)
* Obj: Fix Sonarcube findings
1 parent f7bb1bc commit 9723b35

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

code/AssetLib/Obj/ObjFileImporter.cpp

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static constexpr aiImporterDesc desc = {
6767
"obj"
6868
};
6969

70-
static const unsigned int ObjMinSize = 16;
70+
static constexpr unsigned int ObjMinSize = 16u;
7171

7272
namespace Assimp {
7373

@@ -163,7 +163,7 @@ void ObjFileImporter::InternReadFile(const std::string &file, aiScene *pScene, I
163163
// ------------------------------------------------------------------------------------------------
164164
// Create the data from parsed obj-file
165165
void ObjFileImporter::CreateDataFromImport(const ObjFile::Model *pModel, aiScene *pScene) {
166-
if (nullptr == pModel) {
166+
if (pModel == nullptr) {
167167
return;
168168
}
169169

@@ -178,7 +178,6 @@ void ObjFileImporter::CreateDataFromImport(const ObjFile::Model *pModel, aiScene
178178
}
179179

180180
if (!pModel->mObjects.empty()) {
181-
182181
unsigned int meshCount = 0;
183182
unsigned int childCount = 0;
184183

@@ -258,8 +257,7 @@ void ObjFileImporter::CreateDataFromImport(const ObjFile::Model *pModel, aiScene
258257
aiNode *ObjFileImporter::createNodes(const ObjFile::Model *pModel, const ObjFile::Object *pObject,
259258
aiNode *pParent, aiScene *pScene,
260259
std::vector<std::unique_ptr<aiMesh>> &MeshArray) {
261-
ai_assert(nullptr != pModel);
262-
if (nullptr == pObject) {
260+
if (nullptr == pObject || pModel == nullptr) {
263261
return nullptr;
264262
}
265263

@@ -311,16 +309,13 @@ aiNode *ObjFileImporter::createNodes(const ObjFile::Model *pModel, const ObjFile
311309
// ------------------------------------------------------------------------------------------------
312310
// Create topology data
313311
std::unique_ptr<aiMesh> ObjFileImporter::createTopology(const ObjFile::Model *pModel, const ObjFile::Object *pData, unsigned int meshIndex) {
314-
// Checking preconditions
315-
ai_assert(nullptr != pModel);
316-
317-
if (nullptr == pData) {
312+
if (nullptr == pData || pModel == nullptr) {
318313
return nullptr;
319314
}
320315

321316
// Create faces
322317
ObjFile::Mesh *pObjMesh = pModel->mMeshes[meshIndex];
323-
if (!pObjMesh) {
318+
if (pObjMesh == nullptr) {
324319
return nullptr;
325320
}
326321

@@ -335,7 +330,10 @@ std::unique_ptr<aiMesh> ObjFileImporter::createTopology(const ObjFile::Model *pM
335330

336331
for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++) {
337332
const ObjFile::Face *inp = pObjMesh->m_Faces[index];
338-
333+
if (inp == nullptr) {
334+
continue;
335+
}
336+
339337
if (inp->mPrimitiveType == aiPrimitiveType_LINE) {
340338
pMesh->mNumFaces += static_cast<unsigned int>(inp->m_vertices.size() - 1);
341339
pMesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
@@ -352,14 +350,14 @@ std::unique_ptr<aiMesh> ObjFileImporter::createTopology(const ObjFile::Model *pM
352350
}
353351
}
354352

355-
unsigned int uiIdxCount(0u);
353+
unsigned int uiIdxCount = 0u;
356354
if (pMesh->mNumFaces > 0) {
357355
pMesh->mFaces = new aiFace[pMesh->mNumFaces];
358356
if (pObjMesh->m_uiMaterialIndex != ObjFile::Mesh::NoMaterial) {
359357
pMesh->mMaterialIndex = pObjMesh->m_uiMaterialIndex;
360358
}
361359

362-
unsigned int outIndex(0);
360+
unsigned int outIndex = 0u;
363361

364362
// Copy all data from all stored meshes
365363
for (auto &face : pObjMesh->m_Faces) {
@@ -403,11 +401,14 @@ void ObjFileImporter::createVertexArray(const ObjFile::Model *pModel,
403401
aiMesh *pMesh,
404402
unsigned int numIndices) {
405403
// Checking preconditions
406-
ai_assert(nullptr != pCurrentObject);
404+
if (pCurrentObject == nullptr) {
405+
return;
406+
}
407407

408408
// Break, if no faces are stored in object
409-
if (pCurrentObject->m_Meshes.empty())
409+
if (pCurrentObject->m_Meshes.empty()) {
410410
return;
411+
}
411412

412413
// Get current mesh
413414
ObjFile::Mesh *pObjMesh = pModel->mMeshes[uiMeshIndex];
@@ -586,11 +587,12 @@ void ObjFileImporter::createMaterials(const ObjFile::Model *pModel, aiScene *pSc
586587
it = pModel->mMaterialMap.find(pModel->mMaterialLib[matIndex]);
587588

588589
// No material found, use the default material
589-
if (pModel->mMaterialMap.end() == it)
590+
if (pModel->mMaterialMap.end() == it) {
590591
continue;
592+
}
591593

592594
aiMaterial *mat = new aiMaterial;
593-
ObjFile::Material *pCurrentMaterial = (*it).second;
595+
ObjFile::Material *pCurrentMaterial = it->second;
594596
mat->AddProperty(&pCurrentMaterial->MaterialName, AI_MATKEY_NAME);
595597

596598
// convert illumination model
@@ -777,8 +779,11 @@ void ObjFileImporter::createMaterials(const ObjFile::Model *pModel, aiScene *pSc
777779
// Appends this node to the parent node
778780
void ObjFileImporter::appendChildToParentNode(aiNode *pParent, aiNode *pChild) {
779781
// Checking preconditions
780-
ai_assert(nullptr != pParent);
781-
ai_assert(nullptr != pChild);
782+
if (pParent == nullptr || pChild == nullptr) {
783+
ai_assert(nullptr != pParent);
784+
ai_assert(nullptr != pChild);
785+
return;
786+
}
782787

783788
// Assign parent to child
784789
pChild->mParent = pParent;

0 commit comments

Comments
 (0)