]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: compute the median h uniformly
authorJes Cok <xigua67damn@gmail.com>
Sat, 7 Oct 2023 16:18:16 +0000 (16:18 +0000)
committerGopher Robot <gobot@golang.org>
Mon, 9 Oct 2023 21:56:00 +0000 (21:56 +0000)
Like sort.Search, use "h := int(uint(i+j) >> 1)" to compute the median h.

Change-Id: I62c1e67533657e2b56af872676e5dc4ed4d49c55
GitHub-Last-Rev: cbfba7ee9ab0a0bda371db3ad5e8cc4d5ea91579
GitHub-Pull-Request: golang/go#63439
Reviewed-on: https://go-review.googlesource.com/c/go/+/533595
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/reflect/type.go

index d6744c2898f804c0da69f76c47946eecc53e9a0e..a35898547a7f661c3f3bfb19db2b878368e3a944 100644 (file)
@@ -1539,7 +1539,7 @@ func typesByString(s string) []*abi.Type {
                // This is a copy of sort.Search, with f(h) replaced by (*typ[h].String() >= s).
                i, j := 0, len(offs)
                for i < j {
-                       h := i + (j-i)>>1 // avoid overflow when computing h
+                       h := int(uint(i+j) >> 1) // avoid overflow when computing h
                        // i ≤ h < j
                        if !(stringFor(rtypeOff(section, offs[h])) >= s) {
                                i = h + 1 // preserves f(i-1) == false