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

« back to all changes in this revision

Viewing changes to test/nilptr/structfieldaddr.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
type T struct {
 
14
        x [256<<20] byte
 
15
        i int
 
16
}
 
17
 
 
18
func main() {
 
19
        // the test only tests what we intend to test
 
20
        // if dummy starts in the first 256 MB of memory.
 
21
        // otherwise there might not be anything mapped
 
22
        // at the address that might be accidentally
 
23
        // dereferenced below.
 
24
        if uintptr(unsafe.Pointer(&dummy)) > 256<<20 {
 
25
                panic("dummy too far out")
 
26
        }
 
27
 
 
28
        // The problem here is that indexing into t with a large
 
29
        // enough index can jump out of the unmapped section
 
30
        // at the beginning of memory and into valid memory.
 
31
        // We require the address calculation to check.
 
32
        var t *T
 
33
        println(&t.i)   // should crash
 
34
}