Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ module Ast implements AstSig<Location> {
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) }

Expand Down
9 changes: 6 additions & 3 deletions java/ql/lib/semmle/code/java/ControlFlowGraph.qll
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,18 @@ private module Ast implements AstSig<Location> {
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 {
Expand Down
45 changes: 17 additions & 28 deletions shared/controlflow/codeql/controlflow/ControlFlowGraph.qll
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,12 @@ signature module AstSig<LocationSig Location> {

/** 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).
*/
Comment on lines +188 to +192
Stmt getBody(int index);

/**
* Gets the `catch` clause at the specified (zero-based) position `index`
Expand All @@ -198,15 +202,6 @@ signature module AstSig<LocationSig Location> {
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.
*
Expand Down Expand Up @@ -699,7 +694,7 @@ module Make0<LocationSig Location, AstSig<Location> 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())
)
Expand Down Expand Up @@ -1256,11 +1251,7 @@ module Make0<LocationSig Location, AstSig<Location> 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))
Expand Down Expand Up @@ -1635,16 +1626,11 @@ module Make0<LocationSig Location, AstSig<Location> 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 |
Expand All @@ -1659,8 +1645,11 @@ module Make0<LocationSig Location, AstSig<Location> 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
Expand Down
Loading