From 5c488e3145330f581fd3ebdbdfe81e0f2cd736b7 Mon Sep 17 00:00:00 2001 From: Damian Gryski Date: Wed, 9 Mar 2022 15:41:53 -0800 Subject: [PATCH] src/runtime: handle nil map write panics --- src/runtime/hashmap.go | 10 +++++++++- src/runtime/panic.go | 5 +++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/runtime/hashmap.go b/src/runtime/hashmap.go index bd2489f6..53542d54 100644 --- a/src/runtime/hashmap.go +++ b/src/runtime/hashmap.go @@ -406,7 +406,9 @@ func hashmapNext(m *hashmap, it *hashmapIterator, key, value unsafe.Pointer) boo // Hashmap with plain binary data keys (not containing strings etc.). func hashmapBinarySet(m *hashmap, key, value unsafe.Pointer) { - // TODO: detect nil map here and throw a better panic message? + if m == nil { + nilMapPanic() + } hash := hash32(key, uintptr(m.keySize), m.seed) hashmapSet(m, key, value, hash) } @@ -445,6 +447,9 @@ func hashmapStringPtrHash(sptr unsafe.Pointer, size uintptr, seed uintptr) uint3 } func hashmapStringSet(m *hashmap, key string, value unsafe.Pointer) { + if m == nil { + nilMapPanic() + } hash := hashmapStringHash(key, m.seed) hashmapSet(m, unsafe.Pointer(&key), value, hash) } @@ -561,6 +566,9 @@ func hashmapInterfaceEqual(x, y unsafe.Pointer, n uintptr) bool { } func hashmapInterfaceSet(m *hashmap, key interface{}, value unsafe.Pointer) { + if m == nil { + nilMapPanic() + } hash := hashmapInterfaceHash(key, m.seed) hashmapSet(m, unsafe.Pointer(&key), value, hash) } diff --git a/src/runtime/panic.go b/src/runtime/panic.go index 51778d06..0dc94886 100644 --- a/src/runtime/panic.go +++ b/src/runtime/panic.go @@ -32,6 +32,11 @@ func nilPanic() { runtimePanic("nil pointer dereference") } +// Panic when trying to add an entry to a nil map +func nilMapPanic() { + runtimePanic("assignment to entry in nil map") +} + // Panic when trying to acces an array or slice out of bounds. func lookupPanic() { runtimePanic("index out of range")