Bug #81020
openmgr: PerfCounterInstance::get_latest_data_avg() calls avg_buffer.back() with no empty() guard (UB on empty/scalar buffer)
0%
Description
PerfCounterInstance::get_latest_data_avg() (src/mgr/PerfCounterInstance.h:67) returns avg_buffer.back() with no !avg_buffer.empty() check -- the same class of UB as get_latest_data(). It is reachable two ways: (1) an avg-type instance before its first push_avg(), and (2) any SCALAR (non-PERFCOUNTER_LONGRUNAVG) instance, whose avg_buffer is default-constructed with capacity 0 and is always empty -- so calling get_latest_data_avg() on a scalar instance is always UB.
const AvgDataPoint& get_latest_data_avg() const
{
return avg_buffer.back(); // line 69 - no empty() guard; UB on empty/zero-cap
}
The constructor only sizes one of the two buffers:
PerfCounterInstance(enum perfcounter_type_d type)
{
if (type & PERFCOUNTER_LONGRUNAVG)
avg_buffer = boost::circular_buffer<AvgDataPoint>(20);
else
buffer = boost::circular_buffer<DataPoint>(20); // avg_buffer stays capacity 0
}
Failure scenario¶
Construct PerfCounterInstance(PERFCOUNTER_U64) (scalar; avg_buffer capacity 0) and call get_latest_data_avg() -> avg_buffer.back() on a zero-capacity/empty buffer -> UB.
Suggested fix¶
Guard with if (avg_buffer.empty()); consider asserting the instance is a LONGRUNAVG type before serving avg data.
Unit test (illustrative)¶
TEST(PerfCounterInstance, LatestAvgOnScalarIsGuarded) {
PerfCounterInstance pci(PERFCOUNTER_U64); // scalar: avg_buffer capacity 0
ASSERT_TRUE(pci.get_data_avg().empty());
// Current code: pci.get_latest_data_avg() is UB here.
}
SZ Updated by Steven Zuraski 2 months ago
- Status changed from New to Fix Under Review
- Pull request ID set to 72353