From 7c579e0a3bce6369af67eb60b2580ad87ab119ea Mon Sep 17 00:00:00 2001 From: meteors117 Date: Thu, 17 Jul 2025 16:23:25 +0800 Subject: [PATCH] [stack_printer] Fix code warnings. Fix code warnings of stack_printer. Include type unmatch between '=' operator, check param validity. Signed-off-by: meteors117 Change-Id: Ice45452653d9f2310b99a8d39b40dc9f45c835fa --- .../stack_printer/include/stack_printer.h | 6 +-- .../stack_printer/src/stack_printer.cpp | 39 +++++++++++++++---- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/interfaces/innerkits/stack_printer/include/stack_printer.h b/interfaces/innerkits/stack_printer/include/stack_printer.h index 8ef75ecca..e34a72331 100644 --- a/interfaces/innerkits/stack_printer/include/stack_printer.h +++ b/interfaces/innerkits/stack_printer/include/stack_printer.h @@ -28,9 +28,9 @@ struct TimeStampedPcs { }; struct SampledFrame { - int indent {0}; // the indent of this frame. - int count {0}; // the count of this frame in the serial stacks. - int level {0}; // the level of this frame in its stack call chain. + uint32_t indent {0}; // the indent of this frame. + int32_t count {0}; // the count of this frame in the serial stacks. + uint32_t level {0}; // the level of this frame in its stack call chain. uintptr_t pc {0}; // pc of this stack frame. bool isLeaf {false}; // whether is leaf node in the tree format stack string. std::vector timestamps; // timestamps of this stack sampled. diff --git a/interfaces/innerkits/stack_printer/src/stack_printer.cpp b/interfaces/innerkits/stack_printer/src/stack_printer.cpp index 707aa197f..1726451c2 100644 --- a/interfaces/innerkits/stack_printer/src/stack_printer.cpp +++ b/interfaces/innerkits/stack_printer/src/stack_printer.cpp @@ -33,6 +33,7 @@ constexpr uint64_t SEC_TO_NANOSEC = 1000000000; constexpr uint64_t MICROSEC_TO_NANOSEC = 1000; constexpr int FORMAT_TIME_LEN = 20; constexpr int MICROSEC_LEN = 6; +constexpr size_t MAX_SAMPLE_TIDS = 10; } // namespace struct StackRecord { @@ -43,7 +44,7 @@ struct StackRecord { struct StackItem { uintptr_t pc {0}; int32_t pcCount {0}; - uint64_t level {0}; + uint32_t level {0}; uint64_t stackId {0}; // Only leaf node update this. std::shared_ptr current {nullptr}; std::shared_ptr child {nullptr}; @@ -464,7 +465,7 @@ std::vector StackPrinter::Impl::GenerateTreeStackFrames(const std: if (root_ != nullptr) { nodes.emplace_back(root_); } - const int indent = 2; + const uint32_t indent = 2; while (!nodes.empty()) { std::shared_ptr back = nodes.back(); SampledFrame sampledFrame; @@ -537,19 +538,41 @@ std::map> StackPrinter::Impl::DeserializeSampledF { std::map> sampledFrameMap; size_t mapSize; - is >> mapSize; - is.ignore(1); + if (!(is >> mapSize)) { + is.setstate(std::ios::failbit); + return sampledFrameMap; + } + if (mapSize > MAX_SAMPLE_TIDS) { + return sampledFrameMap; + } + is.ignore(std::numeric_limits::max(), '\n'); for (size_t i = 0; i < mapSize; i++) { - int tid; + std::string tidStr; size_t vecSize; + if (!(is >> tidStr >> vecSize)) { + is.setstate(std::ios::failbit); + return sampledFrameMap; + } + int base = 10; + char* endPtr; + long tidNum = std::strtol(tidStr.c_str(), &endPtr, base); + if (*endPtr != '\0') { + return sampledFrameMap; + } + int tid = static_cast(tidNum); std::vector sampledFrameVec; - is >> tid >> vecSize; for (size_t j = 0; j < vecSize; j++) { SampledFrame frame; - is >> frame; + if (!(is >> frame)) { + is.setstate(std::ios::failbit); + return sampledFrameMap; + } sampledFrameVec.emplace_back(frame); } - is.ignore(1); + is.ignore(std::numeric_limits::max(), '\n'); + if (tid <= 0) { + continue; + } sampledFrameMap[tid] = sampledFrameVec; } return sampledFrameMap; -- Gitee