From 01173bf383fdb3e13b5be81f57f8215944f211ee Mon Sep 17 00:00:00 2001 From: Anders Schack-Mulligen Date: Mon, 8 Jun 2026 14:01:24 +0200 Subject: [PATCH] Cfg: Fold getTryInit into indexed getBody. --- .../controlflow/internal/ControlFlowGraph.qll | 2 +- .../lib/semmle/code/java/ControlFlowGraph.qll | 9 ++-- .../codeql/controlflow/ControlFlowGraph.qll | 45 +++++++------------ 3 files changed, 24 insertions(+), 32 deletions(-) diff --git a/csharp/ql/lib/semmle/code/csharp/controlflow/internal/ControlFlowGraph.qll b/csharp/ql/lib/semmle/code/csharp/controlflow/internal/ControlFlowGraph.qll index 7e5072637c30..0d8ced82e248 100644 --- a/csharp/ql/lib/semmle/code/csharp/controlflow/internal/ControlFlowGraph.qll +++ b/csharp/ql/lib/semmle/code/csharp/controlflow/internal/ControlFlowGraph.qll @@ -203,7 +203,7 @@ module Ast implements AstSig { final private class FinalTryStmt = CS::TryStmt; class TryStmt extends FinalTryStmt { - Stmt getBody() { result = this.getBlock() } + Stmt getBody(int index) { index = 0 and result = this.getBlock() } CatchClause getCatch(int index) { result = this.getCatchClause(index) } diff --git a/java/ql/lib/semmle/code/java/ControlFlowGraph.qll b/java/ql/lib/semmle/code/java/ControlFlowGraph.qll index 3407a43403e8..ec1ce80a9b8b 100644 --- a/java/ql/lib/semmle/code/java/ControlFlowGraph.qll +++ b/java/ql/lib/semmle/code/java/ControlFlowGraph.qll @@ -117,15 +117,18 @@ private module Ast implements AstSig { final private class FinalTryStmt = J::TryStmt; class TryStmt extends FinalTryStmt { - Stmt getBody() { result = super.getBlock() } + Stmt getBody(int index) { + result = super.getResource(index) + or + index = count(super.getAResource()) and + result = super.getBlock() + } CatchClause getCatch(int index) { result = super.getCatchClause(index) } Stmt getFinally() { result = super.getFinally() } } - AstNode getTryInit(TryStmt try, int index) { result = try.getResource(index) } - final private class FinalCatchClause = J::CatchClause; class CatchClause extends FinalCatchClause { diff --git a/shared/controlflow/codeql/controlflow/ControlFlowGraph.qll b/shared/controlflow/codeql/controlflow/ControlFlowGraph.qll index 33a609d55520..4e6fd7ac2d47 100644 --- a/shared/controlflow/codeql/controlflow/ControlFlowGraph.qll +++ b/shared/controlflow/codeql/controlflow/ControlFlowGraph.qll @@ -185,8 +185,12 @@ signature module AstSig { /** A `try` statement with `catch` and/or `finally` clauses. */ class TryStmt extends Stmt { - /** Gets the body of this `try` statement. */ - Stmt getBody(); + /** + * Gets the body of this `try` statement at the specified (zero-based) + * position `index`. In some languages, there is only ever a single body + * (with `index` 0). + */ + Stmt getBody(int index); /** * Gets the `catch` clause at the specified (zero-based) position `index` @@ -198,15 +202,6 @@ signature module AstSig { Stmt getFinally(); } - /** - * Gets the initializer of this `try` statement at the specified (zero-based) - * position `index`, if any. - * - * An example of this are resource declarations in Java's try-with-resources - * statement. - */ - default AstNode getTryInit(TryStmt try, int index) { none() } - /** * Gets the `else` block of this `try` statement, if any. * @@ -699,7 +694,7 @@ module Make0 Ast> { or exists(TryStmt trystmt | trystmt = n and - cannotTerminateNormally(trystmt.getBody()) and + cannotTerminateNormally(trystmt.getBody(_)) and forall(CatchClause catch | trystmt.getCatch(_) = catch | cannotTerminateNormally(catch.getBody()) ) @@ -1256,11 +1251,7 @@ module Make0 Ast> { ) ) or - exists(TryStmt trystmt | - ast = getTryInit(trystmt, _) - or - ast = trystmt.getBody() - | + exists(TryStmt trystmt | ast = trystmt.getBody(_) | c.getSuccessorType() instanceof ExceptionSuccessor and ( n.isBefore(trystmt.getCatch(0)) @@ -1635,16 +1626,11 @@ module Make0 Ast> { or exists(TryStmt trystmt | n1.isBefore(trystmt) and - ( - n2.isBefore(getTryInit(trystmt, 0)) - or - not exists(getTryInit(trystmt, _)) and n2.isBefore(trystmt.getBody()) - ) + n2.isBefore(trystmt.getBody(0)) or - exists(int i | n1.isAfter(getTryInit(trystmt, i)) | - n2.isBefore(getTryInit(trystmt, i + 1)) - or - not exists(getTryInit(trystmt, i + 1)) and n2.isBefore(trystmt.getBody()) + exists(int i | + n1.isAfter(trystmt.getBody(i)) and + n2.isBefore(trystmt.getBody(i + 1)) ) or exists(PreControlFlowNode beforeElse, PreControlFlowNode beforeFinally | @@ -1659,8 +1645,11 @@ module Make0 Ast> { not exists(trystmt.getFinally()) and beforeFinally.isAfter(trystmt) ) | - n1.isAfter(trystmt.getBody()) and - n2 = beforeElse + exists(int i | + n1.isAfter(trystmt.getBody(i)) and + not exists(trystmt.getBody(i + 1)) and + n2 = beforeElse + ) or n1.isAfter(getTryElse(trystmt)) and n2 = beforeFinally