Skip to content

Commit 23ffb5b

Browse files
committed
runtime: overwrite existing keys for mapassign_faststr variant
Fixes #45045 Change-Id: Ifcc7bd31591870446ce3e5127489a0b887d413f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/305089 Trust: Cuong Manh Le <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 49dccf1 commit 23ffb5b

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/runtime/map_faststr.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ bucketloop:
255255
// already have a mapping for key. Update it.
256256
inserti = i
257257
insertb = b
258+
// Overwrite existing key, so it can be garbage collected.
259+
// The size is already guaranteed to be set correctly.
260+
k.str = key.str
258261
goto done
259262
}
260263
ovf := b.overflow(t)

test/fixedbugs/issue45045.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// run
2+
3+
// Copyright 2021 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 (
10+
"reflect"
11+
"runtime"
12+
"unsafe"
13+
)
14+
15+
func k(c chan string, val string) string {
16+
b := make([]byte, 1000)
17+
runtime.SetFinalizer(&b[0], func(*byte) {
18+
c <- val
19+
})
20+
var s string
21+
h := (*reflect.StringHeader)(unsafe.Pointer(&s))
22+
h.Data = uintptr(unsafe.Pointer(&b[0]))
23+
h.Len = len(b)
24+
return s
25+
}
26+
27+
func main() {
28+
{
29+
c := make(chan string, 2)
30+
m := make(map[string]int)
31+
m[k(c, "first")] = 0
32+
m[k(c, "second")] = 0
33+
runtime.GC()
34+
if s := <-c; s != "first" {
35+
panic("map[string], second key did not retain.")
36+
}
37+
runtime.KeepAlive(m)
38+
}
39+
40+
{
41+
c := make(chan string, 2)
42+
m := make(map[[2]string]int)
43+
m[[2]string{k(c, "first")}] = 0
44+
m[[2]string{k(c, "second")}] = 0
45+
runtime.GC()
46+
if s := <-c; s != "first" {
47+
panic("map[[2]string], second key did not retain.")
48+
}
49+
runtime.KeepAlive(m)
50+
}
51+
}

0 commit comments

Comments
 (0)