Project

General

Profile

Actions

Bug #81019

open
SZ SZ

mgr: PerfCounterInstance::get_latest_data() calls buffer.back() with no empty() guard (UB on empty buffer)

Bug #81019: mgr: PerfCounterInstance::get_latest_data() calls buffer.back() with no empty() guard (UB on empty buffer)

Added by Steven Zuraski 2 months ago. Updated 2 months ago.

Status:
Fix Under Review
Priority:
Normal
Category:
ceph-mgr
Target version:
-
Due date:
% Done:

0%

Source:
Backport:
Regression:
No
Severity:
3 - minor
Reviewed:
Affected Versions:
ceph-qa-suite:
Pull request ID:
Tags (freeform):
Merge Commit:
Fixed In:
Released In:
Upkeep Timestamp:

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 Actions #1

  • Status changed from New to Fix Under Review
  • Pull request ID set to 72352
Actions

Also available in: PDF Atom