Skip to content

Commit ccf63e0

Browse files
committed
Python: Document FileOpen.qll.
1 parent f86011f commit ccf63e0

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

python/ql/src/Resources/FileOpen.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/** Contains predicates concerning when and where files are opened and closed. */
2+
13
import python
24
import semmle.python.GuardedControlFlow
35
import semmle.python.pointsto.Filters
@@ -113,19 +115,22 @@ predicate close_method_call(CallNode call, ControlFlowNode self) {
113115
call.getFunction().(AttrNode).getObject("close") = self
114116
}
115117

118+
/** Holds if `close` is a function that appears to close files that are passed to it as an argument. */
116119
predicate function_closes_file(FunctionValue close) {
117120
close = Value::named("os.close")
118121
or
119122
function_should_close_parameter(close.getScope())
120123
}
121124

125+
/** INTERNAL - Helper predicate for `function_closes_file` */
122126
predicate function_should_close_parameter(Function func) {
123127
exists(EssaDefinition def |
124128
closes_file(def) and
125129
def.getSourceVariable().(Variable).getScope() = func
126130
)
127131
}
128132

133+
/** Holds if `f` opens a file, either directly or indirectly. */
129134
predicate function_opens_file(FunctionValue f) {
130135
f = Value::named("open")
131136
or
@@ -140,6 +145,7 @@ predicate function_opens_file(FunctionValue f) {
140145
)
141146
}
142147

148+
/** Holds if `v` refers to a file opened at `open` which is subsequently returned from a function. */
143149
predicate file_is_returned(EssaVariable v, ControlFlowNode open) {
144150
exists(NameNode n, Return ret |
145151
var_is_open(v, open) and

0 commit comments

Comments
 (0)