Commit f9531448 authored by Filip Gruszczynski's avatar Filip Gruszczynski Committed by Brad Fitzpatrick

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: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: default avatarKeith Randall <khr@golang.org>
parent 9f6fde3a
...@@ -255,9 +255,8 @@ func makemap(t *maptype, hint int64, h *hmap, bucket unsafe.Pointer) *hmap { ...@@ -255,9 +255,8 @@ func makemap(t *maptype, hint int64, h *hmap, bucket unsafe.Pointer) *hmap {
throw("bad hmap size") throw("bad hmap size")
} }
if hint < 0 || int64(int32(hint)) != hint { if hint < 0 || hint > int64(maxSliceCap(t.bucket.size)) {
panic(plainError("makemap: size out of range")) hint = 0
// TODO: make hint an int, then none of this nonsense
} }
if !ismapkey(t.key) { if !ismapkey(t.key) {
......
...@@ -588,6 +588,14 @@ func TestMapLargeValNoPointer(t *testing.T) { ...@@ -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) { func benchmarkMapPop(b *testing.B, n int) {
m := map[int]int{} m := map[int]int{}
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
......
...@@ -48,15 +48,6 @@ func bigcap() { ...@@ -48,15 +48,6 @@ func bigcap() {
g1 = make([]block, 10, big) 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 type cblock [1<<16-1]byte
var g4 chan cblock var g4 chan cblock
...@@ -78,8 +69,6 @@ func main() { ...@@ -78,8 +69,6 @@ func main() {
shouldfail(badcap, "badcap") shouldfail(badcap, "badcap")
shouldfail(badcap1, "badcap1") shouldfail(badcap1, "badcap1")
shouldfail(bigcap, "bigcap") shouldfail(bigcap, "bigcap")
shouldfail(badmapcap, "badmapcap")
shouldfail(bigmapcap, "bigmapcap")
shouldfail(badchancap, "badchancap") shouldfail(badchancap, "badchancap")
shouldfail(bigchancap, "bigchancap") shouldfail(bigchancap, "bigchancap")
shouldfail(overflowchan, "overflowchan") shouldfail(overflowchan, "overflowchan")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment