aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Chase <drchase@google.com>2016-12-23 12:00:07 -0500
committerBrad Fitzpatrick <bradfitz@golang.org>2017-01-25 17:17:21 +0000
commit93468efeb7968071f490656eada6db633f8ffe7b (patch)
tree51d99b42c4557cd2b6a293315a67a2d4aa36b4fa
parentf5dd484480276f5d848a8f43b8b451429526b220 (diff)
downloadgo-93468efeb7968071f490656eada6db633f8ffe7b.tar.gz
go-93468efeb7968071f490656eada6db633f8ffe7b.zip
[release-branch.go1.7] cmd/compile: rewrite literal.method to ensure full initialization
CALLPART of STRUCTLIT did not check for incomplete initialization of struct; modify PTRLIT treatment to force zeroing. Test for structlit, believe this might have also failed for arraylit. Fixes #18410. Change-Id: I511abf8ef850e300996d40568944665714efe1fc Reviewed-on: https://go-review.googlesource.com/34622 Run-TryBot: David Chase <drchase@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-on: https://go-review.googlesource.com/35636 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
-rw-r--r--src/cmd/compile/internal/gc/sinit.go2
-rw-r--r--test/fixedbugs/issue18410.go40
2 files changed, 42 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/gc/sinit.go b/src/cmd/compile/internal/gc/sinit.go
index 4469d71f1c..7d006ff739 100644
--- a/src/cmd/compile/internal/gc/sinit.go
+++ b/src/cmd/compile/internal/gc/sinit.go
@@ -1042,6 +1042,8 @@ func anylit(ctxt int, n *Node, var_ *Node, init *Nodes) {
var r *Node
if n.Right != nil {
+ // n.Right is stack temporary used as backing store.
+ init.Append(Nod(OAS, n.Right, nil)) // zero backing store, just in case (#18410)
r = Nod(OADDR, n.Right, nil)
r = typecheck(r, Erv)
} else {
diff --git a/test/fixedbugs/issue18410.go b/test/fixedbugs/issue18410.go
new file mode 100644
index 0000000000..e9c6f862eb
--- /dev/null
+++ b/test/fixedbugs/issue18410.go
@@ -0,0 +1,40 @@
+// run
+
+// Copyright 2016 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.
+
+// This checks partially initialized structure literals
+// used to create value.method functions have their
+// non-initialized fields properly zeroed/nil'd
+
+package main
+
+type X struct {
+ A, B, C *int
+}
+
+//go:noinline
+func (t X) Print() {
+ if t.B != nil {
+ panic("t.B must be nil")
+ }
+}
+
+//go:noinline
+func caller(f func()) {
+ f()
+}
+
+//go:noinline
+func test() {
+ var i, j int
+ x := X{A: &i, C: &j}
+ caller(func() { X{A: &i, C: &j}.Print() })
+ caller(X{A: &i, C: &j}.Print)
+ caller(x.Print)
+}
+
+func main() {
+ test()
+}