|
14 | 14 | */ |
15 | 15 |
|
16 | 16 | import python |
| 17 | +import semmle.python.dataflow.new.internal.DataFlowDispatch |
17 | 18 |
|
18 | | -predicate incorrect_except_order(ExceptStmt ex1, ClassValue cls1, ExceptStmt ex2, ClassValue cls2) { |
| 19 | +predicate incorrectExceptOrder(ExceptStmt ex1, Class cls1, ExceptStmt ex2, Class cls2) { |
19 | 20 | exists(int i, int j, Try t | |
20 | 21 | ex1 = t.getHandler(i) and |
21 | 22 | ex2 = t.getHandler(j) and |
22 | 23 | i < j and |
23 | | - cls1 = except_class(ex1) and |
24 | | - cls2 = except_class(ex2) and |
25 | | - cls1 = cls2.getASuperType() |
| 24 | + cls1 = exceptClass(ex1) and |
| 25 | + cls2 = exceptClass(ex2) and |
| 26 | + cls1 = getADirectSuperclass*(cls2) |
26 | 27 | ) |
27 | 28 | } |
28 | 29 |
|
29 | | -ClassValue except_class(ExceptStmt ex) { ex.getType().pointsTo(result) } |
| 30 | +Class exceptClass(ExceptStmt ex) { ex.getType() = classTracker(result).asExpr() } |
30 | 31 |
|
31 | | -from ExceptStmt ex1, ClassValue cls1, ExceptStmt ex2, ClassValue cls2 |
32 | | -where incorrect_except_order(ex1, cls1, ex2, cls2) |
33 | | -select ex2, |
34 | | - "Except block for $@ is unreachable; the more general $@ for $@ will always be executed in preference.", |
35 | | - cls2, cls2.getName(), ex1, "except block", cls1, cls1.getName() |
| 32 | +from ExceptStmt ex1, Class cls1, ExceptStmt ex2, Class cls2, string msg |
| 33 | +where |
| 34 | + incorrectExceptOrder(ex1, cls1, ex2, cls2) and |
| 35 | + if cls1 = cls2 |
| 36 | + then msg = "This except block handling $@ is unreachable; as $@ also handles $@." |
| 37 | + else |
| 38 | + msg = |
| 39 | + "This except block handling $@ is unreachable; as $@ for the more general $@ always subsumes it." |
| 40 | +select ex2, msg, cls2, cls2.getName(), ex1, "this except block", cls1, cls1.getName() |
0 commit comments