]> Cypherpunks repositories - gostls13.git/commitdiff
runtime/metrics: fix panic in readingAllMetric example
authorChangkun Ou <hi@changkun.de>
Sun, 7 Feb 2021 16:31:12 +0000 (17:31 +0100)
committerMichael Knyszek <mknyszek@google.com>
Mon, 8 Feb 2021 16:09:01 +0000 (16:09 +0000)
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>

src/runtime/metrics/example_test.go

index cade0c38bfde62c046d3e7c9332cff9163b4c23b..624d9d8a6bd1ec9d09c00172866b741efbd7470f 100644 (file)
@@ -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]
                }
        }