forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUninitializedLocal.py
More file actions
290 lines (240 loc) · 4.5 KB
/
UninitializedLocal.py
File metadata and controls
290 lines (240 loc) · 4.5 KB
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
class C:
def m1(self):
y = ug1
x = 1
return y
def m2(self, p):
return p
def m3(self, x1):
return u2
u2 = x1
def m4(self, x2):
if x2:
u3 = 1
return u3
def f():
y = ug1
x = 1
return y
def g(x3):
def h():
y = x3
def q(x4):
def h():
y = x4
x = 1
def j(u4):
del u4
return u4
def k(x5):
x5 + 1
del x5
def m(x6):
if x6:
u6 = 1
u6
#The following are not uninitialized, but unreachable.
u6
u6
#ODASA-1897
def l(x7):
try:
if f():
raise Exception
mod_name = x7[:-3]
mod = __import__(mod_name)
except ImportError:
raise ValueError(mod_name)
def check_del(cond):
u8 = 1
if cond:
del u8
else:
pass
u8
if cond:
u9 = 1
del u9
else:
u9 = 2
u9
if cond:
x10 = 1
del x10
x10 = 2
else:
x10 = 3
x10
u11 = 1
del u11
u11
u12 = "hi"
del u12
del u12
#x will always be defined.
def const_range():
for i in range(4):
x = i
return x
def asserts_false(cond1, cond2):
if cond1:
x13 = 1
elif cond2:
x13 = 2
else:
assert False, "Can't happen"
return x13
#Splitting
def use_def_conditional(cond3):
if cond3:
x14 = 1
x15 = 2
if cond3:
return x14
def use_def_conditional(cond4, cond5):
if cond4:
u14 = 1
x16 = 2
if cond5:
return u14
def init_and_set_flag_in_try(f):
try:
f()
x17 = 1
success = True
except:
success = False
if success:
return x17
#Check that we can rely on splitting
def split_OK():
try:
f()
x18 = 1
cond = not True
except:
cond = not False
if not cond:
return x18
def split_not_OK():
try:
f()
u19 = 1
cond = not True
except:
cond = not False
if not not cond:
return u19
def double_is_none(x):
if x is not None:
v = 0
if x is None:
return 0
else:
return v
#ODASA-4241
def def_in_post_loop(seq):
j(x)
x = []
for p in seq:
x = p
#Check that we are consistent with both sides of if statement
#ODASA-4615
def f(cond1, cond2):
if cond1:
x = 1
else:
y = 1
if cond2:
return x
else:
return y
def needs_splitting(var):
if var:
other = 0
if not var or other: #other looks like it might be undefined, but it is defined.
pass
def odasa4867(status):
fail = (status != 200)
if not fail:
1
if not fail:
var = 2
if not fail:
3
if not fail and not l(var): # Observed FP - var is defined.
4
fail = True # It is possible that this was interfering with splitting.
if not fail: # Not the same SSA variable as earlier
5
def odasa5896(number):
guesses_made = 0
while guesses_made < 6: # This loop is guaranteed to execute at least once.
guess = int(input('Take a guess: '))
guesses_made += 1
if guess == number: # FP here
pass
#ODASA 6212
class C(object):
def fail(self):
raise Exception()
def may_fail(cond, c):
if cond:
x = 0
else:
c.fail()
return x
def deliberate_name_error(cond):
if cond:
x = 0
try:
x # x is uninitialised, but guarded. So don't report it.
except NameError:
x = 1
return x # x is initialised here, but we would need splitting to know that.
from unknown import x
may_fail(x, C())
def with_definition(x):
with open(x) as y:
y
def multiple_defn_in_try(x):
try:
for p, q in x:
p
except KeyError:
pass
# ODASA-6742
import sys
class X(object):
def leave(self, code = 1):
sys.exit(code)
class Y(object):
def __init__(self, x):
self._x = x
def leave(self):
self._x.leave()
def odasa6742(cond, obj):
y = Y(X())
try:
var = may_fail(cond, obj)
except:
y.leave()
var
#ODASA-6904
def avoid_redundant_split(a):
if a: # Should split here
b = x()
else:
b = None
if b: # but not here,
pass
if b: # or here, because
pass
pass
try: # we want to split here
import foo
var = True
except:
var = False
if var:
foo.bar() #foo is defined here.