From b5908aedc0fdcf47a2f73f7e04eccd2e99a66357 Mon Sep 17 00:00:00 2001 From: Yao Chen Date: Fri, 1 Mar 2019 10:04:47 -0800 Subject: [PATCH] Fix incident_helper crash. The header of top cmd changes from Tasks to Threads, causing the parser to run into undefined behavior. Bug: 125377622 Test: adb shell dumpsys incident && incident_helper_test Change-Id: I5ae88767dce22f0ee7b49611b2f19889a07e66ac --- cmds/incident_helper/src/ih_util.cpp | 2 +- cmds/incident_helper/src/parsers/CpuInfoParser.cpp | 5 +++-- cmds/incident_helper/testdata/cpuinfo.txt | 8 ++++---- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cmds/incident_helper/src/ih_util.cpp b/cmds/incident_helper/src/ih_util.cpp index 012310cc277af..77a56e55045b6 100644 --- a/cmds/incident_helper/src/ih_util.cpp +++ b/cmds/incident_helper/src/ih_util.cpp @@ -142,7 +142,7 @@ record_t parseRecordByColumns(const std::string& line, const std::vector& i } if (lineSize - lastIndex > 0) { int beginning = lastIndex; - if (record.size() == indices.size()) { + if (record.size() == indices.size() && !record.empty()) { // We've already encountered all of the columns...put whatever is // left in the last column. record.pop_back(); diff --git a/cmds/incident_helper/src/parsers/CpuInfoParser.cpp b/cmds/incident_helper/src/parsers/CpuInfoParser.cpp index 21ced9cb485c9..5d525e6c7f3ea 100644 --- a/cmds/incident_helper/src/parsers/CpuInfoParser.cpp +++ b/cmds/incident_helper/src/parsers/CpuInfoParser.cpp @@ -65,8 +65,9 @@ CpuInfoParser::Parse(const int in, const int out) const if (line.empty()) continue; nline++; - - if (stripPrefix(&line, "Tasks:")) { + // The format changes from time to time in toybox/toys/posix/ps.c + // With -H, it prints Threads instead of Tasks (FLAG(H)?"Thread":"Task") + if (stripPrefix(&line, "Threads:")) { writeSuffixLine(&proto, CpuInfoProto::TASK_STATS, line, COMMA_DELIMITER, CpuInfoProto::TaskStats::_FIELD_COUNT, CpuInfoProto::TaskStats::_FIELD_NAMES, diff --git a/cmds/incident_helper/testdata/cpuinfo.txt b/cmds/incident_helper/testdata/cpuinfo.txt index ec4a839606983..aa3afc33ad6ad 100644 --- a/cmds/incident_helper/testdata/cpuinfo.txt +++ b/cmds/incident_helper/testdata/cpuinfo.txt @@ -1,8 +1,8 @@ -Tasks: 2038 total, 1 running,2033 sleeping, 0 stopped, 0 zombie +Threads: 2038 total, 1 running,2033 sleeping, 0 stopped, 0 zombie -Mem: 3842668k total, 3761936k used, 80732k free, 220188k buffers + Mem: 3842668k total, 3761936k used, 80732k free, 220188k buffers -Swap: 524284k total, 25892k used, 498392k free, 1316952k cached + Swap: 524284k total, 25892k used, 498392k free, 1316952k cached 400%cpu 17%user 0%nice 43%sys 338%idle 0%iow 0%irq 1%sirq 0%host @@ -12,4 +12,4 @@ Swap: 524284k total, 25892k used, 498392k free, 1316952k cached 29438 29438 rootabcdefghij 20 0 57.9 R 14M 3.8M top test top 916 916 system 18 -2 1.4 S 4.6G 404M fg system_server system_server 28 28 root -2 0 1.4 S 0 0 bg rcuc/3 [rcuc/3] - 27 27 root RT 0 1.4 S 0 0 ta migration/3 [migration/3] \ No newline at end of file + 27 27 root RT 0 1.4 S 0 0 ta migration/3 [migration/3]