From 492a5ca0875cbad95057509c82212a1b8a1cbc39 Mon Sep 17 00:00:00 2001 From: Anders Schack-Mulligen Date: Fri, 8 Aug 2025 10:31:10 +0200 Subject: [PATCH] Java: Add some more exception edges to the CFG to facilitate guard wrappers. --- .../lib/semmle/code/java/ControlFlowGraph.qll | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/java/ql/lib/semmle/code/java/ControlFlowGraph.qll b/java/ql/lib/semmle/code/java/ControlFlowGraph.qll index 612bca35a60..c1c19fa4450 100644 --- a/java/ql/lib/semmle/code/java/ControlFlowGraph.qll +++ b/java/ql/lib/semmle/code/java/ControlFlowGraph.qll @@ -347,12 +347,28 @@ private module ControlFlowGraphImpl { ) } + private predicate methodMayThrow(Method m, ThrowableType t) { + exists(AstNode n | + t = n.(ThrowStmt).getThrownExceptionType() and + not n.(ThrowStmt).getParent() = any(Method m0).getBody() + or + uncheckedExceptionFromMethod(n, t) + | + n.getEnclosingStmt().getEnclosingCallable() = m and + not exists(TryStmt try | + exists(try.getACatchClause()) and try.getBlock() = n.getEnclosingStmt().getEnclosingStmt*() + ) + ) + } + /** - * Bind `t` to an unchecked exception that may occur in a precondition check. + * Bind `t` to an unchecked exception that may occur in a precondition check or guard wrapper. */ private predicate uncheckedExceptionFromMethod(MethodCall ma, ThrowableType t) { conditionCheckArgument(ma, _, _) and (t instanceof TypeError or t instanceof TypeRuntimeException) + or + methodMayThrow(ma.getMethod(), t) } /**