From 6c8396bd90e6e9872c2efe794f9e01542a725166 Mon Sep 17 00:00:00 2001 From: sbrednikhin Date: Mon, 4 Feb 2019 14:13:37 +0200 Subject: [PATCH 1/4] 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. --- cocos/2d/CCFontAtlasCache.cpp | 49 +++++++++++++++++------------------ 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/cocos/2d/CCFontAtlasCache.cpp b/cocos/2d/CCFontAtlasCache.cpp index 8913ec734b05..b6b59db5b31f 100644 --- a/cocos/2d/CCFontAtlasCache.cpp +++ b/cocos/2d/CCFontAtlasCache.cpp @@ -36,7 +36,7 @@ NS_CC_BEGIN std::unordered_map FontAtlasCache::_atlasMap; -#define ATLAS_MAP_KEY_BUFFER 255 +#define ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE 255 void FontAtlasCache::purgeCachedData() { @@ -60,15 +60,11 @@ FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config) useDistanceField = false; } - char tmp[ATLAS_MAP_KEY_BUFFER]; - if (useDistanceField) { - snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "df %.2f %d %s", config->fontSize, config->outlineSize, - realFontFilename.c_str()); - } else { - snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%.2f %d %s", config->fontSize, config->outlineSize, - realFontFilename.c_str()); - } - std::string atlasName = tmp; + std::string key; + char key_prefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE]; + snprintf(key_prefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, useDistanceField ? "df %.2f %d " : "%.2f %d ", config->fontSize, config->outlineSize); + std::string atlasName(key_prefix); + atlasName += realFontFilename; auto it = _atlasMap.find(atlasName); @@ -87,7 +83,7 @@ FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config) } } else - return it->second; + return _atlasMap[atlasName]; return nullptr; } @@ -95,9 +91,10 @@ FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config) FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset /* = Vec2::ZERO */) { auto realFontFilename = FileUtils::getInstance()->getNewFilename(fontFileName); // resolves real file path, to prevent storing multiple atlases for the same file. - char tmp[ATLAS_MAP_KEY_BUFFER]; - snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%.2f %.2f %s", imageOffset.x, imageOffset.y, realFontFilename.c_str()); - std::string atlasName = tmp; + char key_prefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE]; + snprintf(key_prefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%.2f %.2f ", imageOffset.x, imageOffset.y); + std::string atlasName(key_prefix); + atlasName += realFontFilename; auto it = _atlasMap.find(atlasName); if ( it == _atlasMap.end() ) @@ -115,7 +112,7 @@ FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, cons } } else - return it->second; + return _atlasMap[atlasName]; return nullptr; } @@ -140,14 +137,14 @@ FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile) } } else - return it->second; + return _atlasMap[atlasName]; return nullptr; } FontAtlas* FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap) { - char tmp[30]; + char tmp[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE]; sprintf(tmp,"name:%u_%d_%d_%d",texture->getName(),itemWidth,itemHeight,startCharMap); std::string atlasName = tmp; @@ -167,16 +164,17 @@ FontAtlas* FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth } } else - return it->second; + return _atlasMap[atlasName]; return nullptr; } FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap) { - char tmp[ATLAS_MAP_KEY_BUFFER]; - snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%d %d %d %s", itemWidth, itemHeight, startCharMap, charMapFile.c_str()); - std::string atlasName = tmp; + char key_prefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE]; + snprintf(key_prefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%d %d %d ", itemWidth, itemHeight, startCharMap); + std::string atlasName(key_prefix); + atlasName += charMapFile; auto it = _atlasMap.find(atlasName); if ( it == _atlasMap.end() ) @@ -194,7 +192,7 @@ FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& charMapFile, i } } else - return it->second; + return _atlasMap[atlasName]; return nullptr; } @@ -224,9 +222,10 @@ bool FontAtlasCache::releaseFontAtlas(FontAtlas *atlas) void FontAtlasCache::reloadFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset/* = Vec2::ZERO*/) { - char tmp[ATLAS_MAP_KEY_BUFFER]; - snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%.2f %.2f %s", imageOffset.x, imageOffset.y, fontFileName.c_str()); - std::string atlasName = tmp; + char key_prefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE]; + snprintf(key_prefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%.2f %.2f ", imageOffset.x, imageOffset.y); + std::string atlasName(key_prefix); + atlasName += fontFileName; auto it = _atlasMap.find(atlasName); if (it != _atlasMap.end()) From a52e72e4ac2be23996ef9f5f19489661dc91dced Mon Sep 17 00:00:00 2001 From: sbrednikhin Date: Tue, 5 Feb 2019 09:29:04 +0200 Subject: [PATCH 2/4] Incorrect replacement. Using iterator is better. --- cocos/2d/CCFontAtlasCache.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cocos/2d/CCFontAtlasCache.cpp b/cocos/2d/CCFontAtlasCache.cpp index b6b59db5b31f..23e55f9c9117 100644 --- a/cocos/2d/CCFontAtlasCache.cpp +++ b/cocos/2d/CCFontAtlasCache.cpp @@ -83,7 +83,7 @@ FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config) } } else - return _atlasMap[atlasName]; + return it->second; return nullptr; } @@ -112,7 +112,7 @@ FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, cons } } else - return _atlasMap[atlasName]; + return it->second; return nullptr; } @@ -137,7 +137,7 @@ FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile) } } else - return _atlasMap[atlasName]; + return it->second; return nullptr; } @@ -164,7 +164,7 @@ FontAtlas* FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth } } else - return _atlasMap[atlasName]; + return it->second; return nullptr; } @@ -192,7 +192,7 @@ FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& charMapFile, i } } else - return _atlasMap[atlasName]; + return it->second; return nullptr; } From 21b3d9aa29c0c39cc2fb7aa32263ce54de1e70f0 Mon Sep 17 00:00:00 2001 From: sbrednikhin Date: Wed, 13 Feb 2019 09:52:49 +0200 Subject: [PATCH 3/4] Style fix --- cocos/2d/CCFontAtlasCache.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/cocos/2d/CCFontAtlasCache.cpp b/cocos/2d/CCFontAtlasCache.cpp index 23e55f9c9117..3d09ee9eb01e 100644 --- a/cocos/2d/CCFontAtlasCache.cpp +++ b/cocos/2d/CCFontAtlasCache.cpp @@ -61,9 +61,9 @@ FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config) } std::string key; - char key_prefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE]; - snprintf(key_prefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, useDistanceField ? "df %.2f %d " : "%.2f %d ", config->fontSize, config->outlineSize); - std::string atlasName(key_prefix); + char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE]; + snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, useDistanceField ? "df %.2f %d " : "%.2f %d ", config->fontSize, config->outlineSize); + std::string atlasName(keyPrefix); atlasName += realFontFilename; auto it = _atlasMap.find(atlasName); @@ -91,9 +91,9 @@ FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config) FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset /* = Vec2::ZERO */) { auto realFontFilename = FileUtils::getInstance()->getNewFilename(fontFileName); // resolves real file path, to prevent storing multiple atlases for the same file. - char key_prefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE]; - snprintf(key_prefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%.2f %.2f ", imageOffset.x, imageOffset.y); - std::string atlasName(key_prefix); + char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE]; + snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%.2f %.2f ", imageOffset.x, imageOffset.y); + std::string atlasName(keyPrefix); atlasName += realFontFilename; auto it = _atlasMap.find(atlasName); @@ -171,9 +171,9 @@ FontAtlas* FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap) { - char key_prefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE]; - snprintf(key_prefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%d %d %d ", itemWidth, itemHeight, startCharMap); - std::string atlasName(key_prefix); + char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE]; + snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%d %d %d ", itemWidth, itemHeight, startCharMap); + std::string atlasName(keyPrefix); atlasName += charMapFile; auto it = _atlasMap.find(atlasName); @@ -222,9 +222,9 @@ bool FontAtlasCache::releaseFontAtlas(FontAtlas *atlas) void FontAtlasCache::reloadFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset/* = Vec2::ZERO*/) { - char key_prefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE]; - snprintf(key_prefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%.2f %.2f ", imageOffset.x, imageOffset.y); - std::string atlasName(key_prefix); + char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE]; + snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%.2f %.2f ", imageOffset.x, imageOffset.y); + std::string atlasName(keyPrefix); atlasName += fontFileName; auto it = _atlasMap.find(atlasName); From 046c11db2309449d7e069ddcf11b2f78fa2d6d64 Mon Sep 17 00:00:00 2001 From: sbrednikhin Date: Wed, 13 Feb 2019 09:59:09 +0200 Subject: [PATCH 4/4] Correct naming --- cocos/2d/CCFontAtlasCache.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cocos/2d/CCFontAtlasCache.cpp b/cocos/2d/CCFontAtlasCache.cpp index 3d09ee9eb01e..1c7128d01c76 100644 --- a/cocos/2d/CCFontAtlasCache.cpp +++ b/cocos/2d/CCFontAtlasCache.cpp @@ -144,9 +144,9 @@ FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile) FontAtlas* FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap) { - char tmp[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE]; - sprintf(tmp,"name:%u_%d_%d_%d",texture->getName(),itemWidth,itemHeight,startCharMap); - std::string atlasName = tmp; + char key[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE]; + sprintf(key,"name:%u_%d_%d_%d",texture->getName(),itemWidth,itemHeight,startCharMap); + std::string atlasName = key; auto it = _atlasMap.find(atlasName); if ( it == _atlasMap.end() )