Skip to content

Commit eb48412

Browse files
Add tests (WIP)
1 parent 9edfd7a commit eb48412

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

python/ql/src/Exceptions/IncorrectExceptOrder.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @name Unreachable 'except' block
2+
* @name Unreachable `except` block
33
* @description Handling general exceptions before specific exceptions means that the specific
44
* handlers are never executed.
55
* @kind problem
@@ -23,7 +23,7 @@ predicate builtinException(string name) {
2323
}
2424

2525
predicate builtinExceptionSubclass(string base, string sub) {
26-
typeModel("builtins." + base + "~Subclass", sub, "")
26+
typeModel("builtins." + base + "~Subclass", "builtins." + sub, "")
2727
}
2828

2929
newtype TExceptType =
@@ -48,7 +48,7 @@ class ExceptType extends TExceptType {
4848
DataFlow::Node getAUse() {
4949
result = classTracker(this.asClass())
5050
or
51-
result = API::builtin(this.asBuiltinName()).asSource()
51+
API::builtin(this.asBuiltinName()).asSource().flowsTo(result)
5252
}
5353

5454
ExceptType getADirectSuperclass() {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Exceptions/IncorrectExceptOrder.ql
1+
query: Exceptions/IncorrectExceptOrder.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql

python/ql/test/query-tests/Exceptions/general/exceptions_test.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,34 @@ def illegal_handler():
6161
val.attr
6262
except Exception:
6363
print (2)
64-
except AttributeError:
64+
except AttributeError: # $Alert[py/unreachable-except]
6565
print (3)
66+
67+
class MyExc(ValueError):
68+
pass
69+
70+
try:
71+
pass
72+
except ValueError:
73+
pass
74+
except MyExc: # $Alert[py/unreachable-except]
75+
pass
76+
77+
class MyBaseExc(Exception):
78+
pass
79+
80+
class MySubExc(MyBaseExc):
81+
pass
82+
83+
try:
84+
pass
85+
except MyBaseExc:
86+
pass
87+
except MySubExc: # $Alert[py/unreachable-except]
88+
pass
89+
except Exception:
90+
pass
91+
6692

6793
#Catch BaseException
6894
def catch_base_exception():

0 commit comments

Comments
 (0)