From b42fa9d0a98d18ce2958fd9c5bbeabb7893a113d Mon Sep 17 00:00:00 2001 From: LeonShu Date: Mon, 19 Aug 2024 10:08:28 +0800 Subject: [PATCH 1/4] codex for string to int Please enter the commit message for your changes. Lines starting --- services/ui/view/view_api.cpp | 8 +++++++- utils/include/utils.h | 11 ++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/services/ui/view/view_api.cpp b/services/ui/view/view_api.cpp index c8bf1d48..62e6783c 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 > colorMaxSize) { + LOG(ERROR) << "String2Int error, reset = " << reset; + return colorMaxSize; + } + uint8_t ret = (startPos > hexColor.size()) ? 0 : static_case(reset); startPos += width; return ret; }; diff --git a/utils/include/utils.h b/utils/include/utils.h index 018d8d08..3f6e0f82 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 shoule 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); -- Gitee From 47e354a9d2ebd48dc1684976c06e63cd47fafdad Mon Sep 17 00:00:00 2001 From: LeonShu Date: Mon, 19 Aug 2024 02:20:42 +0000 Subject: [PATCH 2/4] update services/ui/view/view_api.cpp. Signed-off-by: LeonShu --- services/ui/view/view_api.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/ui/view/view_api.cpp b/services/ui/view/view_api.cpp index 62e6783c..b7596fc8 100644 --- a/services/ui/view/view_api.cpp +++ b/services/ui/view/view_api.cpp @@ -78,7 +78,7 @@ OHOS::ColorType StrToColor(const std::string &hexColor) LOG(ERROR) << "String2Int error, reset = " << reset; return colorMaxSize; } - uint8_t ret = (startPos > hexColor.size()) ? 0 : static_case(reset); + uint8_t ret = (startPos > hexColor.size()) ? 0 : static_cast(reset); startPos += width; return ret; }; -- Gitee From d0afbfd52d075b16fab991884138455ae4115321 Mon Sep 17 00:00:00 2001 From: LeonShu Date: Mon, 19 Aug 2024 02:22:53 +0000 Subject: [PATCH 3/4] update utils/include/utils.h. Signed-off-by: LeonShu --- utils/include/utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/include/utils.h b/utils/include/utils.h index 3f6e0f82..73c656f7 100644 --- a/utils/include/utils.h +++ b/utils/include/utils.h @@ -41,7 +41,7 @@ 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 shoule be int or size_t or unsigned long long int"); + "type should be int or size_t or unsigned long long int"); char *end = nullptr; if (str.empty()) { errno = EINVAL; -- Gitee From 200a8b159c706f830188b17b4a40539a49db379f Mon Sep 17 00:00:00 2001 From: LeonShu Date: Mon, 19 Aug 2024 03:42:39 +0000 Subject: [PATCH 4/4] update services/ui/view/view_api.cpp. Signed-off-by: LeonShu --- services/ui/view/view_api.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/ui/view/view_api.cpp b/services/ui/view/view_api.cpp index b7596fc8..fb870502 100644 --- a/services/ui/view/view_api.cpp +++ b/services/ui/view/view_api.cpp @@ -74,7 +74,7 @@ OHOS::ColorType StrToColor(const std::string &hexColor) constexpr std::size_t width = 2uL; constexpr uint8_t colorMaxSize = 255; int reset = Utils::String2Int(hexColor.substr(startPos, width)); - if (reset < 0 || reset > colorMaxSize) { + if (reset < 0 || reset > static_cast(colorMaxSize)) { LOG(ERROR) << "String2Int error, reset = " << reset; return colorMaxSize; } -- Gitee