File tree Expand file tree Collapse file tree 1 file changed +6
-0
lines changed
Expand file tree Collapse file tree 1 file changed +6
-0
lines changed Original file line number Diff line number Diff line change 1+ /** Contains predicates concerning when and where files are opened and closed. */
2+
13import python
24import semmle.python.GuardedControlFlow
35import 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. */
116119predicate 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` */
122126predicate 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. */
129134predicate 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. */
143149predicate file_is_returned ( EssaVariable v , ControlFlowNode open ) {
144150 exists ( NameNode n , Return ret |
145151 var_is_open ( v , open ) and
You can’t perform that action at this time.
0 commit comments