Skip to content

Commit 5e60551

Browse files
committed
Python: limit capturing scopes
1 parent 08ae04c commit 5e60551

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -417,18 +417,18 @@ module VariableCapture {
417417
Function f;
418418

419419
CapturedVariable() {
420-
// exists(this.getScopeEntryDefinition())
421420
this.getScope() = f and
422-
(
423-
this.getALoad().getScope() != f
424-
or
425-
this.getAStore().getScope() != f
426-
)
421+
this.getAnAccess().getScope() != f
427422
}
428423

429424
Callable getCallable() { result = f }
430425

431426
Location getLocation() { result = f.getLocation() }
427+
428+
Scope getACapturingScope() {
429+
result = this.getAnAccess().getScope().getScope*() and
430+
result.getScope+() = f
431+
}
432432
}
433433

434434
class CapturedParameter extends CapturedVariable {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ newtype TNode =
119119
/** A synthetic node representing a captured variable. */
120120
TCaptureNode(VariableCapture::Flow::SynthesizedCaptureNode cn) or
121121
/** TODO: extende to also cover lambdas */
122-
TLambdaSelfReferenceNode(Function f)
122+
TLambdaSelfReferenceNode(Function f) {
123+
f = any(VariableCapture::CapturedVariable v).getACapturingScope()
124+
}
123125

124126
private import semmle.python.internal.CachedStages
125127

0 commit comments

Comments
 (0)