if mapValue.Type().Kind() != reflect.Map {
return nil
}
- key := make([]reflect.Value, mapValue.Len())
- value := make([]reflect.Value, len(key))
+ // Note: this code is arranged to not panic even in the presence
+ // of a concurrent map update. The runtime is responsible for
+ // yelling loudly if that happens. See issue 33275.
+ n := mapValue.Len()
+ key := make([]reflect.Value, 0, n)
+ value := make([]reflect.Value, 0, n)
iter := mapValue.MapRange()
- for i := 0; iter.Next(); i++ {
- key[i] = iter.Key()
- value[i] = iter.Value()
+ for iter.Next() {
+ key = append(key, iter.Key())
+ value = append(value, iter.Value())
}
sorted := &SortedMap{
Key: key,
--- /dev/null
+// skip
+
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ "fmt"
+ "time"
+)
+
+func main() {
+ // Make a big map.
+ m := map[int]int{}
+ for i := 0; i < 100000; i++ {
+ m[i] = i
+ }
+ c := make(chan string)
+ go func() {
+ // Print the map.
+ s := fmt.Sprintln(m)
+ c <- s
+ }()
+ go func() {
+ time.Sleep(1 * time.Millisecond)
+ // Add an extra item to the map while iterating.
+ m[-1] = -1
+ c <- ""
+ }()
+ <-c
+ <-c
+}
--- /dev/null
+// +build !nacl,!js
+// run
+
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Make sure we don't get an index out of bounds error
+// while trying to print a map that is concurrently modified.
+// The runtime might complain (throw) if it detects the modification,
+// so we have to run the test as a subprocess.
+
+package main
+
+import (
+ "os/exec"
+ "strings"
+)
+
+func main() {
+ out, _ := exec.Command("go", "run", "fixedbugs/issue33275.go").CombinedOutput()
+ if strings.Contains(string(out), "index out of range") {
+ panic(`go run issue33275.go reported "index out of range"`)
+ }
+}