Source file
test/nilptr.go
1
2
3
4
5
6
7
8
9
10
11
12
13 package main
14
15 import "unsafe"
16
17
18
19
20
21
22 var dummy [256 << 20]byte
23
24 func main() {
25
26
27
28
29
30 if uintptr(unsafe.Pointer(&dummy)) > 256<<20 {
31 panic("dummy too far out")
32 }
33
34 shouldPanic(p1)
35 shouldPanic(p2)
36 shouldPanic(p3)
37 shouldPanic(p4)
38 shouldPanic(p5)
39 shouldPanic(p6)
40 shouldPanic(p7)
41 shouldPanic(p8)
42 shouldPanic(p9)
43 shouldPanic(p10)
44 shouldPanic(p11)
45 shouldPanic(p12)
46 shouldPanic(p13)
47 shouldPanic(p14)
48 shouldPanic(p15)
49 shouldPanic(p16)
50 }
51
52 func shouldPanic(f func()) {
53 defer func() {
54 if recover() == nil {
55 panic("memory reference did not panic")
56 }
57 }()
58 f()
59 }
60
61 func p1() {
62
63 var p *[1 << 30]byte = nil
64 println(p[256<<20])
65 }
66
67 var xb byte
68
69 func p2() {
70 var p *[1 << 30]byte = nil
71 xb = 123
72
73
74 println(p[uintptr(unsafe.Pointer(&xb))])
75 }
76
77 func p3() {
78
79 var p *[1 << 30]byte = nil
80 var x []byte = p[0:]
81 _ = x
82 }
83
84 var q *[1 << 30]byte
85
86 func p4() {
87
88 var x []byte
89 var y = &x
90 *y = q[0:]
91 }
92
93 func fb([]byte) {
94 panic("unreachable")
95 }
96
97 func p5() {
98
99 var p *[1 << 30]byte = nil
100 fb(p[0:])
101 }
102
103 func p6() {
104
105 var p *[1 << 30]byte = nil
106 var _ []byte = p[10 : len(p)-10]
107 }
108
109 type T struct {
110 x [256 << 20]byte
111 i int
112 }
113
114 func f() *T {
115 return nil
116 }
117
118 var y *T
119 var x = &y
120
121 func p7() {
122
123 println(f().i)
124 }
125
126 func p8() {
127
128 println((*x).i)
129 }
130
131 func p9() {
132
133 var t *T
134 println(&t.i)
135 }
136
137 func p10() {
138
139 var t *T
140 println(t.i)
141 }
142
143 type T1 struct {
144 T
145 }
146
147 type T2 struct {
148 *T1
149 }
150
151 func p11() {
152 t := &T2{}
153 p := &t.i
154 println(*p)
155 }
156
157
158 func p12() {
159 var p *T = nil
160 println(*(&((*p).i)))
161 }
162
163
164
165 func p13() {
166 var x *[10]int
167 y := x[:]
168 _ = y
169 }
170
171 func p14() {
172 println((*[1]int)(nil)[:])
173 }
174
175 func p15() {
176 for i := range (*[1]int)(nil)[:] {
177 _ = i
178 }
179 }
180
181 func p16() {
182 for i, v := range (*[1]int)(nil)[:] {
183 _ = i + v
184 }
185 }
186
View as plain text