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
This commit is contained in:
minggo 2019-04-10 18:31:50 -07:00 committed by GitHub
parent 0a9e66a16c
commit 1f7c0c9408
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 22 deletions

View File

@ -36,7 +36,7 @@
NS_CC_BEGIN NS_CC_BEGIN
std::unordered_map<std::string, FontAtlas *> FontAtlasCache::_atlasMap; std::unordered_map<std::string, FontAtlas *> FontAtlasCache::_atlasMap;
#define ATLAS_MAP_KEY_BUFFER 255 #define ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE 255
void FontAtlasCache::purgeCachedData() void FontAtlasCache::purgeCachedData()
{ {
@ -60,15 +60,11 @@ FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config)
useDistanceField = false; useDistanceField = false;
} }
char tmp[ATLAS_MAP_KEY_BUFFER]; std::string key;
if (useDistanceField) { char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "df %.2f %d %s", config->fontSize, config->outlineSize, snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, useDistanceField ? "df %.2f %d " : "%.2f %d ", config->fontSize, config->outlineSize);
realFontFilename.c_str()); std::string atlasName(keyPrefix);
} else { atlasName += realFontFilename;
snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%.2f %d %s", config->fontSize, config->outlineSize,
realFontFilename.c_str());
}
std::string atlasName = tmp;
auto it = _atlasMap.find(atlasName); auto it = _atlasMap.find(atlasName);
@ -95,9 +91,10 @@ FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config)
FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset /* = Vec2::ZERO */) 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. 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]; char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%.2f %.2f %s", imageOffset.x, imageOffset.y, realFontFilename.c_str()); snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%.2f %.2f ", imageOffset.x, imageOffset.y);
std::string atlasName = tmp; std::string atlasName(keyPrefix);
atlasName += realFontFilename;
auto it = _atlasMap.find(atlasName); auto it = _atlasMap.find(atlasName);
if ( it == _atlasMap.end() ) if ( it == _atlasMap.end() )
@ -147,9 +144,9 @@ FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile)
FontAtlas* FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap) FontAtlas* FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
{ {
char tmp[30]; char key[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
sprintf(tmp,"name:%u_%d_%d_%d",texture->getName(),itemWidth,itemHeight,startCharMap); sprintf(key,"name:%u_%d_%d_%d",texture->getName(),itemWidth,itemHeight,startCharMap);
std::string atlasName = tmp; std::string atlasName = key;
auto it = _atlasMap.find(atlasName); auto it = _atlasMap.find(atlasName);
if ( it == _atlasMap.end() ) if ( it == _atlasMap.end() )
@ -174,9 +171,10 @@ FontAtlas* FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth
FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap) FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
{ {
char tmp[ATLAS_MAP_KEY_BUFFER]; char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%d %d %d %s", itemWidth, itemHeight, startCharMap, charMapFile.c_str()); snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%d %d %d ", itemWidth, itemHeight, startCharMap);
std::string atlasName = tmp; std::string atlasName(keyPrefix);
atlasName += charMapFile;
auto it = _atlasMap.find(atlasName); auto it = _atlasMap.find(atlasName);
if ( it == _atlasMap.end() ) if ( it == _atlasMap.end() )
@ -224,9 +222,10 @@ bool FontAtlasCache::releaseFontAtlas(FontAtlas *atlas)
void FontAtlasCache::reloadFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset/* = Vec2::ZERO*/) void FontAtlasCache::reloadFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset/* = Vec2::ZERO*/)
{ {
char tmp[ATLAS_MAP_KEY_BUFFER]; char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%.2f %.2f %s", imageOffset.x, imageOffset.y, fontFileName.c_str()); snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%.2f %.2f ", imageOffset.x, imageOffset.y);
std::string atlasName = tmp; std::string atlasName(keyPrefix);
atlasName += fontFileName;
auto it = _atlasMap.find(atlasName); auto it = _atlasMap.find(atlasName);
if (it != _atlasMap.end()) if (it != _atlasMap.end())