convertnhcb: reject NaN bucket boundary in SetBucketCount (#18383)

Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
This commit is contained in:
Julien
2026-03-31 14:23:17 +02:00
committed by GitHub
parent ffb296eed7
commit 7fd7a393b6
2 changed files with 18 additions and 0 deletions

View File

@@ -26,6 +26,7 @@ import (
var (
errNegativeBucketCount = errors.New("bucket count must be non-negative")
errNaNBucket = errors.New("bucket boundary must not be NaN")
errNegativeCount = errors.New("count must be non-negative")
errCountMismatch = errors.New("count mismatch")
errCountNotCumulative = errors.New("count is not cumulative")
@@ -71,6 +72,10 @@ func (h *TempHistogram) SetBucketCount(boundary, count float64) error {
if h.err != nil {
return h.err
}
if math.IsNaN(boundary) {
h.err = errNaNBucket
return h.err
}
if count < 0 {
h.err = fmt.Errorf("%w: le=%g, count=%g", errNegativeBucketCount, boundary, count)
return h.err

View File

@@ -147,6 +147,19 @@ func TestNHCBConvert(t *testing.T) {
},
expectedErr: errNegativeCount,
},
"NaN bucket upper bound": {
setup: func() *TempHistogram {
h := NewTempHistogram()
// Add a real bucket first so that the NaN call reaches the
// default branch of the switch in SetBucketCount; without an
// existing bucket, len(h.buckets)==0 and the NaN is silently
// appended without triggering the panic.
h.SetBucketCount(1.0, 5)
h.SetBucketCount(math.NaN(), 10)
return &h
},
expectedErr: errNaNBucket,
},
"mixed order": {
setup: func() *TempHistogram {
h := NewTempHistogram()