From 79ee9f8c8593a76c025938f6b4a8c9b53dc8bd76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E6=98=8C=E6=99=B6?= Date: Thu, 17 Apr 2025 15:22:07 +0800 Subject: [PATCH] fix hilog domainId 0x0 --- frameworks/libhilog/utils/include/log_utils.h | 4 +-- frameworks/libhilog/utils/log_utils.cpp | 17 +++++++------ interfaces/native/innerkits/libhilog.map | 4 +-- services/hilogtool/main.cpp | 12 ++++++--- test/unittest/common/hilog_utils_test.cpp | 25 +++++++++++++++++-- 5 files changed, 46 insertions(+), 16 deletions(-) diff --git a/frameworks/libhilog/utils/include/log_utils.h b/frameworks/libhilog/utils/include/log_utils.h index 5b0ad29..0326015 100644 --- a/frameworks/libhilog/utils/include/log_utils.h +++ b/frameworks/libhilog/utils/include/log_utils.h @@ -93,9 +93,9 @@ uint32_t GetBitsCount(uint64_t n); uint16_t GetBitPos(uint64_t n); std::string Uint2DecStr(uint32_t i); -uint32_t DecStr2Uint(const std::string& str); +uint32_t DecStr2Uint(const std::string& str, bool& success); std::string Uint2HexStr(uint32_t i); -uint32_t HexStr2Uint(const std::string& str); +uint32_t HexStr2Uint(const std::string& str, bool& success); #if !defined(__WINDOWS__) and !defined(__LINUX__) std::string GetProgName(); diff --git a/frameworks/libhilog/utils/log_utils.cpp b/frameworks/libhilog/utils/log_utils.cpp index 0e3d8d1..a68ed40 100644 --- a/frameworks/libhilog/utils/log_utils.cpp +++ b/frameworks/libhilog/utils/log_utils.cpp @@ -371,7 +371,7 @@ static string Num2Str(T num, Radix radix) } template -static void Str2Num(const string& str, T& num, Radix radix) +static bool Str2Num(const string& str, T& num, Radix radix) { T i = 0; std::stringstream ss; @@ -380,9 +380,12 @@ static void Str2Num(const string& str, T& num, Radix radix) r = std::hex; } ss << r << str; - ss >> i; + bool success = false; + if (ss >> i) { + success = true; + } num = i; - return; + return success; } string Uint2DecStr(uint32_t i) @@ -390,10 +393,10 @@ string Uint2DecStr(uint32_t i) return Num2Str(i, Radix::RADIX_DEC); } -uint32_t DecStr2Uint(const string& str) +uint32_t DecStr2Uint(const string& str, bool& success) { uint32_t i = 0; - Str2Num(str, i, Radix::RADIX_DEC); + success = Str2Num(str, i, Radix::RADIX_DEC); return i; } @@ -402,10 +405,10 @@ string Uint2HexStr(uint32_t i) return Num2Str(i, Radix::RADIX_HEX); } -uint32_t HexStr2Uint(const string& str) +uint32_t HexStr2Uint(const string& str, bool& success) { uint32_t i = 0; - Str2Num(str, i, Radix::RADIX_HEX); + success = Str2Num(str, i, Radix::RADIX_HEX); return i; } diff --git a/interfaces/native/innerkits/libhilog.map b/interfaces/native/innerkits/libhilog.map index 4dce8bb..023acf7 100644 --- a/interfaces/native/innerkits/libhilog.map +++ b/interfaces/native/innerkits/libhilog.map @@ -54,7 +54,7 @@ "OHOS::HiviewDFX::SetTagLevel(std::__h::basic_string, std::__h::allocator> const&, unsigned short)"; "OHOS::HiviewDFX::SetPersistTagLevel(std::__h::basic_string, std::__h::allocator> const&, unsigned short)"; "OHOS::HiviewDFX::Split(std::__h::basic_string, std::__h::allocator> const&, std::__h::vector, std::__h::allocator>, std::__h::allocator, std::__h::allocator>>>&, std::__h::basic_string, std::__h::allocator> const&)"; - "OHOS::HiviewDFX::HexStr2Uint(std::__h::basic_string, std::__h::allocator> const&)"; + "OHOS::HiviewDFX::HexStr2Uint(std::__h::basic_string, std::__h::allocator> const&, bool&)"; "OHOS::HiviewDFX::ComboLogType2Str(unsigned short)"; "OHOS::HiviewDFX::Str2Size(std::__h::basic_string, std::__h::allocator> const&)"; "OHOS::HiviewDFX::Str2ComboLogLevel(std::__h::basic_string, std::__h::allocator> const&)"; @@ -104,7 +104,7 @@ "OHOS::HiviewDFX::GetBitPos(unsigned long long)"; "OHOS::HiviewDFX::GetBitPos(unsigned long)"; "OHOS::HiviewDFX::Uint2DecStr(unsigned int)"; - "OHOS::HiviewDFX::DecStr2Uint(std::__h::basic_string, std::__h::allocator> const&)"; + "OHOS::HiviewDFX::DecStr2Uint(std::__h::basic_string, std::__h::allocator> const&, bool&)"; }; local: diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index 63cd0c0..76d67b8 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -510,11 +510,17 @@ static int DomainHandler(HilogArgs& context, const char *arg) if (index >= MAX_DOMAINS) { return ERR_TOO_MANY_DOMAINS; } - uint32_t domain = HexStr2Uint(d); - if (domain == 0) { + bool success = false; + uint32_t domain = HexStr2Uint(d, success); + if (!success) { + return ERR_INVALID_DOMAIN_STR; + } + if (((domain >= DOMAIN_APP_MIN) && (domain <= DOMAIN_APP_MAX)) || + ((domain >= DOMAIN_OS_MIN) && (domain <= DOMAIN_OS_MAX))) { + context.domains[index++] = domain; + } else { return ERR_INVALID_DOMAIN_STR; } - context.domains[index++] = domain; } context.domainCount = index; return RET_SUCCESS; diff --git a/test/unittest/common/hilog_utils_test.cpp b/test/unittest/common/hilog_utils_test.cpp index 0c420df..49be836 100644 --- a/test/unittest/common/hilog_utils_test.cpp +++ b/test/unittest/common/hilog_utils_test.cpp @@ -191,10 +191,13 @@ HWTEST_F(HilogUtilsTest, HilogUtilsTest_006, TestSize.Level1) uint32_t hexNum = 0xd002d00; std::string decStr = "1250"; std::string hexStr = "d002d00"; + bool success = false; EXPECT_EQ(Uint2DecStr(decNum), decStr); - EXPECT_EQ(DecStr2Uint(decStr), decNum); + EXPECT_EQ(DecStr2Uint(decStr, success), decNum); + EXPECT_TRUE(success); EXPECT_EQ(Uint2HexStr(hexNum), hexStr); - EXPECT_EQ(HexStr2Uint(hexStr), hexNum); + EXPECT_EQ(HexStr2Uint(hexStr, success), hexNum); + EXPECT_TRUE(success); } /** @@ -251,4 +254,22 @@ HWTEST_F(HilogUtilsTest, HilogUtilsTest_009, TestSize.Level1) PrintErrorno(errno); EXPECT_EQ(ret, RET_FAIL); } + +/** + * @tc.name: Dfx_HilogUtilsTest_HilogUtilsTest_010 + * @tc.desc: HexStr2Uint. + * @tc.type: FUNC + */ +HWTEST_F(HilogUtilsTest, HilogUtilsTest_010, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HilogUtilsTest_010: start."; + uint32_t hexNum = 0x0; + std::string hexStr = "0x0"; + bool success = false; + EXPECT_EQ(HexStr2Uint(hexStr, success), hexNum); + EXPECT_TRUE(success); + std::string str = "zzz"; + EXPECT_EQ(HexStr2Uint(str, success), hexNum); + EXPECT_FALSE(success); +} } // namespace -- Gitee