]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: rewrite literal.method to ensure full initialization
authorDavid Chase <drchase@google.com>
Fri, 23 Dec 2016 17:00:07 +0000 (12:00 -0500)
committerDavid Chase <drchase@google.com>
Fri, 6 Jan 2017 20:35:52 +0000 (20:35 +0000)
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>
src/cmd/compile/internal/gc/sinit.go
test/fixedbugs/issue18410.go [new file with mode: 0644]

index 350c8677259a1e009df6b3d4829dab6b1d5a4ec9..89eec60216ea264eb766a55de6b4c076d7d10382 100644 (file)
@@ -1078,6 +1078,8 @@ func anylit(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 (file)
index 0000000..e9c6f86
--- /dev/null
@@ -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()
+}