-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
a.go
305 lines (269 loc) · 5.5 KB
/
a.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package a
type X struct{ f, g int }
func f(x, y *X) {
if x == nil {
print(x.f) // want "nil dereference in field selection"
} else {
print(x.f)
}
if x == nil {
if nil != y {
print(1)
panic(0)
}
x.f = 1 // want "nil dereference in field selection"
y.f = 1 // want "nil dereference in field selection"
}
var f func()
if f == nil { // want "tautological condition: nil == nil"
go f() // want "nil dereference in dynamic function call"
} else {
// This block is unreachable,
// so we don't report an error for the
// nil dereference in the call.
defer f()
}
}
func f2(ptr *[3]int, i interface{}) {
if ptr != nil {
print(ptr[:])
*ptr = [3]int{}
print(*ptr)
} else {
print(ptr[:]) // want "nil dereference in slice operation"
*ptr = [3]int{} // want "nil dereference in store"
print(*ptr) // want "nil dereference in load"
if ptr != nil { // want "impossible condition: nil != nil"
// Dominated by ptr==nil and ptr!=nil,
// this block is unreachable.
// We do not report errors within it.
print(*ptr)
}
}
if i != nil {
print(i.(interface{ f() }))
} else {
print(i.(interface{ f() })) // want "nil dereference in type assertion"
}
}
func g() error { return nil }
func f3() error {
err := g()
if err != nil {
return err
}
if err != nil && err.Error() == "foo" { // want "impossible condition: nil != nil"
print(0)
}
ch := make(chan int)
if ch == nil { // want "impossible condition: non-nil == nil"
print(0)
}
if ch != nil { // want "tautological condition: non-nil != nil"
print(0)
}
return nil
}
func h(err error, b bool) {
if err != nil && b {
return
} else if err != nil {
panic(err)
}
}
func i(*int) error {
for {
if err := g(); err != nil {
return err
}
}
}
func f4(x *X) {
if x == nil {
panic(x)
}
}
func f5(x *X) {
panic(nil) // want "panic with nil value"
}
func f6(x *X) {
var err error
panic(err) // want "panic with nil value"
}
func f7() {
x, err := bad()
if err != nil {
panic(0)
}
if x == nil {
panic(err) // want "panic with nil value"
}
}
func bad() (*X, error) {
return nil, nil
}
func f8() {
var e error
v, _ := e.(interface{})
print(v)
}
func f9(x interface {
a()
b()
c()
}) {
x.b() // we don't catch this panic because we don't have any facts yet
xx := interface {
a()
b()
}(x)
if xx != nil {
return
}
x.c() // want "nil dereference in dynamic method call"
xx.b() // want "nil dereference in dynamic method call"
xxx := interface{ a() }(xx)
xxx.a() // want "nil dereference in dynamic method call"
if unknown() {
panic(x) // want "panic with nil value"
}
if unknown() {
panic(xx) // want "panic with nil value"
}
if unknown() {
panic(xxx) // want "panic with nil value"
}
}
func f10() {
s0 := make([]string, 0)
if s0 == nil { // want "impossible condition: non-nil == nil"
print(0)
}
var s1 []string
if s1 == nil { // want "tautological condition: nil == nil"
print(0)
}
s2 := s1[:][:]
if s2 == nil { // want "tautological condition: nil == nil"
print(0)
}
}
func unknown() bool {
return false
}
func f11(a interface{}) {
switch a.(type) {
case nil:
return
}
switch a.(type) {
case nil: // want "impossible condition: non-nil == nil"
return
}
}
func f12(a interface{}) {
switch a {
case nil:
return
}
switch a {
case 5,
nil: // want "impossible condition: non-nil == nil"
return
}
}
type Y struct {
innerY
}
type innerY struct {
value int
}
func f13() {
var d *Y
print(d.value) // want "nil dereference in field selection"
}
func f14() {
var x struct{ f string }
if x == struct{ f string }{} { // we don't catch this tautology as we restrict to reference types
print(x)
}
}
func f15(x any) {
ptr, ok := x.(*int)
if ok {
return
}
println(*ptr) // want "nil dereference in load"
}
func f16(x any) {
ptr, ok := x.(*int)
if !ok {
println(*ptr) // want "nil dereference in load"
return
}
println(*ptr)
}
func f18(x any) {
ptr, ok := x.(*int)
if ok {
println(ptr)
// falls through
}
println(*ptr)
}
// Regression test for https://github.com/golang/go/issues/65674:
// spurious "nil deference in slice index operation" when the
// index was subject to a range loop.
func f19(slice []int, array *[2]int, m map[string]int, ch chan int) {
if slice == nil {
// A range over a nil slice is dynamically benign,
// but still signifies a programmer mistake.
//
// Since SSA has melted down the control structure,
// so we can only report a diagnostic about the
// index operation, with heuristics for "range".
for range slice { // nothing to report here
}
for _, v := range slice { // want "range of nil slice"
_ = v
}
for i := range slice {
_ = slice[i] // want "range of nil slice"
}
{
var i int
for i = range slice {
}
_ = slice[i] // want "index of nil slice"
}
for i := range slice {
if i < len(slice) {
_ = slice[i] // want "range of nil slice"
}
}
if len(slice) > 3 {
_ = slice[2] // want "index of nil slice"
}
for i := 0; i < len(slice); i++ {
_ = slice[i] // want "index of nil slice"
}
}
if array == nil {
// (The v var is necessary, otherwise the SSA
// code doesn't dereference the pointer.)
for _, v := range array { // want "nil dereference in array index operation"
_ = v
}
}
if m == nil {
for range m { // want "range over nil map"
}
m["one"] = 1 // want "nil dereference in map update"
}
if ch == nil {
for range ch { // want "receive from nil channel"
}
<-ch // want "receive from nil channel"
ch <- 0 // want "send to nil channel"
}
}