Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions python/ql/src/Resources/FileNotAlwaysClosedQuery.qll
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,29 @@ class FileWrapperCall extends DataFlow::CallCfgNode {
abstract class FileClose extends DataFlow::CfgNode {
/** Holds if this file close will occur if an exception is thrown at `raises`. */
predicate guardsExceptions(DataFlow::CfgNode raises) {
this.asCfgNode() = raises.asCfgNode().getAnExceptionalSuccessor().getASuccessor*()
cfgGetASuccessorStar(raises.asCfgNode().getAnExceptionalSuccessor(), this.asCfgNode())
or
// The expression is after the close call.
// This also covers the body of a `with` statement.
raises.asCfgNode() = this.asCfgNode().getASuccessor*()
cfgGetASuccessorStar(this.asCfgNode(), raises.asCfgNode())
}
}

private predicate cfgGetASuccessor(ControlFlowNode src, ControlFlowNode sink) {
sink = src.getASuccessor()
}

pragma[inline]
private predicate cfgGetASuccessorPlus(ControlFlowNode src, ControlFlowNode sink) =
fastTC(cfgGetASuccessor/2)(src, sink)

pragma[inline]
private predicate cfgGetASuccessorStar(ControlFlowNode src, ControlFlowNode sink) {
Copy link

Copilot AI Apr 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] It may be helpful to add documentation clarifying the base condition 'src = sink' in cfgGetASuccessorStar to outline its role in implementing the transitive closure.

Suggested change
private predicate cfgGetASuccessorStar(ControlFlowNode src, ControlFlowNode sink) {
private predicate cfgGetASuccessorStar(ControlFlowNode src, ControlFlowNode sink) {
// Base condition: A node is always reachable from itself, ensuring reflexivity.

Copilot uses AI. Check for mistakes.
src = sink
or
cfgGetASuccessorPlus(src, sink)
}

/** A call to the `.close()` method of a file object. */
class FileCloseCall extends FileClose {
FileCloseCall() { exists(DataFlow::MethodCallNode mc | mc.calls(this, "close")) }
Expand Down
Loading