1 // $G $D/$F.go && $L $F.$A &&
2 // ./$A.out -pass 0 >tmp.go && $G tmp.go && $L -o $A.out1 tmp.$A && ./$A.out1 &&
3 // ./$A.out -pass 1 >tmp.go && errchk $G -e tmp.go &&
4 // ./$A.out -pass 2 >tmp.go && errchk $G -e tmp.go
5 // rm -f tmp.go $A.out1
7 // Copyright 2010 The Go Authors. All rights reserved.
8 // Use of this source code is governed by a BSD-style
9 // license that can be found in the LICENSE file.
11 // Generate test of index and slice bounds checks.
12 // The output is compiled and run.
31 type quad struct { x, y, z, w int }
41 ci64bigger int64 = 1<<32
50 cni64big int64 = -1<<31
51 cni64bigger int64 = -1<<32
59 var i32 int32 = 100022
60 var i64 int64 = 100023
61 var i64big int64 = 1<<31
62 var i64bigger int64 = 1<<32
63 var huge uint64 = 1<<64 - 1
71 var ni64big int64 = -1<<31
72 var ni64bigger int64 = -1<<32
73 var nhuge int64 = -1<<63
75 var si []int = make([]int, 10)
77 var pai *[10]int = &ai
79 var sq []quad = make([]quad, 10)
81 var paq *[10]quad = &aq
83 var sib []int = make([]int, 100000)
85 var paib *[100000]int = &aib
87 var sqb []quad = make([]quad, 100000)
89 var paqb *[100000]quad = &aqb
107 var t = T{si, ai, pai, sq, aq, paq, sib, aib, paib, sqb, aqb, paqb}
109 var pt = &T{si, ai, pai, sq, aq, paq, sib, aib, paib, sqb, aqb, paqb}
111 // test that f panics
112 func test(f func(), s string) {
114 if err := recover(); err == nil {
115 _, file, line, _ := runtime.Caller(2)
117 print(file, ":", line, ": ", s, " did not panic\n")
118 } else if !contains(err.(error).Error(), "out of range") {
119 _, file, line, _ := runtime.Caller(2)
121 print(file, ":", line, ": ", s, " unexpected panic: ", err.(error).Error(), "\n")
127 func contains(x, y string) bool {
128 for i := 0; i+len(y) <= len(x); i++ {
129 if x[i:i+len(y)] == y {
138 func use(y interface{}) {
155 // 0 - dynamic checks
156 // 1 - static checks of invalid constants (cannot assign to types)
157 // 2 - static checks of array bounds
158 var pass = flag.Int("pass", 0, "which test (0,1,2)")
160 func testExpr(b *bufio.Writer, expr string) {
162 fmt.Fprintf(b, "\ttest(func(){use(%s)}, %q)\n", expr, expr)
164 fmt.Fprintf(b, "\tuse(%s) // ERROR \"index|overflow\"\n", expr)
169 b := bufio.NewWriter(os.Stdout)
174 fmt.Fprint(b, "// $G $D/$F.go && $L $F.$A && ./$A.out\n\n")
176 fmt.Fprint(b, "// errchk $G -e $D/$F.go\n\n")
178 fmt.Fprint(b, prolog)
180 var choices = [][]string{
181 // Direct value, fetch from struct, fetch from struct pointer.
182 // The last two cases get us to oindex_const_sudo in gsubr.c.
183 []string{"", "t.", "pt."},
185 // Array, pointer to array, slice.
186 []string{"a", "pa", "s"},
188 // Element is int, element is quad (struct).
189 // This controls whether we end up in gsubr.c (i) or cgen.c (q).
195 // Variable or constant.
198 // Positive or negative.
202 []string{"j", "i", "i8", "i16", "i32", "i64", "i64big", "i64bigger", "huge"},
205 forall(choices, func(x []string) {
206 p, a, e, big, c, n, i := x[0], x[1], x[2], x[3], x[4], x[5], x[6]
208 // Pass: dynamic=0, static=1, 2.
209 // Which cases should be caught statically?
210 // Only constants, obviously.
211 // Beyond that, must be one of these:
212 // indexing into array or pointer to array
216 if c == "c" && (a == "a" || a == "pa" || n == "n" || i == "i64big" || i == "i64bigger" || i == "huge") {
218 // Due to a detail of 6g's internals,
219 // the huge constant errors happen in an
220 // earlier pass than the others and inhibits
221 // the next pass from running.
222 // So run it as a separate check.
229 // If we're using the big-len data, positive int8 and int16 cannot overflow.
230 if big == "b" && n == "" && (i == "i8" || i == "i16") {
234 // Only print the test case if it is appropriate for this pass.
235 if thisPass == *pass {
240 testExpr(b, pae + "[" + cni + "]")
243 // Low index 0 is a special case in ggen.c
244 // so test both 0 and 1.
245 testExpr(b, pae + "[0:" + cni + "]")
246 testExpr(b, pae + "[1:" + cni + "]")
247 testExpr(b, pae + "[" + cni + ":]")
248 testExpr(b, pae + "[" + cni + ":" + cni + "]")
256 func forall(choices [][]string, f func([]string)) {
257 x := make([]string, len(choices))
259 var recurse func(d int)
260 recurse = func(d int) {
261 if d >= len(choices) {
265 for _, x[d] = range choices[d] {