Skip to content

Commit 001b9f8

Browse files
committed
Java: account for generic exceptions in java/unreachable-catch-clause
1 parent 7962530 commit 001b9f8

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

java/ql/src/Likely Bugs/Statements/PartiallyMaskedCatch.ql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ RefType getAThrownExceptionType(TryStmt t) {
4747
t.getBlock() = call.getEnclosingStmt().getParent*() or
4848
t.getAResourceDecl() = call.getEnclosingStmt()
4949
|
50-
call.getCallee().getAnException() = e and
50+
(
51+
call.getCallee().getAnException() = e or
52+
call.(GenericCall).getATypeArgument(call.getCallee().getAnException().getType()) = e.getType()
53+
) and
5154
not caughtInside(t, call.getEnclosingStmt(), e.getType()) and
5255
result = e.getType()
5356
) or

java/ql/test/query-tests/PartiallyMaskedCatch/PartiallyMaskedCatchTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ public static void method() {
6868
} catch (IOException e) {
6969
// reachable: IOException is thrown by getClosableThing()
7070
}
71+
72+
try (ClosableThing thing = new ClosableThing()) {
73+
genericThrowingMethod(IOException.class);
74+
} catch (ExceptionA e) {
75+
// reachable: ExceptionA is thrown by implicit invocation of CloseableThing.close()
76+
} catch (IOException e) {
77+
// reachable: IOException is thrown by invocation of genericThrowingMethod(IOException.class)
78+
}
7179
}
7280

7381
public static ClosableThing getClosableThing() throws IOException {
@@ -94,4 +102,6 @@ public static void throwingMethod()
94102
throws ClassNotFoundException,
95103
NoSuchMethodException, InstantiationException, IllegalAccessException,
96104
InvocationTargetException {}
105+
106+
public static <E extends Exception> void genericThrowingMethod(Class<E> c) throws E {}
97107
}

0 commit comments

Comments
 (0)