From 8aea9fb68156dfbaf2a788f7ffc571e2aa03bd61 Mon Sep 17 00:00:00 2001 From: zhou_xq Date: Thu, 21 Aug 2025 15:40:23 +0800 Subject: [PATCH 1/2] fix:FormatJsonStack Signed-off-by: zhou_xq --- .../formatter/dfx_json_formatter.cpp | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/interfaces/innerkits/formatter/dfx_json_formatter.cpp b/interfaces/innerkits/formatter/dfx_json_formatter.cpp index ac349891b..f438cf718 100644 --- a/interfaces/innerkits/formatter/dfx_json_formatter.cpp +++ b/interfaces/innerkits/formatter/dfx_json_formatter.cpp @@ -85,7 +85,11 @@ bool DfxJsonFormatter::FormatJsonStack(const std::string& jsonStack, std::string outStackStr.append("Failed to parse json stack info."); return false; } - + constexpr int maxThreadCount = 10000; + if (threads.size() > maxThreadCount) { + outStackStr.append("Thread count exceeds limit(10000)."); + return false; + } for (uint32_t i = 0; i < threads.size(); ++i) { std::string ss; Json::Value thread = threads[i]; @@ -93,23 +97,30 @@ bool DfxJsonFormatter::FormatJsonStack(const std::string& jsonStack, std::string thread["thread_name"].isConvertibleTo(Json::stringValue)) { ss += "Tid:" + thread["tid"].asString() + ", Name:" + thread["thread_name"].asString() + "\n"; } - const Json::Value frames = thread["frames"]; - for (uint32_t j = 0; j < frames.size(); ++j) { - std::string frameStr = ""; - bool formatStatus = false; - if (frames[j]["line"].asString().empty()) { - formatStatus = FormatNativeFrame(frames, j, frameStr); - } else { - formatStatus = FormatJsFrame(frames, j, frameStr); + if (thread.isMember("frames") && thread["frames"].isArray()) { + const Json::Value frames = thread["frames"]; + constexpr int maxFrameNum = 1000; + if (frames.size() > maxFrameNum) { + continue; } - if (formatStatus) { - ss += frameStr + "\n"; - } else { - // Shall we try to print more information? - outStackStr.append("Frame info is illegal."); - return false; + for (uint32_t j = 0; j < frames.size(); ++j) { + std::string frameStr = ""; + bool formatStatus = false; + if (frames[j]["line"].asString().empty()) { + formatStatus = FormatNativeFrame(frames, j, frameStr); + } else { + formatStatus = FormatJsFrame(frames, j, frameStr); + } + if (formatStatus) { + ss += frameStr + "\n"; + } else { + // Shall we try to print more information? + outStackStr.append("Frame info is illegal."); + return false; + } } } + outStackStr.append(ss); } return true; -- Gitee From 93b4f6476a7441a0953fd8179d4aae788e31efcb Mon Sep 17 00:00:00 2001 From: zhou_xq Date: Thu, 21 Aug 2025 08:38:24 +0000 Subject: [PATCH 2/2] update interfaces/innerkits/formatter/dfx_json_formatter.cpp. Signed-off-by: zhou_xq --- .../formatter/dfx_json_formatter.cpp | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/interfaces/innerkits/formatter/dfx_json_formatter.cpp b/interfaces/innerkits/formatter/dfx_json_formatter.cpp index f438cf718..677c5ce8c 100644 --- a/interfaces/innerkits/formatter/dfx_json_formatter.cpp +++ b/interfaces/innerkits/formatter/dfx_json_formatter.cpp @@ -97,27 +97,28 @@ bool DfxJsonFormatter::FormatJsonStack(const std::string& jsonStack, std::string thread["thread_name"].isConvertibleTo(Json::stringValue)) { ss += "Tid:" + thread["tid"].asString() + ", Name:" + thread["thread_name"].asString() + "\n"; } - if (thread.isMember("frames") && thread["frames"].isArray()) { - const Json::Value frames = thread["frames"]; - constexpr int maxFrameNum = 1000; - if (frames.size() > maxFrameNum) { - continue; + if (!thread.isMember("frames") || !thread["frames"].isArray()) { + continue; + } + const Json::Value frames = thread["frames"]; + constexpr int maxFrameNum = 1000; + if (frames.size() > maxFrameNum) { + continue; + } + for (uint32_t j = 0; j < frames.size(); ++j) { + std::string frameStr = ""; + bool formatStatus = false; + if (frames[j]["line"].asString().empty()) { + formatStatus = FormatNativeFrame(frames, j, frameStr); + } else { + formatStatus = FormatJsFrame(frames, j, frameStr); } - for (uint32_t j = 0; j < frames.size(); ++j) { - std::string frameStr = ""; - bool formatStatus = false; - if (frames[j]["line"].asString().empty()) { - formatStatus = FormatNativeFrame(frames, j, frameStr); - } else { - formatStatus = FormatJsFrame(frames, j, frameStr); - } - if (formatStatus) { - ss += frameStr + "\n"; - } else { - // Shall we try to print more information? - outStackStr.append("Frame info is illegal."); - return false; - } + if (formatStatus) { + ss += frameStr + "\n"; + } else { + // Shall we try to print more information? + outStackStr.append("Frame info is illegal."); + return false; } } -- Gitee