]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: use the zero value for results of impossible indexing
authorKeith Randall <khr@golang.org>
Tue, 31 Aug 2021 21:09:41 +0000 (14:09 -0700)
committerKeith Randall <khr@golang.org>
Tue, 31 Aug 2021 21:49:48 +0000 (21:49 +0000)
type A [0]int
var a A
x := a[i]

Use the zero value for x instead of the "impossible" value. That lets
us at least compile code like this with -B, even though it can't
possibly run correctly.

Fixes #48092

Change-Id: Idad5cfab49e05f375c069b05addceed68a15299f
Reviewed-on: https://go-review.googlesource.com/c/go/+/346589
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: David Chase <drchase@google.com>
TryBot-Result: Go Bot <gobot@golang.org>

src/cmd/compile/internal/ssagen/ssa.go
test/fixedbugs/issue48092.go [new file with mode: 0644]

index 77a350720fc75852a65645a6c097da71257f853d..a64901305fe72587b444ab574256e9a28658427a 100644 (file)
@@ -3014,7 +3014,8 @@ func (s *state) expr(n ir.Node) *ssa.Value {
                                        z := s.constInt(types.Types[types.TINT], 0)
                                        s.boundsCheck(z, z, ssa.BoundsIndex, false)
                                        // The return value won't be live, return junk.
-                                       return s.newValue0(ssa.OpUnknown, n.Type())
+                                       // But not quite junk, in case bounds checks are turned off. See issue 48092.
+                                       return s.zeroVal(n.Type())
                                }
                                len := s.constInt(types.Types[types.TINT], bound)
                                s.boundsCheck(i, len, ssa.BoundsIndex, n.Bounded()) // checks i == 0
diff --git a/test/fixedbugs/issue48092.go b/test/fixedbugs/issue48092.go
new file mode 100644 (file)
index 0000000..47b812c
--- /dev/null
@@ -0,0 +1,17 @@
+// compile -B
+
+// Copyright 2021 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 that we can at least compile this code
+// successfully with -B. We can't ever produce the right
+// answer at runtime with -B, as the access must panic.
+
+package p
+
+type A [0]byte
+
+func (a *A) Get(i int) byte {
+       return a[i]
+}