~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to test/nilptr/arrayindex1.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-04-20 17:36:48 UTC
  • Revision ID: james.westby@ubuntu.com-20110420173648-ifergoxyrm832trd
Tags: upstream-2011.03.07.1
Import upstream version 2011.03.07.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// $G $D/$F.go && $L $F.$A &&
 
2
//      ((! sh -c ./$A.out) >/dev/null 2>&1 || echo BUG: should fail)
 
3
 
 
4
// Copyright 2009 The Go Authors. All rights reserved.
 
5
// Use of this source code is governed by a BSD-style
 
6
// license that can be found in the LICENSE file.
 
7
 
 
8
package main
 
9
 
 
10
import "unsafe"
 
11
 
 
12
var dummy [512<<20]byte // give us a big address space
 
13
func main() {
 
14
        // the test only tests what we intend to test
 
15
        // if dummy starts in the first 256 MB of memory.
 
16
        // otherwise there might not be anything mapped
 
17
        // at the address that might be accidentally
 
18
        // dereferenced below.
 
19
        if uintptr(unsafe.Pointer(&dummy)) > 256<<20 {
 
20
                panic("dummy too far out")
 
21
        }
 
22
 
 
23
        // The problem here is that indexing into p[] with a large
 
24
        // enough index jumps out of the unmapped section
 
25
        // at the beginning of memory and into valid memory.
 
26
        // Pointer offsets and array indices, if they are
 
27
        // very large, need to dereference the base pointer
 
28
        // to trigger a trap.
 
29
        var p *[1<<30]byte = nil
 
30
        println(p[256<<20])     // very likely to be inside dummy, but should crash
 
31
}