]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: use 64 bit calculation in overLoadFactor
authorJosh Bleecher Snyder <josharian@gmail.com>
Mon, 1 May 2017 14:36:43 +0000 (07:36 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Mon, 1 May 2017 17:35:57 +0000 (17:35 +0000)
overLoadFactor used a uintptr for its calculations.
When the number of potential buckets was large,
perhaps due to a coding error or corrupt/malicious user input
leading to a very large map size hint,
this led to overflow on 32 bit systems.
This overflow resulted in an infinite loop.

Prevent it by always using a 64 bit calculation.

Updates #20195

Change-Id: Iaabc710773cd5da6754f43b913478cc5562d89a2
Reviewed-on: https://go-review.googlesource.com/42185
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
src/runtime/hashmap.go

index 0c9505e655be645473f95b078054e682ab07f24f..c6c2fa5fdf64fb9f41962a073076d50bc6e8532a 100644 (file)
@@ -985,7 +985,7 @@ func hashGrow(t *maptype, h *hmap) {
 // overLoadFactor reports whether count items placed in 1<<B buckets is over loadFactor.
 func overLoadFactor(count int64, B uint8) bool {
        // TODO: rewrite to use integer math and comparison?
-       return count >= bucketCnt && float32(count) >= loadFactor*float32((uintptr(1)<<B))
+       return count >= bucketCnt && float32(count) >= loadFactor*float32((uint64(1)<<B))
 }
 
 // tooManyOverflowBuckets reports whether noverflow buckets is too many for a map with 1<<B buckets.