Skip to content

Commit b5c0fbb

Browse files
authored
Merge pull request github#15776 from RasmusWL/tt-consistency
Python: Add type-tracking consistency query
2 parents 4085c8e + 800351c commit b5c0fbb

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
private import python
2+
private import semmle.python.dataflow.new.DataFlow
3+
private import semmle.python.dataflow.new.internal.DataFlowPrivate as DataFlowPrivate
4+
private import semmle.python.dataflow.new.internal.TypeTrackingImpl
5+
6+
private module ConsistencyChecksInput implements ConsistencyChecksInputSig {
7+
predicate unreachableNodeExclude(DataFlow::Node n) {
8+
n instanceof DataFlowPrivate::SyntheticPostUpdateNode
9+
or
10+
n instanceof DataFlowPrivate::SyntheticPreUpdateNode
11+
or
12+
// TODO: when adding support for proper content, handle **kwargs passing better!
13+
n instanceof DataFlowPrivate::SynthDictSplatArgumentNode
14+
or
15+
// TODO: when adding support for proper content, handle unpacking tuples in match
16+
// cases better, such as
17+
//
18+
// match (NONSOURCE, SOURCE):
19+
// case (x, y): ...
20+
exists(DataFlow::Node m |
21+
m.asCfgNode().getNode() instanceof MatchCapturePattern
22+
or
23+
m.asCfgNode().getNode() instanceof MatchAsPattern
24+
or
25+
m.asCfgNode().getNode() instanceof MatchOrPattern
26+
|
27+
TypeTrackingInput::simpleLocalSmallStep*(m, n)
28+
)
29+
or
30+
// TODO: when adding support for proper content, handle iterable unpacking better
31+
// such as `for k,v in items:`, or `a, (b,c) = ...`
32+
n instanceof DataFlow::IterableSequenceNode
33+
or
34+
// We have missing use-use flow in
35+
// https://github.com/python/cpython/blob/0fb18b02c8ad56299d6a2910be0bab8ad601ef24/Lib/socketserver.py#L276-L303
36+
// which I couldn't just fix. We ignore the problems here, and instead rely on the
37+
// test-case added in https://github.com/github/codeql/pull/15841
38+
n.getLocation().getFile().getAbsolutePath().matches("%/socketserver.py")
39+
}
40+
}
41+
42+
import ConsistencyChecks<ConsistencyChecksInput>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
unreachableNode
2+
| test2.py:16:17:16:17 | ControlFlowNode for y | Unreachable node in step of kind load bar. |
3+
| test2.py:25:23:25:23 | ControlFlowNode for x | Unreachable node in step of kind load attribute. |
4+
| test2.py:25:23:25:23 | ControlFlowNode for x | Unreachable node in step of kind simpleLocalSmallStep. |
5+
| test2.py:26:17:26:17 | ControlFlowNode for y | Unreachable node in step of kind load bar. |
6+
| test2.py:27:23:27:23 | ControlFlowNode for x | Unreachable node in step of kind simpleLocalSmallStep. |

0 commit comments

Comments
 (0)