Skip to content

Commit

Permalink
runtime/metrics: fix panic in readingAllMetric example
Browse files Browse the repository at this point in the history
medianBucket can return if the total is greater than thresh.
However, if a histogram has no counts, total and thresh
will both be zero and cause panic.

Adding an equal sign to prevent the potential panic.

Fixes #44148

Change-Id: Ifb8a781990f490d142ae7c035b4e01d6a07ae04d
Reviewed-on: https://go-review.googlesource.com/c/go/+/290171
Trust: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Run-TryBot: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
  • Loading branch information
changkun authored and mknyszek committed Feb 8, 2021
1 parent ed3e4af commit 1901853
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/runtime/metrics/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func medianBucket(h *metrics.Float64Histogram) float64 {
total = 0
for i, count := range h.Counts {
total += count
if total > thresh {
if total >= thresh {
return h.Buckets[i]
}
}
Expand Down

0 comments on commit 1901853

Please sign in to comment.