From bb4cba21a237ad19f0b213d5345df196b587743d Mon Sep 17 00:00:00 2001 From: leiguangyu Date: Wed, 11 Dec 2024 11:39:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8B=E6=A0=87=E8=B6=8A?= =?UTF-8?q?=E7=95=8C=E5=92=8C=E5=86=85=E5=AD=98=E5=BC=82=E5=B8=B8=E5=88=86?= =?UTF-8?q?=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Icb255c48b959baa4069bba4086afc827053be98f Signed-off-by: leiguangyu --- include/report.h | 7 ++++--- src/perf_file_reader.cpp | 1 + .../common/native/perf_file_reader_test.cpp | 11 +++++++++++ test/unittest/common/native/report_test.cpp | 17 +++++++++++++++++ .../common/native/subcommand_stat_test.cpp | 11 ++++++----- 5 files changed, 39 insertions(+), 8 deletions(-) diff --git a/include/report.h b/include/report.h index c23779e..e1a9644 100644 --- a/include/report.h +++ b/include/report.h @@ -506,12 +506,13 @@ public: std::map configIdIndexMaps_; // index of configNames_ std::string GetConfigName(uint64_t id) { - return configs_[GetConfigIndex(id)].eventName_; + size_t index = GetConfigIndex(id); + HIPERF_ASSERT(index < configs_.size(), "unable found config index %zu\n", index); + return configs_[index].eventName_; } size_t GetConfigIndex(uint64_t id) { - HLOG_ASSERT_MESSAGE(configIdIndexMaps_.find(id) != configIdIndexMaps_.end(), - "unable found id %" PRIx64 "", id); + HIPERF_ASSERT(configIdIndexMaps_.find(id) != configIdIndexMaps_.end(), "unable found id %" PRIx64 "\n", id); return configIdIndexMaps_.at(id); } diff --git a/src/perf_file_reader.cpp b/src/perf_file_reader.cpp index 7ad4bf3..9631965 100644 --- a/src/perf_file_reader.cpp +++ b/src/perf_file_reader.cpp @@ -144,6 +144,7 @@ bool PerfFileReader::ReadAttrSection() sizeof(perf_file_attr)); } CHECK_TRUE(header_.attrSize == 0, false, 0, ""); + CHECK_TRUE(header_.attrSize > THOUSANDS, false, 1, "attr size exceeds 1000"); int attrCount = header_.attrs.size / header_.attrSize; CHECK_TRUE(attrCount == 0, false, 1, "no attr in file"); if (fseek(fp_, header_.attrs.offset, SEEK_SET) != 0) { diff --git a/test/unittest/common/native/perf_file_reader_test.cpp b/test/unittest/common/native/perf_file_reader_test.cpp index 5207d22..f59cdfc 100644 --- a/test/unittest/common/native/perf_file_reader_test.cpp +++ b/test/unittest/common/native/perf_file_reader_test.cpp @@ -149,6 +149,17 @@ HWTEST_F(PerfFileReaderTest, ReadIdsForAttr3, TestSize.Level1) EXPECT_TRUE(reader.ReadIdsForAttr(attr, &v)); EXPECT_TRUE(v.size() * sizeof(uint64_t) >= attr.ids.size); } + +HWTEST_F(PerfFileReaderTest, Test_OverAttrSize, TestSize.Level1) +{ + const uint64_t overSize = 100 * sizeof(perf_file_attr); + const std::string filename = "perf.data"; + FILE *fp = stdout; + PerfFileReader *hiperfFileReader = new PerfFileReader(filename, fp); + perf_file_header header = hiperfFileReader->GetHeader(); + header.attrSize = overSize; + EXPECT_EQ(hiperfFileReader->ReadAttrSection(), false); +} } // namespace HiPerf } // namespace Developtools } // namespace OHOS diff --git a/test/unittest/common/native/report_test.cpp b/test/unittest/common/native/report_test.cpp index 2fe1a7f..c130140 100644 --- a/test/unittest/common/native/report_test.cpp +++ b/test/unittest/common/native/report_test.cpp @@ -813,6 +813,23 @@ HWTEST_F(ReportTest, PrepareConsole, TestSize.Level1) EXPECT_EQ(report_->consoleWidth_, report_->ConsoleDefaultWidth); } } + +HWTEST_F(ReportTest, OverConfigIndex, TestSize.Level1) +{ + pid_t pid = fork(); + ASSERT_NE(pid, -1); + + if (pid == 0) { + report_->configIdIndexMaps_.emplace(1000u, 1000u); + report_->GetConfigName(1000); + _exit(-2); + } else { + int status = 0; + waitpid(pid, &status, 0); + ASSERT_TRUE(WIFEXITED(status)); + EXPECT_EQ(WEXITSTATUS(status), static_cast(-1)); + } +} } // namespace HiPerf } // namespace Developtools } // namespace OHOS diff --git a/test/unittest/common/native/subcommand_stat_test.cpp b/test/unittest/common/native/subcommand_stat_test.cpp index 4f5eec5..facf6fa 100644 --- a/test/unittest/common/native/subcommand_stat_test.cpp +++ b/test/unittest/common/native/subcommand_stat_test.cpp @@ -2245,18 +2245,19 @@ HWTEST_F(SubCommandStatTest, TestOnSubCommand_app_running, TestSize.Level1) */ HWTEST_F(SubCommandStatTest, CheckPidAndApp, TestSize.Level1) { + std::string cmd = "stat -p " + std::to_string(INT_MAX) + " -d 2"; + EXPECT_EQ(Command::DispatchCommand(cmd), false); pid_t existPid = -1; const std::string basePath {"/proc/"}; std::vector subDirs = GetSubDirs(basePath); - for (const auto &subDir : subDirs) { + for (int i = subDirs.size() - 1; i >= 0; i--) { + std::string subDir = subDirs[i]; if (!IsDigits(subDir)) { continue; } - pid_t pid = std::stoll(subDir); - existPid = max(existPid, pid); + existPid = std::stoll(subDir); + break; } - std::string cmd = "stat -p " + std::to_string(existPid + rand() % 100 + 1) + " -d 2"; - EXPECT_EQ(Command::DispatchCommand(cmd), false); StdoutRecord stdoutRecord; stdoutRecord.Start(); -- Gitee