OLD | NEW |
1 // [ $GOOS != nacl ] || exit 0 # do not bother on NaCl | 1 // [ $GOOS != nacl ] || exit 0 # do not bother on NaCl |
2 // $G $D/$F.go && $L $F.$A && | 2 // $G $D/$F.go && $L $F.$A && |
3 // ((! sh -c ./$A.out) >/dev/null 2>&1 || echo BUG: should fail) | 3 // ((! sh -c ./$A.out) >/dev/null 2>&1 || echo BUG: should fail) |
4 | 4 |
5 // Copyright 2009 The Go Authors. All rights reserved. | 5 // Copyright 2009 The Go Authors. All rights reserved. |
6 // Use of this source code is governed by a BSD-style | 6 // Use of this source code is governed by a BSD-style |
7 // license that can be found in the LICENSE file. | 7 // license that can be found in the LICENSE file. |
8 | 8 |
9 package main | 9 package main |
10 | 10 |
11 import "unsafe" | 11 import "unsafe" |
12 | 12 |
13 var dummy [512<<20]byte;» // give us a big address space | 13 var dummy [512<<20]byte»// give us a big address space |
14 func main() { | 14 func main() { |
15 // the test only tests what we intend to test | 15 // the test only tests what we intend to test |
16 // if dummy starts in the first 256 MB of memory. | 16 // if dummy starts in the first 256 MB of memory. |
17 // otherwise there might not be anything mapped | 17 // otherwise there might not be anything mapped |
18 // at the address that might be accidentally | 18 // at the address that might be accidentally |
19 // dereferenced below. | 19 // dereferenced below. |
20 if uintptr(unsafe.Pointer(&dummy)) > 256<<20 { | 20 if uintptr(unsafe.Pointer(&dummy)) > 256<<20 { |
21 » » panic("dummy too far out"); | 21 » » panic("dummy too far out") |
22 } | 22 } |
23 | 23 |
24 // The problem here is that indexing into p[] with a large | 24 // The problem here is that indexing into p[] with a large |
25 // enough index can jump out of the unmapped section | 25 // enough index can jump out of the unmapped section |
26 // at the beginning of memory and into valid memory. | 26 // at the beginning of memory and into valid memory. |
27 // | 27 // |
28 // To avoid needing a check on every slice beyond the | 28 // To avoid needing a check on every slice beyond the |
29 // usual len and cap, we require the slice operation | 29 // usual len and cap, we require the slice operation |
30 // to do the check. | 30 // to do the check. |
31 » var p *[1<<30]byte = nil; | 31 » var p *[1<<30]byte = nil |
32 » var _ []byte = p[10:len(p)-10];»// should crash | 32 » var _ []byte = p[10:len(p)-10]» // should crash |
33 } | 33 } |
OLD | NEW |