Skip to content

Commit 5c488e3

Browse files
dgryskideadprogram
authored andcommitted
src/runtime: handle nil map write panics
1 parent e45ff9c commit 5c488e3

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/runtime/hashmap.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,9 @@ func hashmapNext(m *hashmap, it *hashmapIterator, key, value unsafe.Pointer) boo
406406

407407
// Hashmap with plain binary data keys (not containing strings etc.).
408408
func hashmapBinarySet(m *hashmap, key, value unsafe.Pointer) {
409-
// TODO: detect nil map here and throw a better panic message?
409+
if m == nil {
410+
nilMapPanic()
411+
}
410412
hash := hash32(key, uintptr(m.keySize), m.seed)
411413
hashmapSet(m, key, value, hash)
412414
}
@@ -445,6 +447,9 @@ func hashmapStringPtrHash(sptr unsafe.Pointer, size uintptr, seed uintptr) uint3
445447
}
446448

447449
func hashmapStringSet(m *hashmap, key string, value unsafe.Pointer) {
450+
if m == nil {
451+
nilMapPanic()
452+
}
448453
hash := hashmapStringHash(key, m.seed)
449454
hashmapSet(m, unsafe.Pointer(&key), value, hash)
450455
}
@@ -561,6 +566,9 @@ func hashmapInterfaceEqual(x, y unsafe.Pointer, n uintptr) bool {
561566
}
562567

563568
func hashmapInterfaceSet(m *hashmap, key interface{}, value unsafe.Pointer) {
569+
if m == nil {
570+
nilMapPanic()
571+
}
564572
hash := hashmapInterfaceHash(key, m.seed)
565573
hashmapSet(m, unsafe.Pointer(&key), value, hash)
566574
}

src/runtime/panic.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ func nilPanic() {
3232
runtimePanic("nil pointer dereference")
3333
}
3434

35+
// Panic when trying to add an entry to a nil map
36+
func nilMapPanic() {
37+
runtimePanic("assignment to entry in nil map")
38+
}
39+
3540
// Panic when trying to acces an array or slice out of bounds.
3641
func lookupPanic() {
3742
runtimePanic("index out of range")

0 commit comments

Comments
 (0)