From 67c258e75dc22fd4e8539e5f58f6c5e0142e3b26 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 5 Jan 2016 11:38:06 +0100 Subject: [PATCH] snprintf: change next couple of sprintf to snprintf. --- code/3DSConverter.cpp | 2 +- code/ACLoader.cpp | 2 +- code/LWOLoader.cpp | 4 ++-- code/LWSLoader.cpp | 4 ++-- code/MD5Loader.cpp | 2 +- code/MD5Parser.cpp | 4 ++-- code/MDLLoader.cpp | 14 +++++++++----- code/MDLMaterialLoader.cpp | 2 +- code/NFFLoader.cpp | 6 +++--- 9 files changed, 22 insertions(+), 18 deletions(-) diff --git a/code/3DSConverter.cpp b/code/3DSConverter.cpp index 940e9c5ea..5b9eb02c8 100644 --- a/code/3DSConverter.cpp +++ b/code/3DSConverter.cpp @@ -757,7 +757,7 @@ void Discreet3DSImporter::GenerateNodeGraph(aiScene* pcOut) pcNode->mNumMeshes = 1; // Build a name for the node - pcNode->mName.length = sprintf(pcNode->mName.data,"3DSMesh_%u",i); + pcNode->mName.length = snprintf(pcNode->mName.data, MAXLEN, "3DSMesh_%u",i); } // Build dummy nodes for all cameras diff --git a/code/ACLoader.cpp b/code/ACLoader.cpp index f5dfbea8d..649637f3d 100644 --- a/code/ACLoader.cpp +++ b/code/ACLoader.cpp @@ -744,7 +744,7 @@ aiNode* AC3DImporter::ConvertObjectSection(Object& object, // there shouldn't be more than one world, but we don't care case Object::World: - node->mName.length = ::sprintf(node->mName.data,"ACWorld_%i",worlds++); + node->mName.length = ::snprintf(node->mName.data, MAXLEN, "ACWorld_%i",worlds++); break; } } diff --git a/code/LWOLoader.cpp b/code/LWOLoader.cpp index 6847e770c..bda07934d 100644 --- a/code/LWOLoader.cpp +++ b/code/LWOLoader.cpp @@ -1167,7 +1167,7 @@ void LWOImporter::LoadLWO2Clip(unsigned int length) std::ostringstream ss; GetS0(s,head.length); - head.length -= (unsigned int)s.length()+1; + head.length -= (uint16_t)s.length()+1; ss << s; ss << std::setw(digits) << offset + start; GetS0(s,head.length); @@ -1370,7 +1370,7 @@ void LWOImporter::LoadLWO2File() // if the name is empty, generate a default name if (layer.mName.empty()) { char buffer[128]; // should be sufficiently large - ::sprintf(buffer,"Layer_%i", iUnnamed++); + ::snprintf(buffer, 128, "Layer_%i", iUnnamed++); layer.mName = buffer; } diff --git a/code/LWSLoader.cpp b/code/LWSLoader.cpp index a98fdb41a..5ec6bb448 100644 --- a/code/LWSLoader.cpp +++ b/code/LWSLoader.cpp @@ -323,11 +323,11 @@ void LWSImporter::SetupNodeName(aiNode* nd, LWS::NodeDesc& src) else ++s; std::string::size_type t = src.path.substr(s).find_last_of("."); - nd->mName.length = ::sprintf(nd->mName.data,"%s_(%08X)",src.path.substr(s).substr(0,t).c_str(),combined); + nd->mName.length = ::snprintf(nd->mName.data, MAXLEN, "%s_(%08X)",src.path.substr(s).substr(0,t).c_str(),combined); return; } } - nd->mName.length = ::sprintf(nd->mName.data,"%s_(%08X)",src.name,combined); + nd->mName.length = ::snprintf(nd->mName.data, MAXLEN, "%s_(%08X)",src.name,combined); } // ------------------------------------------------------------------------------------------------ diff --git a/code/MD5Loader.cpp b/code/MD5Loader.cpp index bc8dfcd6c..9e4fcaa8e 100644 --- a/code/MD5Loader.cpp +++ b/code/MD5Loader.cpp @@ -735,7 +735,7 @@ void MD5Importer::LoadMD5CameraFile () for (std::vector::const_iterator it = cuts.begin(); it != cuts.end()-1; ++it) { aiAnimation* anim = *tmp++ = new aiAnimation(); - anim->mName.length = ::sprintf(anim->mName.data,"anim%u_from_%u_to_%u",(unsigned int)(it-cuts.begin()),(*it),*(it+1)); + anim->mName.length = ::snprintf(anim->mName.data, MAXLEN, "anim%u_from_%u_to_%u",(unsigned int)(it-cuts.begin()),(*it),*(it+1)); anim->mTicksPerSecond = cameraParser.fFrameRate; anim->mChannels = new aiNodeAnim*[anim->mNumChannels = 1]; diff --git a/code/MD5Parser.cpp b/code/MD5Parser.cpp index 5e9012f13..2bc1e9d1d 100644 --- a/code/MD5Parser.cpp +++ b/code/MD5Parser.cpp @@ -85,7 +85,7 @@ MD5Parser::MD5Parser(char* _buffer, unsigned int _fileSize ) if ( !DefaultLogger::isNullLogger()) { char szBuffer[128]; // should be sufficiently large - ::sprintf(szBuffer,"MD5Parser end. Parsed %i sections",(int)mSections.size()); + ::snprintf(szBuffer,128,"MD5Parser end. Parsed %i sections",(int)mSections.size()); DefaultLogger::get()->debug(szBuffer); } } @@ -95,7 +95,7 @@ MD5Parser::MD5Parser(char* _buffer, unsigned int _fileSize ) /*static*/ AI_WONT_RETURN void MD5Parser::ReportError (const char* error, unsigned int line) { char szBuffer[1024]; - ::sprintf(szBuffer,"[MD5] Line %u: %s",line,error); + ::snprintf(szBuffer, 1024, "[MD5] Line %u: %s",line,error); throw DeadlyImportError(szBuffer); } diff --git a/code/MDLLoader.cpp b/code/MDLLoader.cpp index 8f8d0228f..549da6ad1 100644 --- a/code/MDLLoader.cpp +++ b/code/MDLLoader.cpp @@ -942,7 +942,7 @@ void MDLImporter::CalcAbsBoneMatrices_3DGS_MDL7(MDL::IntBone_MDL7** apcOutBones) if (AI_MDL7_BONE_STRUCT_SIZE__NAME_IS_NOT_THERE == pcHeader->bone_stc_size) { // no real name for our poor bone is specified :-( - pcOutBone->mName.length = ::sprintf(pcOutBone->mName.data, + pcOutBone->mName.length = ::snprintf(pcOutBone->mName.data, MAXLEN, "UnnamedBone_%i",iBone); } else { @@ -1384,7 +1384,8 @@ void MDLImporter::InternReadFile_3DGS_MDL7( ) avOutList[i].reserve(3); // buffer to held the names of all groups in the file - char* aszGroupNameBuffer = new char[AI_MDL7_MAX_GROUPNAMESIZE*pcHeader->groups_num]; + const size_t buffersize( AI_MDL7_MAX_GROUPNAMESIZE*pcHeader->groups_num ); + char* aszGroupNameBuffer = new char[ buffersize ]; // read all groups for (unsigned int iGroup = 0; iGroup < (unsigned int)pcHeader->groups_num;++iGroup) { @@ -1544,9 +1545,12 @@ void MDLImporter::InternReadFile_3DGS_MDL7( ) // setup the name of the node char* const szBuffer = &aszGroupNameBuffer[i*AI_MDL7_MAX_GROUPNAMESIZE]; - if ('\0' == *szBuffer) - pcNode->mName.length = ::sprintf(szBuffer,"Group_%u",p); - else pcNode->mName.length = ::strlen(szBuffer); + if ('\0' == *szBuffer) { + const size_t maxSize(buffersize - (i*AI_MDL7_MAX_GROUPNAMESIZE)); + pcNode->mName.length = ::snprintf(szBuffer, maxSize, "Group_%u", p); + } else { + pcNode->mName.length = ::strlen(szBuffer); + } ::strcpy(pcNode->mName.data,szBuffer); ++p; } diff --git a/code/MDLMaterialLoader.cpp b/code/MDLMaterialLoader.cpp index a5de2d9e3..5025bd97a 100644 --- a/code/MDLMaterialLoader.cpp +++ b/code/MDLMaterialLoader.cpp @@ -699,7 +699,7 @@ void MDLImporter::ParseSkinLump_3DGS_MDL7( // place this as diffuse texture char szCurrent[5]; - ::sprintf(szCurrent,"*%i",this->pScene->mNumTextures); + ::snprintf(szCurrent,5,"*%i",this->pScene->mNumTextures); aiString szFile; const size_t iLen = strlen((const char*)szCurrent); diff --git a/code/NFFLoader.cpp b/code/NFFLoader.cpp index 1486ecde9..3892bf4e2 100644 --- a/code/NFFLoader.cpp +++ b/code/NFFLoader.cpp @@ -862,7 +862,7 @@ void NFFImporter::InternReadFile( const std::string& pFile, currentMesh.faces.resize(currentMesh.vertices.size()/3,3); // generate a name for the mesh - ::sprintf(currentMesh.name,"sphere_%i",sphere++); + ::snprintf(currentMesh.name,128,"sphere_%i",sphere++); } // 'dod' - dodecahedron else if (TokenMatch(sz,"dod",3)) @@ -879,7 +879,7 @@ void NFFImporter::InternReadFile( const std::string& pFile, currentMesh.faces.resize(currentMesh.vertices.size()/3,3); // generate a name for the mesh - ::sprintf(currentMesh.name,"dodecahedron_%i",dodecahedron++); + ::snprintf(currentMesh.name,128,"dodecahedron_%i",dodecahedron++); } // 'oct' - octahedron @@ -897,7 +897,7 @@ void NFFImporter::InternReadFile( const std::string& pFile, currentMesh.faces.resize(currentMesh.vertices.size()/3,3); // generate a name for the mesh - ::sprintf(currentMesh.name,"octahedron_%i",octahedron++); + ::snprintf(currentMesh.name,128,"octahedron_%i",octahedron++); } // 'tet' - tetrahedron