]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/gc: reject invalid unsafe.Sizeof([0]byte{}[0])
authorMatthew Dempsky <mdempsky@google.com>
Thu, 15 Oct 2015 18:29:06 +0000 (11:29 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Thu, 15 Oct 2015 20:56:58 +0000 (20:56 +0000)
Apply static bounds checking logic during type checking even to
zero-element arrays, but skip synthesized OINDEX nodes that the
compiler has asserted are within bounds (such as the ones generated
while desugaring ORANGE nodes).  This matches the logic in walkexpr
that also skips static bounds checking when Bounded is true.

Passes toolstash/buildall.

Fixes #12944.

Change-Id: I14ba03d71c002bf969d69783bec8d1a8e10e7d75
Reviewed-on: https://go-review.googlesource.com/15902
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/cmd/compile/internal/gc/typecheck.go
test/fixedbugs/issue12944.go [new file with mode: 0644]

index 8be29f0923f10572e426015772586ff92b956b90..f30d07179824acc574807df5b1e812c913487dd1 100644 (file)
@@ -1026,11 +1026,11 @@ OpSwitch:
                                break
                        }
 
-                       if Isconst(n.Right, CTINT) {
+                       if !n.Bounded && Isconst(n.Right, CTINT) {
                                x := Mpgetfix(n.Right.Val().U.(*Mpint))
                                if x < 0 {
                                        Yyerror("invalid %s index %v (index must be non-negative)", why, n.Right)
-                               } else if Isfixedarray(t) && t.Bound > 0 && x >= t.Bound {
+                               } else if Isfixedarray(t) && x >= t.Bound {
                                        Yyerror("invalid array index %v (out of bounds for %d-element array)", n.Right, t.Bound)
                                } else if Isconst(n.Left, CTSTR) && x >= int64(len(n.Left.Val().U.(string))) {
                                        Yyerror("invalid string index %v (out of bounds for %d-byte string)", n.Right, len(n.Left.Val().U.(string)))
diff --git a/test/fixedbugs/issue12944.go b/test/fixedbugs/issue12944.go
new file mode 100644 (file)
index 0000000..59379f1
--- /dev/null
@@ -0,0 +1,13 @@
+// errorcheck
+
+// Copyright 2015 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.
+
+package main
+
+import "unsafe"
+
+const (
+       _ = unsafe.Sizeof([0]byte{}[0]) // ERROR "out of bounds"
+)