Skip to content

Commit dd7cbf3

Browse files
committed
cmd/compile: fix map assignment with panicking right-hand side
Make sure that when we're assigning to a map, we evaluate the right-hand side before we attempt to insert into the map. We used to evaluate the left-hand side to a pointer-to-slot-in-bucket (which as a side effect does len(m)++), then evaluate the right-hand side, then do the assignment. That clearly isn't correct when the right-hand side might panic. Fixes #22881 Change-Id: I42a62870ff4bf480568c9bdbf0bb18958962bdf0 Reviewed-on: https://go-review.googlesource.com/81817 Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 2ff2eab commit dd7cbf3

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

src/cmd/compile/internal/gc/order.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,10 @@ func ordercall(n *Node, order *Order) {
427427
// to make sure that all map assignments have the form m[k] = x.
428428
// (Note: orderexpr has already been called on n, so we know k is addressable.)
429429
//
430-
// If n is the multiple assignment form ..., m[k], ... = ..., the rewrite is
430+
// If n is the multiple assignment form ..., m[k], ... = ..., x, ..., the rewrite is
431431
// t1 = m
432432
// t2 = k
433-
// ...., t3, ... = x
433+
// ...., t3, ... = ..., x, ...
434434
// t1[t2] = t3
435435
//
436436
// The temporaries t1, t2 are needed in case the ... being assigned
@@ -444,6 +444,11 @@ func ordermapassign(n *Node, order *Order) {
444444
Fatalf("ordermapassign %v", n.Op)
445445

446446
case OAS:
447+
if n.Left.Op == OINDEXMAP {
448+
// Make sure we evaluate the RHS before starting the map insert.
449+
// We need to make sure the RHS won't panic. See issue 22881.
450+
n.Right = ordercheapexpr(n.Right, order)
451+
}
447452
order.out = append(order.out, n)
448453

449454
case OAS2, OAS2DOTTYPE, OAS2MAPR, OAS2FUNC:

test/fixedbugs/issue22881.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
// Test to make sure RHS is evaluated before map insert is started.
8+
// The RHS panics in all of these cases.
9+
10+
package main
11+
12+
import "fmt"
13+
14+
func main() {
15+
for i, f := range []func(map[int]int){
16+
f0, f1, f2, f3, f4, f5, f6, f7,
17+
} {
18+
m := map[int]int{}
19+
func() { // wrapper to scope the defer.
20+
defer func() {
21+
recover()
22+
}()
23+
f(m) // Will panic. Shouldn't modify m.
24+
fmt.Printf("RHS didn't panic, case f%d\n", i)
25+
}()
26+
if len(m) != 0 {
27+
fmt.Printf("map insert happened, case f%d\n", i)
28+
}
29+
}
30+
}
31+
32+
func f0(m map[int]int) {
33+
var p *int
34+
m[0] = *p
35+
}
36+
37+
func f1(m map[int]int) {
38+
var p *int
39+
m[0] += *p
40+
}
41+
42+
func f2(m map[int]int) {
43+
var p *int
44+
sink, m[0] = sink, *p
45+
}
46+
47+
func f3(m map[int]int) {
48+
var p *chan int
49+
m[0], sink = <-(*p)
50+
}
51+
52+
func f4(m map[int]int) {
53+
var p *interface{}
54+
m[0], sink = (*p).(int)
55+
}
56+
57+
func f5(m map[int]int) {
58+
var p *map[int]int
59+
m[0], sink = (*p)[0]
60+
}
61+
62+
func f6(m map[int]int) {
63+
var z int
64+
m[0] /= z
65+
}
66+
67+
func f7(m map[int]int) {
68+
var a []int
69+
m[0] = a[0]
70+
}
71+
72+
var sink bool

0 commit comments

Comments
 (0)