Bug #81019
openmgr: PerfCounterInstance::get_latest_data() calls buffer.back() with no empty() guard (UB on empty buffer)
0%
Description
PerfCounterInstance::get_latest_data() (src/mgr/PerfCounterInstance.h:59) returns buffer.back() with no !buffer.empty() check. boost::circular_buffer::back() on an empty buffer is undefined behaviour (dereferences an invalid iterator). A freshly-constructed non-avg PerfCounterInstance has an empty (capacity-20) buffer until the first push(); calling get_latest_data() before any data point exists is UB -- a potential mgr daemon crash / bad read.
const DataPoint& get_latest_data() const
{
return buffer.back(); // line 61 - no empty() guard
}
Failure scenario¶
Construct PerfCounterInstance(PERFCOUNTER_U64) (non-avg), then call get_latest_data() before push() -> buffer.back() on an empty circular_buffer -> UB.
Suggested fix¶
Guard with if (buffer.empty()) and return/throw a defined result, or require callers to check get_data().empty() first (and document the precondition).
Unit test (illustrative)¶
TEST(PerfCounterInstance, LatestDataOnEmptyIsGuarded) {
PerfCounterInstance pci(PERFCOUNTER_U64);
ASSERT_TRUE(pci.get_data().empty());
// Current code: pci.get_latest_data() is UB here.
// After fix: expect defined behavior (guard / throw / sentinel).
}
SZ Updated by Steven Zuraski 2 months ago
- Status changed from New to Fix Under Review
- Pull request ID set to 72352