Skip to content

Commit 294092b

Browse files
committed
Python: use comprehension function argument
For a comprehension `[x for x in l] - `l` is now a legal argument (in DataFlowPublic) - `l` is the argument of the comprehension function (in DataFlowDispatch) - the parameter of the comprehension function is being read rather than `l` (in IterableUnpacking) Thus the read that used to cross callable boundaries is now split into a arg-param edge and a read from that param.
1 parent fc2dc28 commit 294092b

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

python/ql/lib/semmle/python/dataflow/new/internal/DataFlowDispatch.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,10 @@ class ComprehensionCall extends ExtractedDataFlowCall, TComprehensionCall {
14911491

14921492
override DataFlowCallable getCallable() { result.(DataFlowFunction).getScope() = target }
14931493

1494-
override ArgumentNode getArgument(ArgumentPosition apos) { none() }
1494+
override ArgumentNode getArgument(ArgumentPosition apos) {
1495+
result.asExpr() = c.getIterable() and
1496+
apos.isPositional(0)
1497+
}
14951498

14961499
override Location getLocation() { result = c.getLocation() }
14971500
}

python/ql/lib/semmle/python/dataflow/new/internal/DataFlowPublic.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ class ExtractedArgumentNode extends ArgumentNode {
350350
or
351351
// and self arguments
352352
this.asCfgNode() = any(CallNode c).getFunction().(AttrNode).getObject()
353+
or
354+
// for comprehensions, we allow the synthetic `iterable` argument
355+
this.asExpr() = any(Comp c).getIterable()
353356
}
354357

355358
final override predicate argumentOf(DataFlowCall call, ArgumentPosition pos) {

python/ql/lib/semmle/python/dataflow/new/internal/IterableUnpacking.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class ForTarget extends ControlFlowNode {
187187
)
188188
or
189189
exists(Comp comp |
190-
source = comp.getIterable() and
190+
source = comp.getFunction().getArg(0) and
191191
this.getNode() = comp.getNthInnerLoop(0).getTarget()
192192
)
193193
}

0 commit comments

Comments
 (0)