Java: account for generic exceptions in java/unreachable-catch-clause

This commit is contained in:
yh-semmle
2018-10-08 22:27:39 -04:00
parent 7962530789
commit 001b9f8b56
2 changed files with 14 additions and 1 deletions

View File

@@ -47,7 +47,10 @@ RefType getAThrownExceptionType(TryStmt t) {
t.getBlock() = call.getEnclosingStmt().getParent*() or
t.getAResourceDecl() = call.getEnclosingStmt()
|
call.getCallee().getAnException() = e and
(
call.getCallee().getAnException() = e or
call.(GenericCall).getATypeArgument(call.getCallee().getAnException().getType()) = e.getType()
) and
not caughtInside(t, call.getEnclosingStmt(), e.getType()) and
result = e.getType()
) or

View File

@@ -68,6 +68,14 @@ public class PartiallyMaskedCatchTest {
} catch (IOException e) {
// reachable: IOException is thrown by getClosableThing()
}
try (ClosableThing thing = new ClosableThing()) {
genericThrowingMethod(IOException.class);
} catch (ExceptionA e) {
// reachable: ExceptionA is thrown by implicit invocation of CloseableThing.close()
} catch (IOException e) {
// reachable: IOException is thrown by invocation of genericThrowingMethod(IOException.class)
}
}
public static ClosableThing getClosableThing() throws IOException {
@@ -94,4 +102,6 @@ public class PartiallyMaskedCatchTest {
throws ClassNotFoundException,
NoSuchMethodException, InstantiationException, IllegalAccessException,
InvocationTargetException {}
public static <E extends Exception> void genericThrowingMethod(Class<E> c) throws E {}
}