Bug #81337
openmgr: ThreadMonitor::read_thread_stat() returns true with uninitialised values on malformed /proc stat line
0%
Description
ThreadMonitor::read_thread_stat() (src/mgr/ThreadMonitor.cc:226) parses /proc/<pid>/task/<tid>/stat using std::stringstream >> utime >> stime. There is no stream success check after the extraction. If the parse fails (malformed or unexpectedly short line), utime and stime contain uninitialised / default values and the function still returns true, causing the caller to compute a CPU percentage from garbage data.
bool ThreadMonitor::read_thread_stat(pid_t tid, ...) {
std::ifstream stat_file(...);
std::string stat_content(...);
std::stringstream ss(stat_content);
// skip fields 1-13
uint64_t utime, stime;
ss >> utime >> stime; // no success check
// uses utime/stime even if extraction failed
...
return true; // unconditional true
}
Failure scenario¶
On a system where the /proc stat line is unexpectedly formatted (e.g. a future kernel adds fields before utime/stime, or the file is transiently short), utime and stime hold uninitialised values. The subsequent CPU-percentage calculation produces garbage metrics silently reported to all mgr modules.
Suggested fix¶
Check ss.fail() or the boolean result of each >> extraction, and return false on parse failure:
if (!(ss >> utime >> stime))
return false;
Unit test (illustrative)¶
TEST(ThreadMonitor, ReadThreadStatReturnsFalseOnMalformedLine) {
// Feed a deliberately malformed stat line; assert read_thread_stat returns false.
}
SZ Updated by Steven Zuraski 2 months ago
- Status changed from New to Fix Under Review
- Pull request ID set to 72354