Forráskód Böngészése

Remove strcpy. (#5802)

* Remove strcpy.

* Sonarcube: Add more strcpy replacements

* BlenderLoader: Replace strncpy by memcpy
Kim Kulling 8 hónapja
szülő
commit
41cc2f66a4

+ 1 - 1
code/AssetLib/AMF/AMFImporter_Postprocess.cpp

@@ -1,4 +1,4 @@
-/*
+/*
 ---------------------------------------------------------------------------
 Open Asset Import Library (assimp)
 ---------------------------------------------------------------------------

+ 3 - 4
code/AssetLib/Blender/BlenderLoader.cpp

@@ -1,11 +1,9 @@
-
 /*
 Open Asset Import Library (assimp)
 ----------------------------------------------------------------------
 
 Copyright (c) 2006-2024, assimp team
 
-
 All rights reserved.
 
 Redistribution and use of this software in source and binary forms,
@@ -495,8 +493,9 @@ void BlenderImporter::BuildDefaultMaterial(Blender::ConversionData &conv_data) {
             if (index == static_cast<unsigned int>(-1)) {
                 // Setup a default material.
                 std::shared_ptr<Material> p(new Material());
-                ai_assert(::strlen(AI_DEFAULT_MATERIAL_NAME) < sizeof(p->id.name) - 2);
-                strcpy(p->id.name + 2, AI_DEFAULT_MATERIAL_NAME);
+                const size_t len = ::strlen(AI_DEFAULT_MATERIAL_NAME);
+                ai_assert(len < sizeof(p->id.name) - 2);
+                memcpy(p->id.name + 2, AI_DEFAULT_MATERIAL_NAME, len);
 
                 // Note: MSVC11 does not zero-initialize Material here, although it should.
                 // Thus all relevant fields should be explicitly initialized. We cannot add

+ 2 - 2
code/AssetLib/NFF/NFFLoader.cpp

@@ -546,9 +546,9 @@ void NFFImporter::InternReadFile(const std::string &file, aiScene *pScene, IOSys
                         // We need to add a new mesh to the list. We assign
                         // an unique name to it to make sure the scene will
                         // pass the validation step for the moment.
-                        // TODO: fix naming of objects in the scenegraph later
+                        // TODO: fix naming of objects in the scene-graph later
                         if (objectName.length()) {
-                            ::strcpy(mesh->name, objectName.c_str());
+                            ::strncpy(mesh->name, objectName.c_str(), objectName.size());
                             ASSIMP_itoa10(&mesh->name[objectName.length()], 30, subMeshIdx++);
                         }
 

+ 3 - 4
code/AssetLib/SMD/SMDLoader.cpp

@@ -455,11 +455,10 @@ void SMDImporter::CreateOutputNodes() {
         delete pcOldRoot;
 
         pScene->mRootNode->mParent = nullptr;
-    }
-    else
-    {
-        ::strcpy(pScene->mRootNode->mName.data, "<SMD_root>");
+    } else {
+        static constexpr char rootName[11] = "<SMD_root>";
         pScene->mRootNode->mName.length = 10;
+        ::strncpy(pScene->mRootNode->mName.data, rootName, pScene->mRootNode->mName.length);
     }
 }
 

+ 7 - 4
code/AssetLib/glTF/glTFImporter.cpp

@@ -631,8 +631,9 @@ void glTFImporter::ImportEmbeddedTextures(glTF::Asset &r) {
             numEmbeddedTexs += 1;
     }
 
-    if (numEmbeddedTexs == 0)
+    if (numEmbeddedTexs == 0) {
         return;
+    }
 
     mScene->mTextures = new aiTexture *[numEmbeddedTexs];
 
@@ -657,11 +658,13 @@ void glTFImporter::ImportEmbeddedTextures(glTF::Asset &r) {
         if (!img.mimeType.empty()) {
             const char *ext = strchr(img.mimeType.c_str(), '/') + 1;
             if (ext) {
-                if (strcmp(ext, "jpeg") == 0) ext = "jpg";
+                if (strncmp(ext, "jpeg", 4) == 0) {
+                    ext = "jpg";
+                }
 
-                size_t len = strlen(ext);
+                const size_t len = strlen(ext);
                 if (len <= 3) {
-                    strcpy(tex->achFormatHint, ext);
+                    strncpy(tex->achFormatHint, ext, len);
                 }
             }
         }

+ 3 - 3
code/AssetLib/glTF2/glTF2Importer.cpp

@@ -1630,7 +1630,7 @@ void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset &r) {
         if (!img.mimeType.empty()) {
             const char *ext = strchr(img.mimeType.c_str(), '/') + 1;
             if (ext) {
-                if (strcmp(ext, "jpeg") == 0) {
+                if (strncmp(ext, "jpeg", 4) == 0) {
                     ext = "jpg";
                 } else if (strcmp(ext, "ktx2") == 0) { // basisu: ktx remains
                     ext = "kx2";
@@ -1638,9 +1638,9 @@ void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset &r) {
                     ext = "bu";
                 }
 
-                size_t len = strlen(ext);
+                const size_t len = strlen(ext);
                 if (len <= 3) {
-                    strcpy(tex->achFormatHint, ext);
+                    strncpy(tex->achFormatHint, ext, len);
                 }
             }
         }

+ 0 - 9
code/Common/SceneCombiner.cpp

@@ -316,15 +316,6 @@ void SceneCombiner::MergeScenes(aiScene **_dest, aiScene *master, std::vector<At
         boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rndGen(rng, dist);
 #endif
         for (unsigned int i = 1; i < src.size(); ++i) {
-            //if (i != duplicates[i])
-            //{
-            //  // duplicate scenes share the same UID
-            //  ::strcpy( src[i].id, src[duplicates[i]].id );
-            //  src[i].idlen = src[duplicates[i]].idlen;
-
-            //  continue;
-            //}
-
             src[i].idlen = ai_snprintf(src[i].id, 32, "$%.6X$_", i);
 
             if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {

+ 1 - 2
code/Material/MaterialSystem.cpp

@@ -4,7 +4,6 @@ Open Asset Import Library (assimp)
 
 Copyright (c) 2006-2024, assimp team
 
-
 All rights reserved.
 
 Redistribution and use of this software in source and binary forms,
@@ -74,7 +73,7 @@ aiReturn aiGetMaterialProperty(const aiMaterial *pMat,
         aiMaterialProperty *prop = pMat->mProperties[i];
 
         if (prop /* just for safety ... */
-                && 0 == strcmp(prop->mKey.data, pKey) && (UINT_MAX == type || prop->mSemantic == type) /* UINT_MAX is a wild-card, but this is undocumented :-) */
+                && 0 == strncmp(prop->mKey.data, pKey, strlen(pKey)) && (UINT_MAX == type || prop->mSemantic == type) /* UINT_MAX is a wild-card, but this is undocumented :-) */
                 && (UINT_MAX == index || prop->mIndex == index)) {
             *pPropOut = pMat->mProperties[i];
             return AI_SUCCESS;