Skip to content

Commit 118840d

Browse files
authored
Merge pull request github#5690 from tausbn/python-disallow-post-update-nodes-as-local-source-nodes
Python: Disallow `PostUpdateNode` as `LocalSourceNode`
2 parents 5c2bf68 + 92b4eb7 commit 118840d

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

python/ql/src/semmle/python/dataflow/new/internal/LocalSources.qll

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,34 @@ private predicate comes_from_cfgnode(Node node) {
2121
* A data flow node that is a source of local flow. This includes things like
2222
* - Expressions
2323
* - Function parameters
24+
*
25+
*
26+
* Local source nodes and the `flowsTo` relation should be thought of in terms of the reference
27+
* semantics of the underlying object. For instance, in the following snippet of code
28+
*
29+
* ```python
30+
* x = []
31+
* x.append(1)
32+
* x.append(2)
33+
* ```
34+
*
35+
* the local source node corresponding to the occurrences of `x` is the empty list that is assigned to `x`
36+
* originally. Even though the two `append` calls modify the value of `x`, they do not change the fact that
37+
* `x` still points to the same object. If, however, we next do `x = x + [3]`, then the expression `x + [3]`
38+
* will be the new local source of what `x` now points to.
2439
*/
2540
class LocalSourceNode extends Node {
2641
cached
2742
LocalSourceNode() {
2843
not comes_from_cfgnode(this) and
29-
not this instanceof ModuleVariableNode
44+
not this instanceof ModuleVariableNode and
45+
// Currently, we create synthetic post-update nodes for
46+
// - arguments to calls that may modify said argument
47+
// - direct reads a writes of object attributes
48+
// Both of these preserve the identity of the underlying pointer, and hence we exclude these as
49+
// local source nodes.
50+
// We do, however, allow the post-update nodes that arise from object creation (which are non-synthetic).
51+
not this instanceof SyntheticPostUpdateNode
3052
or
3153
this = any(ModuleVariableNode mvn).getARead()
3254
}

0 commit comments

Comments
 (0)