Skip to content

Commit 04fc887

Browse files
committed
runtime: delay marking maps as writing until after first alg call
Fixes #19359 Change-Id: I196b47cf0471915b6dc63785e8542aa1876ff695 Reviewed-on: https://go-review.googlesource.com/37665 Run-TryBot: Josh Bleecher Snyder <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 0ee9c46 commit 04fc887

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/runtime/hashmap.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -498,11 +498,13 @@ func mapassign(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer {
498498
if h.flags&hashWriting != 0 {
499499
throw("concurrent map writes")
500500
}
501-
h.flags |= hashWriting
502-
503501
alg := t.key.alg
504502
hash := alg.hash(key, uintptr(h.hash0))
505503

504+
// Set hashWriting after calling alg.hash, since alg.hash may panic,
505+
// in which case we have not actually done a write.
506+
h.flags |= hashWriting
507+
506508
if h.buckets == nil {
507509
h.buckets = newarray(t.bucket, 1)
508510
}
@@ -611,10 +613,14 @@ func mapdelete(t *maptype, h *hmap, key unsafe.Pointer) {
611613
if h.flags&hashWriting != 0 {
612614
throw("concurrent map writes")
613615
}
614-
h.flags |= hashWriting
615616

616617
alg := t.key.alg
617618
hash := alg.hash(key, uintptr(h.hash0))
619+
620+
// Set hashWriting after calling alg.hash, since alg.hash may panic,
621+
// in which case we have not actually done a write (delete).
622+
h.flags |= hashWriting
623+
618624
bucket := hash & (uintptr(1)<<h.B - 1)
619625
if h.growing() {
620626
growWork(t, h, bucket)

test/fixedbugs/issue19359.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// run
2+
3+
// Copyright 2017 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
import "fmt"
10+
11+
func set(m map[interface{}]interface{}, key interface{}) (err error) {
12+
defer func() {
13+
if r := recover(); r != nil {
14+
err = fmt.Errorf("set failed: %v", r)
15+
}
16+
}()
17+
m[key] = nil
18+
return nil
19+
}
20+
21+
func del(m map[interface{}]interface{}, key interface{}) (err error) {
22+
defer func() {
23+
if r := recover(); r != nil {
24+
err = fmt.Errorf("del failed: %v", r)
25+
}
26+
}()
27+
delete(m, key)
28+
return nil
29+
}
30+
31+
func main() {
32+
m := make(map[interface{}]interface{})
33+
set(m, []int{1, 2, 3})
34+
set(m, "abc") // used to throw
35+
del(m, []int{1, 2, 3})
36+
del(m, "abc") // used to throw
37+
}

0 commit comments

Comments
 (0)