diff --git a/include/utilities.h b/include/utilities.h index be195b88e795bf3b495187b7f6cd21951e3f1523..a59584c842589fcfe1e7c824a58492320e5df949 100644 --- a/include/utilities.h +++ b/include/utilities.h @@ -340,6 +340,7 @@ bool PowerOfTwo(uint64_t n); bool IsNumeric(const std::string& str); bool IscontainDigits(const std::string& str); bool IsStringToIntSuccess(const std::string &str, int &num); +bool StringToUint64(const std::string &str, uint64_t &val); bool IsDirectoryExists(const std::string& fileName); bool CreateDirectory(const std::string& path, mode_t mode); bool IsValidOutPath(const std::string& path); @@ -385,7 +386,7 @@ void CollectPidsByAppname(std::set &pids, const Container& appPackage) } } -bool CheckAppIsRunning (std::vector &selectPids, const std::string &appPackage, int checkAppMs); +bool CheckAppIsRunning(std::vector &selectPids, const std::string &appPackage, int checkAppMs); bool IsExistDebugByApp(const std::string& bundleName, std::string& err); bool IsExistDebugByPid(const std::vector &pids, std::string& err); bool IsSupportNonDebuggableApp(); diff --git a/src/subcommand_record.cpp b/src/subcommand_record.cpp index 3f52413e3e2cb6cf8008b0681d34a2f0936aea05..367d1acc80419e8d29353f1f2de0ba5b5954289c 100644 --- a/src/subcommand_record.cpp +++ b/src/subcommand_record.cpp @@ -1696,8 +1696,18 @@ uint32_t SubCommandRecord::GetCountFromFile(const std::string &fileName) ret++; std::vector vSubstr = StringSplit(subStr, "-"); static const size_t BEGIN_END = 2; - if (vSubstr.size() == BEGIN_END) { - ret += static_cast((std::stoi(vSubstr[1]) - std::stoi(vSubstr[0]))); + if (vSubstr.size() != BEGIN_END) { + continue; + } + int num1 = 0; + int num2 = 0; + if (vSubstr[1].size() >= 2 && StringEndsWith(vSubstr[1], "\n")) { // 2: string size + vSubstr[1].resize(vSubstr[1].size() - 1); + } + bool ret1 = IsStringToIntSuccess(vSubstr[1], num1); + bool ret2 = IsStringToIntSuccess(vSubstr[0], num2); + if (ret1 && ret2) { + ret += static_cast(num1 - num2); } } return ret; @@ -1767,7 +1777,11 @@ void SubCommandRecord::AddMemTotalFeature() } if ((it + 1) != subStrs.end()) { - uint64_t memTotal = std::stoul(*(it + 1)); + uint64_t memTotal = 0; + if (!StringToUint64(*(it + 1), memTotal)) { + HIPERF_HILOGE(MODULE_DEFAULT, "get uint64_t failed, paramValue: %{pubilc}s", (*(it + 1)).c_str()); + continue; + } fileWriter_->AddU64Feature(FEATURE::TOTAL_MEM, memTotal); } break; diff --git a/src/utilities.cpp b/src/utilities.cpp index f0623bdc7d57ff8efd96c6fb1c17d3440e320120..a08fd3de69d4393c392105fd47c2baf153a2f871 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -369,6 +369,19 @@ bool IsStringToIntSuccess(const std::string &str, int &val) return true; } +bool StringToUint64(const std::string &str, uint64_t &val) +{ + char *endPtr = nullptr; + errno = 0; + uint64_t num = std::strtoull(str.c_str(), &endPtr, 10); // 10 : decimal scale + if (endPtr == str.c_str() || *endPtr != '\0' || errno != 0 || num > ULLONG_MAX || str.c_str()[0] == '-') { + HIPERF_HILOGE(MODULE_DEFAULT, "get uint64 failed, str: %{public}s", str.c_str()); + return false; + } + val = num; + return true; +} + bool ReadIntFromProcFile(const std::string &path, int &value) { std::string s = ReadFileToString(path); @@ -376,7 +389,12 @@ bool ReadIntFromProcFile(const std::string &path, int &value) if (!IscontainDigits(s)) { return false; } - value = std::stoi(s); + if (s.size() >= 2 && StringEndsWith(s, "\n")) { // 2: string size + s.resize(s.size() - 1); + } + if (!IsStringToIntSuccess(s, value)) { + return false; + } return true; } @@ -542,7 +560,11 @@ std::vector GetSubthreadIDs(const pid_t pid) std::vector res {}; for (auto tidStr : tids) { if (!tidStr.empty()) { - pid_t tid = static_cast(std::stoul(tidStr, nullptr)); + uint64_t dest = 0; + if (!StringToUint64(tidStr, dest)) { + continue; + } + pid_t tid = static_cast(dest); if (tid == pid) { continue; } @@ -561,7 +583,11 @@ std::vector GetSubthreadIDs(const pid_t pid, std::map std::vector res{}; for (auto tidStr : tids) { ThreadInfos info; - pid_t tid = static_cast(std::stoul(tidStr, nullptr)); + uint64_t dest = 0; + if (!StringToUint64(tidStr, dest)) { + continue; + } + pid_t tid = static_cast(dest); info.tid = tid; info.pid = pid; thread_map[tid] = info; @@ -673,7 +699,7 @@ pid_t GetAppPackagePid(const std::string &appPackage, const pid_t oldPid, const return res; } -bool CheckAppIsRunning (std::vector &selectPids, const std::string &appPackage, int checkAppMs) +bool CheckAppIsRunning(std::vector &selectPids, const std::string &appPackage, int checkAppMs) { if (!appPackage.empty()) { pid_t appPid = GetAppPackagePid(appPackage, -1, checkAppMs, waitAppRunCheckTimeOut); diff --git a/src/virtual_runtime.cpp b/src/virtual_runtime.cpp index 14f43f45a10737d8f3133a5a8d203956938803bc..e2be7139785d1cc7697687599d11ff79c50b9c34 100644 --- a/src/virtual_runtime.cpp +++ b/src/virtual_runtime.cpp @@ -105,7 +105,7 @@ VirtualThread &VirtualRuntime::UpdateThread(pid_t pid, pid_t tid, const std::str const auto startTime = steady_clock::now(); #endif VirtualThread &thread = GetThread(pid, tid, name); - if (!name.empty() && thread.name_.empty()) { + if (!name.empty() && (thread.name_.empty() || !StringEndsWith(thread.name_, name))) { thread.name_ = name; } #ifdef HIPERF_DEBUG_TIME @@ -252,7 +252,7 @@ void VirtualRuntime::UpdateKernelModulesSpaceMaps() if (lineSize > 4096) { // 4096: line length continue; } - char *module = new char[lineSize]; + char *module = new char[lineSize + 1]; /* name size load map hi_mipi_rx 53248 0 - Live 0xbf109000 (O) diff --git a/src/virtual_thread.cpp b/src/virtual_thread.cpp index 78dd072db977a28812bee5bc4559c36f67f0f954..b8ae3abbc36fcab4d00b336271762c6f7db39b43 100644 --- a/src/virtual_thread.cpp +++ b/src/virtual_thread.cpp @@ -293,7 +293,11 @@ void VirtualThread::ParseServiceMap(const std::string &filename) if (mapTokens.size() == MMAP_LINE_MAX_TOKEN && mapTokens[MMAP_LINE_TOKEN_INDEX_NAME] == name_) { HLOGM("map line: %s", line.c_str()); + constexpr int mmapAddrRangeToken = 2; std::vector addrRanges = StringSplit(mapTokens[0], "-"); + if (addrRanges.size() < mmapAddrRangeToken) { + continue; + } begin = std::stoull(addrRanges[0], nullptr, NUMBER_FORMAT_HEX_BASE); end = std::stoull(addrRanges[1], nullptr, NUMBER_FORMAT_HEX_BASE); break; diff --git a/test/unittest/common/native/utilities_test.cpp b/test/unittest/common/native/utilities_test.cpp index 0d20993012869a4e392f9b24ac0a2e6438d76b26..5a69b86b0731090208b996cfead3604553d9ab49 100644 --- a/test/unittest/common/native/utilities_test.cpp +++ b/test/unittest/common/native/utilities_test.cpp @@ -894,6 +894,48 @@ HWTEST_F(UtilitiesTest, IsValidOutPathErr, TestSize.Level2) std::string file = "/data/log/hiperflog/perf.data"; EXPECT_FALSE(IsValidOutPath(file)); } + +/** + * @tc.name: StringToIntTest + * @tc.desc: Test StringToUint64 function. + * @tc.type: FUNC +*/ +HWTEST_F(UtilitiesTest, StringToIntTest, TestSize.Level2) +{ + std::string traceParamsStr = "1234567890"; + uint64_t paramsUint64 = 0; + EXPECT_TRUE(StringToUint64(traceParamsStr, paramsUint64)); + EXPECT_EQ(paramsUint64, 1234567890); // 1234567890: test value + traceParamsStr = "18446744073709551615"; + EXPECT_TRUE(StringToUint64(traceParamsStr, paramsUint64)); + EXPECT_EQ(paramsUint64, ULLONG_MAX); +} + +/** + * @tc.name: StringToUint64ErrorTest + * @tc.desc: Test StringToUint64 function. + * @tc.type: FUNC +*/ +HWTEST_F(UtilitiesTest, StringToUint64ErrorTest, TestSize.Level2) +{ + std::string traceParamsStr = "-1234567890"; + uint64_t paramsUint64 = 0; + EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64)); + traceParamsStr = "a123"; + EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64)); + traceParamsStr = ""; + EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64)); + traceParamsStr = "12a3"; + EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64)); + traceParamsStr = "abc"; + EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64)); + traceParamsStr = ".1"; + EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64)); + traceParamsStr = "1.1"; + EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64)); + traceParamsStr = "18446744073709551616"; + EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64)); +} } // namespace HiPerf } // namespace Developtools } // namespace OHOS