From e3cc4e43c4b8c23518c2b20d6182add1ef45215b Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Mon, 4 Nov 2024 11:16:56 +0100 Subject: [PATCH] fix: zero-lenght h.upperBounds Signed-off-by: Ivan Goncharov --- prometheus/histogram.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/prometheus/histogram.go b/prometheus/histogram.go index d01e68d50..c0b6ecd7b 100644 --- a/prometheus/histogram.go +++ b/prometheus/histogram.go @@ -858,13 +858,16 @@ func (h *histogram) Write(out *dto.Metric) error { // findBucket returns the index of the bucket for the provided value, or // len(h.upperBounds) for the +Inf bucket. func (h *histogram) findBucket(v float64) int { + n := len(h.upperBounds) + if n == 0 { + return 0 + } + // Early exit: if v is less than or equal to the first upper bound, return 0 if v <= h.upperBounds[0] { return 0 } - n := len(h.upperBounds) - // Early exit: if v is greater than the last upper bound, return len(h.upperBounds) if v > h.upperBounds[n-1] { return n