Skip to content

Commit 1f7c0c9

Browse files
author
minggo
authored
If path to file is tool long crash is possible. (#19593)
* If path to file is tool long crash is possible. If path to file is tool long crash is possible, because of chart buffer overflow. * Incorrect replacement. Using iterator is better. * Style fix * Correct naming
1 parent 0a9e66a commit 1f7c0c9

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

Diff for: cocos/2d/CCFontAtlasCache.cpp

+21-22
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
NS_CC_BEGIN
3737

3838
std::unordered_map<std::string, FontAtlas *> FontAtlasCache::_atlasMap;
39-
#define ATLAS_MAP_KEY_BUFFER 255
39+
#define ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE 255
4040

4141
void FontAtlasCache::purgeCachedData()
4242
{
@@ -60,15 +60,11 @@ FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config)
6060
useDistanceField = false;
6161
}
6262

63-
char tmp[ATLAS_MAP_KEY_BUFFER];
64-
if (useDistanceField) {
65-
snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "df %.2f %d %s", config->fontSize, config->outlineSize,
66-
realFontFilename.c_str());
67-
} else {
68-
snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%.2f %d %s", config->fontSize, config->outlineSize,
69-
realFontFilename.c_str());
70-
}
71-
std::string atlasName = tmp;
63+
std::string key;
64+
char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
65+
snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, useDistanceField ? "df %.2f %d " : "%.2f %d ", config->fontSize, config->outlineSize);
66+
std::string atlasName(keyPrefix);
67+
atlasName += realFontFilename;
7268

7369
auto it = _atlasMap.find(atlasName);
7470

@@ -95,9 +91,10 @@ FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config)
9591
FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset /* = Vec2::ZERO */)
9692
{
9793
auto realFontFilename = FileUtils::getInstance()->getNewFilename(fontFileName); // resolves real file path, to prevent storing multiple atlases for the same file.
98-
char tmp[ATLAS_MAP_KEY_BUFFER];
99-
snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%.2f %.2f %s", imageOffset.x, imageOffset.y, realFontFilename.c_str());
100-
std::string atlasName = tmp;
94+
char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
95+
snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%.2f %.2f ", imageOffset.x, imageOffset.y);
96+
std::string atlasName(keyPrefix);
97+
atlasName += realFontFilename;
10198

10299
auto it = _atlasMap.find(atlasName);
103100
if ( it == _atlasMap.end() )
@@ -147,9 +144,9 @@ FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile)
147144

148145
FontAtlas* FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
149146
{
150-
char tmp[30];
151-
sprintf(tmp,"name:%u_%d_%d_%d",texture->getName(),itemWidth,itemHeight,startCharMap);
152-
std::string atlasName = tmp;
147+
char key[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
148+
sprintf(key,"name:%u_%d_%d_%d",texture->getName(),itemWidth,itemHeight,startCharMap);
149+
std::string atlasName = key;
153150

154151
auto it = _atlasMap.find(atlasName);
155152
if ( it == _atlasMap.end() )
@@ -174,9 +171,10 @@ FontAtlas* FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth
174171

175172
FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
176173
{
177-
char tmp[ATLAS_MAP_KEY_BUFFER];
178-
snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%d %d %d %s", itemWidth, itemHeight, startCharMap, charMapFile.c_str());
179-
std::string atlasName = tmp;
174+
char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
175+
snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%d %d %d ", itemWidth, itemHeight, startCharMap);
176+
std::string atlasName(keyPrefix);
177+
atlasName += charMapFile;
180178

181179
auto it = _atlasMap.find(atlasName);
182180
if ( it == _atlasMap.end() )
@@ -224,9 +222,10 @@ bool FontAtlasCache::releaseFontAtlas(FontAtlas *atlas)
224222

225223
void FontAtlasCache::reloadFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset/* = Vec2::ZERO*/)
226224
{
227-
char tmp[ATLAS_MAP_KEY_BUFFER];
228-
snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%.2f %.2f %s", imageOffset.x, imageOffset.y, fontFileName.c_str());
229-
std::string atlasName = tmp;
225+
char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
226+
snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%.2f %.2f ", imageOffset.x, imageOffset.y);
227+
std::string atlasName(keyPrefix);
228+
atlasName += fontFileName;
230229

231230
auto it = _atlasMap.find(atlasName);
232231
if (it != _atlasMap.end())

0 commit comments

Comments
 (0)