-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathLoopVariableCapture.ql
More file actions
69 lines (58 loc) · 2.19 KB
/
LoopVariableCapture.ql
File metadata and controls
69 lines (58 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @name Loop variable capture
* @description Capture of a loop variable is not the same as capturing the value of a loop variable, and may be erroneous.
* @kind problem
* @tags correctness
* @problem.severity error
* @sub-severity low
* @precision high
* @id py/loop-variable-capture
*/
import python
import semmle.python.dataflow.new.DataFlow
abstract class Loop extends AstNode {
abstract Variable getALoopVariable();
}
class ForLoop extends Loop, For {
override Variable getALoopVariable() {
this.getTarget() = result.getAnAccess().getParentNode*() and
result.getScope() = this.getScope()
}
}
predicate capturesLoopVariable(CallableExpr capturing, Loop loop, Variable var) {
var.getAnAccess().getScope() = capturing.getInnerScope() and
capturing.getParentNode+() = loop and
var = loop.getALoopVariable()
}
module EscapingCaptureFlowSig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node node) { capturesLoopVariable(node.asExpr(), _, _) }
predicate isSink(DataFlow::Node node) {
// Stored in a field.
exists(DataFlow::AttrWrite aw | aw.getObject() = node)
or
// Stored in a dict/list.
exists(Assign assign, Subscript sub |
sub = assign.getATarget() and node.asExpr() = assign.getValue()
)
or
// Stored in a list.
exists(DataFlow::MethodCallNode mc | mc.calls(_, "append") and node = mc.getArg(0))
or
// Used in a yeild statement, likely included in a collection.
// The element of comprehension expressions desugar to involve a yield statement internally.
exists(Yield y | node.asExpr() = y.getValue())
}
predicate isBarrierOut(DataFlow::Node node) { isSink(node) }
predicate allowImplicitRead(DataFlow::Node node, DataFlow::ContentSet cs) {
isSink(node) and
exists(cs)
}
}
module EscapingCaptureFlow = DataFlow::Global<EscapingCaptureFlowSig>;
predicate escapingCapture(CallableExpr capturing, Loop loop, Variable var) {
capturesLoopVariable(capturing, loop, var) and
EscapingCaptureFlow::flow(DataFlow::exprNode(capturing), _)
}
from CallableExpr capturing, AstNode loop, Variable var
where escapingCapture(capturing, loop, var)
select capturing, "Capture of loop variable $@.", loop, var.getId()