aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFilip Gruszczynski <gruszczy@gmail.com>2017-04-15 15:17:29 -0700
committerBrad Fitzpatrick <bradfitz@golang.org>2017-05-02 20:51:39 +0000
commitf9531448b8abf7bb3c2702761bd8792a3bef33a0 (patch)
tree6476b596aca0c943b1f6fd2456f48c6b5a81fc0b
parent9f6fde3a23c7d40eb625ad3a58a6b1ad4e33265a (diff)
downloadgo-f9531448b8abf7bb3c2702761bd8792a3bef33a0.tar.gz
go-f9531448b8abf7bb3c2702761bd8792a3bef33a0.zip
runtime: don't panic for bad size hint in hashmap
Because the hint parameter is supposed to be treated purely as a hint, if it doesn't meet the requirements we disregard it and continue as if there was no hint at all. Fixes #19926 Change-Id: I86e7f99472fad6b99ba4e2fd33e4a9e55d55115e Reviewed-on: https://go-review.googlesource.com/40854 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Keith Randall <khr@golang.org>
-rw-r--r--src/runtime/hashmap.go5
-rw-r--r--src/runtime/map_test.go8
-rw-r--r--test/fixedbugs/bug273.go11
3 files changed, 10 insertions, 14 deletions
diff --git a/src/runtime/hashmap.go b/src/runtime/hashmap.go
index c6c2fa5fdf..11ce0cbc4b 100644
--- a/src/runtime/hashmap.go
+++ b/src/runtime/hashmap.go
@@ -255,9 +255,8 @@ func makemap(t *maptype, hint int64, h *hmap, bucket unsafe.Pointer) *hmap {
throw("bad hmap size")
}
- if hint < 0 || int64(int32(hint)) != hint {
- panic(plainError("makemap: size out of range"))
- // TODO: make hint an int, then none of this nonsense
+ if hint < 0 || hint > int64(maxSliceCap(t.bucket.size)) {
+ hint = 0
}
if !ismapkey(t.key) {
diff --git a/src/runtime/map_test.go b/src/runtime/map_test.go
index 45d14126c2..81f05a0613 100644
--- a/src/runtime/map_test.go
+++ b/src/runtime/map_test.go
@@ -588,6 +588,14 @@ func TestMapLargeValNoPointer(t *testing.T) {
}
}
+// Test that making a map with a large or invalid hint
+// doesn't panic. (Issue 19926).
+func TestIgnoreBogusMapHint(t *testing.T) {
+ for _, hint := range []int64{-1, 1 << 62} {
+ _ = make(map[int]int, hint)
+ }
+}
+
func benchmarkMapPop(b *testing.B, n int) {
m := map[int]int{}
for i := 0; i < b.N; i++ {
diff --git a/test/fixedbugs/bug273.go b/test/fixedbugs/bug273.go
index b6258d54fc..c04f2116c5 100644
--- a/test/fixedbugs/bug273.go
+++ b/test/fixedbugs/bug273.go
@@ -48,15 +48,6 @@ func bigcap() {
g1 = make([]block, 10, big)
}
-var g3 map[block]block
-func badmapcap() {
- g3 = make(map[block]block, minus1)
-}
-
-func bigmapcap() {
- g3 = make(map[block]block, big)
-}
-
type cblock [1<<16-1]byte
var g4 chan cblock
@@ -78,8 +69,6 @@ func main() {
shouldfail(badcap, "badcap")
shouldfail(badcap1, "badcap1")
shouldfail(bigcap, "bigcap")
- shouldfail(badmapcap, "badmapcap")
- shouldfail(bigmapcap, "bigmapcap")
shouldfail(badchancap, "badchancap")
shouldfail(bigchancap, "bigchancap")
shouldfail(overflowchan, "overflowchan")