diff --git a/services/ui/view/view_api.cpp b/services/ui/view/view_api.cpp index c8bf1d481e59be13432e20958344a1fde28fa01a..fb870502480b22618e0b4899009e3b4d723bddd3 100644 --- a/services/ui/view/view_api.cpp +++ b/services/ui/view/view_api.cpp @@ -72,7 +72,13 @@ OHOS::ColorType StrToColor(const std::string &hexColor) std::size_t startPos = 1uL; auto getNextField = [&startPos, &hexColor] () { constexpr std::size_t width = 2uL; - uint8_t ret = (startPos > hexColor.size()) ? 0 : Utils::String2Int(hexColor.substr(startPos, width)); + constexpr uint8_t colorMaxSize = 255; + int reset = Utils::String2Int(hexColor.substr(startPos, width)); + if (reset < 0 || reset > static_cast(colorMaxSize)) { + LOG(ERROR) << "String2Int error, reset = " << reset; + return colorMaxSize; + } + uint8_t ret = (startPos > hexColor.size()) ? 0 : static_cast(reset); startPos += width; return ret; }; diff --git a/utils/include/utils.h b/utils/include/utils.h index 018d8d08e520570f5304a124fb7e736c2803726b..73c656f7903a87192c332d109e8534581fc3c839 100644 --- a/utils/include/utils.h +++ b/utils/include/utils.h @@ -40,6 +40,8 @@ constexpr const char* ON_SERVER = "ON_SERVER"; template T String2Int(const std::string &str, int base = N_HEX) { + static_assert(std::is_same_v || std::is_same_v || std::is_same_v, + "type should be int or size_t or unsigned long long int"); char *end = nullptr; if (str.empty()) { errno = EINVAL; @@ -48,7 +50,14 @@ T String2Int(const std::string &str, int base = N_HEX) if (((str[0] == '0') && (str[1] == 'x')) || (str[1] == 'X')) { base = N_HEX; } - T result = strtoull(str.c_str(), &end, base); + T result = 0; + if constexpr (std::is_same_v) { + result = strtol(str.c_str(), &end, base); + } else if constexpr (std::is_same_v || std::is_same_v) { + result = strtoull(str.c_str(), &end, base); + } else { + errno = EINVAL; + } return result; } int32_t DeleteFile(const std::string& filename);