Skip to content

Commit 0b293ea

Browse files
Update test output
1 parent bd3fa7f commit 0b293ea

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

python/ql/test/query-tests/Resources/FileNotAlwaysClosed/FileNotAlwaysClosed.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
| resources_test.py:112:11:112:28 | ControlFlowNode for opener_func2() | File may not be closed if $@ raises an exception. | resources_test.py:113:5:113:22 | ControlFlowNode for Attribute() | this operation |
55
| resources_test.py:123:11:123:24 | ControlFlowNode for opener_func2() | File is opened but is not closed. | file://:0:0:0:0 | (none) | this operation |
66
| resources_test.py:129:15:129:24 | ControlFlowNode for open() | File may not be closed if $@ raises an exception. | resources_test.py:130:9:130:26 | ControlFlowNode for Attribute() | this operation |
7+
| resources_test.py:154:15:154:24 | ControlFlowNode for open() | File may not be closed if $@ raises an exception. | resources_test.py:159:9:159:18 | ControlFlowNode for Attribute() | this operation |
78
| resources_test.py:248:11:248:25 | ControlFlowNode for open() | File is opened but is not closed. | file://:0:0:0:0 | (none) | this operation |
89
| resources_test.py:269:10:269:27 | ControlFlowNode for Attribute() | File may not be closed if $@ raises an exception. | resources_test.py:271:5:271:19 | ControlFlowNode for Attribute() | this operation |
910
| resources_test.py:285:11:285:20 | ControlFlowNode for open() | File may not be closed if $@ raises an exception. | resources_test.py:287:5:287:31 | ControlFlowNode for Attribute() | this operation |
10-
| resources_test.py:305:10:305:19 | ControlFlowNode for open() | File may not be closed if $@ raises an exception. | resources_test.py:308:5:308:24 | ControlFlowNode for Attribute() | this operation |

python/ql/test/query-tests/Resources/FileNotAlwaysClosed/resources_test.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ def not_closed17():
151151
#With statement will close the fp
152152
def closed18(path):
153153
try:
154-
f18 = open(path)
154+
f18 = open(path) # $SPURIOUS: Alert # Dataflow appears to not detect this with statement as guarding the exceptions produced by the `read()` call.
155155
except IOError as ex:
156156
print(ex)
157157
raise ex
158-
with f18:
158+
with f18:
159159
f18.read()
160160

161161
class Closed19(object):
@@ -286,7 +286,7 @@ def closed29(path):
286286
f28.close()
287287
f28.write("already closed")
288288

289-
# False positive:
289+
# False positive in a previous implementation:
290290

291291
class NotWrapper:
292292
def __init__(self, fp):
@@ -297,12 +297,13 @@ def do_something():
297297
pass
298298

299299
def closed30(path):
300-
# Combination of approximations resulting in this FP:
300+
# Combination of approximations resulted in this FP:
301301
# - NotWrapper is treated as a wrapper class as a file handle is passed to it
302302
# - thing.do_something() is treated as a call that can raise an exception while a file is open
303303
# - this call is treated as occurring after the open but not as being guarded by the with statement, as it is in the same basic block
304+
# - - this behaviour has been changed fixing the FP
304305

305-
with open(path) as fp: # $SPURIOUS:Alert # not closed on exception
306+
with open(path) as fp: # No longer spurious alert here.
306307
thing = NotWrapper(fp)
307308

308309
thing.do_something()
@@ -314,10 +315,20 @@ def closed31(path):
314315
data2 = fp.readline()
315316

316317

317-
class FlowReader():
318+
class Wrapper():
318319
def __init__(self, f):
320+
self.f = f
321+
def read(self):
322+
return self.f.read()
323+
def __enter__(self):
319324
pass
320-
def test_cannot_convert(tdata):
321-
with open(tdata, "rb") as f:
322-
flow_reader = FlowReader(f)
323-
list(flow_reader.stream())
325+
def __exit__(self):
326+
self.f.close()
327+
328+
def closed32(path):
329+
with open(path, "rb") as f: # No longer spurious alert here.
330+
wrap = Wrapper(f)
331+
# This resulted in an FP in a previous implementation,
332+
# due to a check that an operation is lexically contained within a `with` block (with `expr.getParent*()`)
333+
# not detecting this case.
334+
return list(wrap.read())

0 commit comments

Comments
 (0)