Skip to content

Commit bb9b30a

Browse files
committed
Mass replaces all sprintf with safer snprintf equivalents to get rid of deprecation warning: 'sprintf' is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead.
1 parent 76903de commit bb9b30a

File tree

58 files changed

+175
-175
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+175
-175
lines changed

cocos/2d/CCFontAtlasCache.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile)
181181
FontAtlas* FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
182182
{
183183
char key[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
184-
sprintf(key,"name:%p_%d_%d_%d",texture->getBackendTexture(),itemWidth,itemHeight,startCharMap);
184+
snprintf(key, sizeof(key),"name:%p_%d_%d_%d",texture->getBackendTexture(),itemWidth,itemHeight,startCharMap);
185185
std::string atlasName = key;
186186

187187
auto it = _atlasMap.find(atlasName);

cocos/2d/CCLabel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2334,7 +2334,7 @@ void Label::updateColor()
23342334
std::string Label::getDescription() const
23352335
{
23362336
char tmp[50];
2337-
sprintf(tmp, "<Label | Tag = %d, Label = >", _tag);
2337+
snprintf(tmp, sizeof(tmp), "<Label | Tag = %d, Label = >", _tag);
23382338
std::string ret = tmp;
23392339
ret += _utf8Text;
23402340

cocos/2d/CCSprite.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Sprite* Sprite::createWithSpriteFrameName(const std::string& spriteFrameName)
125125

126126
#if COCOS2D_DEBUG > 0
127127
char msg[256] = {0};
128-
sprintf(msg, "Invalid spriteFrameName: %s", spriteFrameName.c_str());
128+
snprintf(msg, sizeof(msg), "Invalid spriteFrameName: %s", spriteFrameName.c_str());
129129
CCASSERT(frame != nullptr, msg);
130130
#endif
131131

@@ -1730,9 +1730,9 @@ std::string Sprite::getDescription() const
17301730
{
17311731
char textureDescriptor[100];
17321732
if (_renderMode == RenderMode::QUAD_BATCHNODE)
1733-
sprintf(textureDescriptor, "<Sprite | Tag = %d, TextureID = %p>", _tag, _batchNode->getTextureAtlas()->getTexture()->getBackendTexture());
1733+
snprintf(textureDescriptor, sizeof(textureDescriptor), "<Sprite | Tag = %d, TextureID = %p>", _tag, _batchNode->getTextureAtlas()->getTexture()->getBackendTexture());
17341734
else
1735-
sprintf(textureDescriptor, "<Sprite | Tag = %d, TextureID = %p>", _tag, _texture->getBackendTexture());
1735+
snprintf(textureDescriptor, sizeof(textureDescriptor), "<Sprite | Tag = %d, TextureID = %p>", _tag, _texture->getBackendTexture());
17361736

17371737
return textureDescriptor;
17381738
}

cocos/3d/CCBundle3D.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ bool Bundle3D::loadObj(MeshDatas& meshdatas, MaterialDatas& materialdatas, NodeD
236236
tex.wrapS = backend::SamplerAddressMode::CLAMP_TO_EDGE;
237237
tex.wrapT = backend::SamplerAddressMode::CLAMP_TO_EDGE;
238238

239-
sprintf(str, "%d", ++i);
239+
snprintf(str, sizeof(str), "%d", ++i);
240240
materialdata.textures.push_back(tex);
241241
materialdata.id = str;
242242
material.name = str;
@@ -308,7 +308,7 @@ bool Bundle3D::loadObj(MeshDatas& meshdatas, MaterialDatas& materialdatas, NodeD
308308
for (auto& submesh : subMeshMap) {
309309
meshdata->subMeshIndices.push_back(submesh.second);
310310
meshdata->subMeshAABB.push_back(calculateAABB(meshdata->vertex, meshdata->getPerVertexSize(), submesh.second));
311-
sprintf(str, "%d", ++i);
311+
snprintf(str, sizeof(str), "%d", ++i);
312312
meshdata->subMeshIds.push_back(str);
313313

314314
auto modelnode = new (std::nothrow) ModelData();
@@ -1103,7 +1103,7 @@ bool Bundle3D::loadBinary(const std::string& path)
11031103
}
11041104

11051105
char version[20] = {0};
1106-
sprintf(version, "%d.%d", ver[0], ver[1]);
1106+
snprintf(version, sizeof(version), "%d.%d", ver[0], ver[1]);
11071107
_version = version;
11081108

11091109
// Read ref table size

cocos/3d/CCTerrain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ void Terrain::cacheUniformAttribLocation()
841841
for (int i = 0; i < _maxDetailMapValue; ++i)
842842
{
843843
char str[20];
844-
sprintf(str, "u_texture%d", i);
844+
snprintf(str, sizeof(str), "u_texture%d", i);
845845
_detailMapLocation[i] = _programState->getUniformLocation(str);
846846
}
847847

cocos/base/CCConsole.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,17 +1242,17 @@ void Console::commandProjection(int fd, const std::string& /*args*/)
12421242
auto proj = director->getProjection();
12431243
switch (proj) {
12441244
case cocos2d::Director::Projection::_2D:
1245-
sprintf(buf,"2d");
1245+
snprintf(buf, sizeof(buf),"2d");
12461246
break;
12471247
case cocos2d::Director::Projection::_3D:
1248-
sprintf(buf,"3d");
1248+
snprintf(buf, sizeof(buf),"3d");
12491249
break;
12501250
case cocos2d::Director::Projection::CUSTOM:
1251-
sprintf(buf,"custom");
1251+
snprintf(buf, sizeof(buf),"custom");
12521252
break;
12531253

12541254
default:
1255-
sprintf(buf,"unknown");
1255+
snprintf(buf, sizeof(buf),"unknown");
12561256
break;
12571257
}
12581258
Console::Utility::mydprintf(fd, "Current projection: %s\n", buf);

cocos/base/CCDirector.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ void Director::showStats()
11801180
// to make the FPS stable
11811181
if (_accumDt > CC_DIRECTOR_STATS_INTERVAL)
11821182
{
1183-
sprintf(buffer, "%.1f / %.3f", _frames / _accumDt, _secondsPerFrame);
1183+
snprintf(buffer, sizeof(buffer), "%.1f / %.3f", _frames / _accumDt, _secondsPerFrame);
11841184
_FPSLabel->setString(buffer);
11851185
_accumDt = 0;
11861186
_frames = 0;
@@ -1189,13 +1189,13 @@ void Director::showStats()
11891189
auto currentCalls = (unsigned long)_renderer->getDrawnBatches();
11901190
auto currentVerts = (unsigned long)_renderer->getDrawnVertices();
11911191
if( currentCalls != prevCalls ) {
1192-
sprintf(buffer, "GL calls:%6lu", currentCalls);
1192+
snprintf(buffer, sizeof(buffer), "GL calls:%6lu", currentCalls);
11931193
_drawnBatchesLabel->setString(buffer);
11941194
prevCalls = currentCalls;
11951195
}
11961196

11971197
if( currentVerts != prevVerts) {
1198-
sprintf(buffer, "GL verts:%6lu", currentVerts);
1198+
snprintf(buffer, sizeof(buffer), "GL verts:%6lu", currentVerts);
11991199
_drawnVerticesLabel->setString(buffer);
12001200
prevVerts = currentVerts;
12011201
}

cocos/base/CCProfiling.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ std::string ProfilingTimer::getDescription() const
115115
{
116116
static char s_description[512] = {0};
117117

118-
sprintf(s_description, "%s ::\tavg1: %ldu,\tavg2: %ldu,\tmin: %ldu,\tmax: %ldu,\ttotal: %.2fs,\tnr calls: %ld", _nameStr.c_str(), _averageTime1, _averageTime2, minTime, maxTime, totalTime/1000000., numberOfCalls);
118+
snprintf(s_description, sizeof(s_description), "%s ::\tavg1: %ldu,\tavg2: %ldu,\tmin: %ldu,\tmax: %ldu,\ttotal: %.2fs,\tnr calls: %ld", _nameStr.c_str(), _averageTime1, _averageTime2, minTime, maxTime, totalTime/1000000., numberOfCalls);
119119
return s_description;
120120
}
121121

cocos/base/CCUserDefault.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ void UserDefault::setIntegerForKey(const char* pKey, int value)
371371
// format the value
372372
char tmp[50];
373373
memset(tmp, 0, 50);
374-
sprintf(tmp, "%d", value);
374+
snprintf(tmp, sizeof(tmp), "%d", value);
375375

376376
setValueForKey(pKey, tmp);
377377
}
@@ -392,7 +392,7 @@ void UserDefault::setDoubleForKey(const char* pKey, double value)
392392
// format the value
393393
char tmp[50];
394394
memset(tmp, 0, 50);
395-
sprintf(tmp, "%f", value);
395+
snprintf(tmp, sizeof(tmp), "%f", value);
396396

397397
setValueForKey(pKey, tmp);
398398
}

cocos/editor-support/cocostudio/TriggerBase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ using namespace cocostudio;
3232
void sendEvent(unsigned int event)
3333
{
3434
char buf[10];
35-
sprintf(buf, "%d", event);
35+
snprintf(buf, sizeof(buf), "%d", event);
3636
std::string custom_event_name(buf);
3737

3838
EventCustom eventCustom(custom_event_name);

0 commit comments

Comments
 (0)