diff --git a/include/resource_data.h b/include/resource_data.h index 28fdce9..ecd056d 100644 --- a/include/resource_data.h +++ b/include/resource_data.h @@ -59,6 +59,7 @@ const static int8_t INVALID_ID = -1; const static int MIN_SUPPORT_NEW_MODULE_API_VERSION = 20; const static int MIN_SUPPORT_TS_HEADER_API_VERSION = 23; const static int API_VERSION_DIVISOR = 1000; +const static int HEX_BASE = 16; enum class IgnoreType { IGNORE_FILE, diff --git a/include/resource_util.h b/include/resource_util.h index 2873406..e7ace19 100644 --- a/include/resource_util.h +++ b/include/resource_util.h @@ -299,6 +299,24 @@ public: */ static bool IsHarResource(int64_t id); + /** + * @brief Convert string to int + * @param str: input string + * @param value: output int value + * @param base: conversion base, default 10 + * @return true if success, other false + */ + static bool StrToInt(const std::string &str, int &value, int base = 10); + + /** + * @brief Convert string to long long + * @param str: input string + * @param value: output long long value + * @param base: conversion base, default 10 + * @return true if success, other false + */ + static bool StrToLongLong(const std::string &str, long long &value, int base = 10); + private: static const std::map DEFAULT_IGNORE_FILE_REGEX; static std::string GetLocaleLimitkey(const KeyParam &KeyParam); diff --git a/src/config_parser.cpp b/src/config_parser.cpp index 9ab4e76..57e1ed4 100644 --- a/src/config_parser.cpp +++ b/src/config_parser.cpp @@ -398,7 +398,11 @@ bool ConfigParser::ParseJsonArrayRef(cJSON *parent, const string &key, cJSON *no return false; } if (update) { - cJSON_AddItemToArray(array, cJSON_CreateNumber(atoll(value.c_str()))); + long long idValue = 0; + if (!ResourceUtil::StrToLongLong(value, idValue)) { + return false; + } + cJSON_AddItemToArray(array, cJSON_CreateNumber(idValue)); } } cJSON_AddItemToObject(parent, (key + "Id").c_str(), array); @@ -421,8 +425,12 @@ bool ConfigParser::ParseJsonStringRef(cJSON *parent, const string &key, cJSON *n return false; } if (update) { - cJSON_AddItemToObject(parent, (key + "Id").c_str(), cJSON_CreateNumber(atoll(value.c_str()))); - AddCheckNode(key, static_cast(atoll(value.c_str()))); + long long idValue = 0; + if (!ResourceUtil::StrToLongLong(value, idValue)) { + return false; + } + cJSON_AddItemToObject(parent, (key + "Id").c_str(), cJSON_CreateNumber(idValue)); + AddCheckNode(key, static_cast(idValue)); } return true; } diff --git a/src/json_compiler.cpp b/src/json_compiler.cpp index 1e9accd..35d2c1a 100644 --- a/src/json_compiler.cpp +++ b/src/json_compiler.cpp @@ -416,9 +416,9 @@ bool JsonCompiler::CheckJsonSymbolValue(const cJSON *valueNode, const ResourceIt if (regex_match(unicodeStr, regex("^\\$(ohos:)?symbol:.*"))) { return true; } - int unicode = strtol(unicodeStr.c_str(), nullptr, 16); - if (!ResourceUtil::isUnicodeInPlane15or16(unicode)) { - PrintError(GetError(ERR_CODE_INVALID_SYMBOL).FormatCause(unicode, resourceItem.GetName().c_str()) + int unicode = 0; + if (!ResourceUtil::StrToInt(unicodeStr, unicode, HEX_BASE) || !ResourceUtil::isUnicodeInPlane15or16(unicode)) { + PrintError(GetError(ERR_CODE_INVALID_SYMBOL).FormatCause(unicodeStr.c_str(), resourceItem.GetName().c_str()) .SetPosition(resourceItem.GetFilePath())); return false; } diff --git a/src/resource_util.cpp b/src/resource_util.cpp index adf12bb..8123ed8 100644 --- a/src/resource_util.cpp +++ b/src/resource_util.cpp @@ -15,6 +15,7 @@ #include "resource_util.h" #include +#include #include #include #include @@ -581,6 +582,42 @@ bool ResourceUtil::IsHarResource(int64_t id) std::lock_guard lock(g_harResourceMutex); return g_harResourceIds.count(id); } + +bool ResourceUtil::StrToInt(const string &str, int &value, int base) +{ + if (str.empty()) { + return false; + } + char *end = nullptr; + errno = 0; + long result = strtol(str.c_str(), &end, base); + if (end == str.c_str() || *end != '\0') { + return false; + } + if (errno == ERANGE || result < INT_MIN || result > INT_MAX) { + return false; + } + value = static_cast(result); + return true; +} + +bool ResourceUtil::StrToLongLong(const string &str, long long &value, int base) +{ + if (str.empty()) { + return false; + } + char *end = nullptr; + errno = 0; + long long result = strtoll(str.c_str(), &end, base); + if (end == str.c_str() || *end != '\0') { + return false; + } + if (errno == ERANGE || result == LLONG_MAX || result == LLONG_MIN) { + return false; + } + value = result; + return true; +} } } } diff --git a/src/restool_errors.cpp b/src/restool_errors.cpp index 2e4cc25..83e86ec 100644 --- a/src/restool_errors.cpp +++ b/src/restool_errors.cpp @@ -476,7 +476,7 @@ const std::map ERRORS_MAP = { { ERR_CODE_INVALID_SYMBOL, { ERR_CODE_INVALID_SYMBOL, ERR_TYPE_RESOURCE_PACK, - "Invalid value '%d' of the symbol resource '%s'. It should be in the scope [0xF0000,0xFFFFF] or " + "Invalid value '%s' of the symbol resource '%s'. It should be in the scope [0xF0000,0xFFFFF] or " "[0x100000,0x10FFFF]." "", {},