Skip to content

Commit 7106475

Browse files
Merge pull request #19411 from joefarebrother/python-qual-file-not-closed
Python: Improve performance of FileNotClosed query by using an explicit fastTC
2 parents 2c95f00 + acb9c20 commit 7106475

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

python/ql/src/Resources/FileNotAlwaysClosedQuery.qll

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,29 @@ class FileWrapperCall extends DataFlow::CallCfgNode {
5252
abstract class FileClose extends DataFlow::CfgNode {
5353
/** Holds if this file close will occur if an exception is thrown at `raises`. */
5454
predicate guardsExceptions(DataFlow::CfgNode raises) {
55-
this.asCfgNode() = raises.asCfgNode().getAnExceptionalSuccessor().getASuccessor*()
55+
cfgGetASuccessorStar(raises.asCfgNode().getAnExceptionalSuccessor(), this.asCfgNode())
5656
or
5757
// The expression is after the close call.
5858
// This also covers the body of a `with` statement.
59-
raises.asCfgNode() = this.asCfgNode().getASuccessor*()
59+
cfgGetASuccessorStar(this.asCfgNode(), raises.asCfgNode())
6060
}
6161
}
6262

63+
private predicate cfgGetASuccessor(ControlFlowNode src, ControlFlowNode sink) {
64+
sink = src.getASuccessor()
65+
}
66+
67+
pragma[inline]
68+
private predicate cfgGetASuccessorPlus(ControlFlowNode src, ControlFlowNode sink) =
69+
fastTC(cfgGetASuccessor/2)(src, sink)
70+
71+
pragma[inline]
72+
private predicate cfgGetASuccessorStar(ControlFlowNode src, ControlFlowNode sink) {
73+
src = sink
74+
or
75+
cfgGetASuccessorPlus(src, sink)
76+
}
77+
6378
/** A call to the `.close()` method of a file object. */
6479
class FileCloseCall extends FileClose {
6580
FileCloseCall() { exists(DataFlow::MethodCallNode mc | mc.calls(this, "close")) }

0 commit comments

Comments
 (0)