Skip to content

Commit c57ee57

Browse files
authored
Fix ValidateCounterId so an ID exactly equal to MaxCounterId also fails. (#143)
1 parent 37d675d commit c57ee57

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

src/Adaptive.Agrona/Concurrent/Status/CountersReader.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,12 @@ public string GetCounterLabel(int counterId)
456456

457457
protected void ValidateCounterId(int counterId)
458458
{
459-
if (counterId < 0 || counterId > MaxCounterId)
459+
// .NET's MaxCounterId is one-past-max (capacity / COUNTER_LENGTH), used as the
460+
// upper bound in `for (i = 0; i < MaxCounterId; i++)` iteration loops throughout
461+
// the codebase. So a counterId equal to MaxCounterId is out of bounds — it would
462+
// produce CounterOffset(counterId) == capacity and a buffer-overrun read.
463+
// Java's maxCounterId is one less (inclusive max) and uses `> maxCounterId` here.
464+
if (counterId < 0 || counterId >= MaxCounterId)
460465
{
461466
throw new System.ArgumentException(
462467
"Counter id " + counterId + " out of range: maxCounterId=" + MaxCounterId

0 commit comments

Comments
 (0)