Skip to content

Commit 973befe

Browse files
cuonglmgopherbot
authored andcommitted
cmd/compile: check ODEREF for safe lhs in assignment during static init
For #66585 Change-Id: Iddc407e3ef4c3b6ecf5173963b66b3e65e43c92d Reviewed-on: https://go-review.googlesource.com/c/go/+/575336 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Keith Randall <[email protected]> Auto-Submit: Cuong Manh Le <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 94dba61 commit 973befe

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/cmd/compile/internal/staticinit/sched.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,13 @@ func mayModifyPkgVar(n ir.Node) bool {
881881
// safeLHS reports whether the assigned-to variable lhs is either a
882882
// local variable or a global from another package.
883883
safeLHS := func(lhs ir.Node) bool {
884-
v, ok := ir.OuterValue(lhs).(*ir.Name)
884+
outer := ir.OuterValue(lhs)
885+
// "*p = ..." should be safe if p is a local variable.
886+
// TODO: Should ir.OuterValue handle this?
887+
for outer.Op() == ir.ODEREF {
888+
outer = outer.(*ir.StarExpr).X
889+
}
890+
v, ok := outer.(*ir.Name)
885891
return ok && v.Op() == ir.ONAME && !(v.Class == ir.PEXTERN && v.Sym().Pkg == types.LocalPkg)
886892
}
887893

test/codegen/issue66585.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// asmcheck
2+
3+
// Copyright 2024 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 p
8+
9+
var x = func() int {
10+
n := 0
11+
f(&n)
12+
return n
13+
}()
14+
15+
func f(p *int) {
16+
*p = 1
17+
}
18+
19+
var y = 1
20+
21+
// z can be static initialized.
22+
//
23+
// amd64:-"MOVQ"
24+
var z = y

0 commit comments

Comments
 (0)