Skip to content

Commit 5a9fca4

Browse files
authored
Python: Fix ExceptStmt::getType
We were not supporting `except` statements handling multiple exception types (specified as a tuple) correctly, instead just returning the tuple itself as the "type" (which makes little sense). To fix this, we explicitly extract the elements of this node, in the case where it _is_ a tuple. This is a change that can potentially affect many queries (as `getType` is used in quite a few places), so some care should be taken to ensure that this does not adversely affect performance.
1 parent ec9063b commit 5a9fca4

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

python/ql/src/semmle/python/Stmts.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ class ExceptStmt extends ExceptStmt_ {
153153
override Stmt getASubStatement() { result = this.getAStmt() }
154154

155155
override Stmt getLastStatement() { result = this.getBody().getLastItem().getLastStatement() }
156+
157+
override Expr getType() {
158+
result = super.getType() and not result instanceof Tuple
159+
or
160+
result = super.getType().(Tuple).getAnElt()
161+
}
156162
}
157163

158164
/** An assert statement, such as `assert a == b, "A is not equal to b"` */
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
| test.py:5:15:5:22 | ControlFlowNode for next() | Call to next() in a generator |
22
| test.py:10:20:10:27 | ControlFlowNode for next() | Call to next() in a generator |
3-
| test.py:62:19:62:28 | ControlFlowNode for next() | Call to next() in a generator |

0 commit comments

Comments
 (0)