Skip to content

Commit a93b4ed

Browse files
committed
Java: Make JumpStmt a proper superclass
1 parent 8faabb8 commit a93b4ed

File tree

11 files changed

+65
-21
lines changed

11 files changed

+65
-21
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: fix
3+
---
4+
* The QL class `JumpStmt` has been made the superclass of `BreakStmt`, `ContinueStmt` and `YieldStmt`. This allows directly using its inherited predicates without having to explicitly cast to `JumpStmt` first.

java/ql/lib/semmle/code/java/Expr.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1399,7 +1399,7 @@ class SwitchExpr extends Expr, StmtParent, @switchexpr {
13991399
Expr getAResult() {
14001400
result = this.getACase().getRuleExpression()
14011401
or
1402-
exists(YieldStmt yield | yield.(JumpStmt).getTarget() = this and result = yield.getValue())
1402+
exists(YieldStmt yield | yield.getTarget() = this and result = yield.getValue())
14031403
}
14041404

14051405
/** Gets a printable representation of this expression. */

java/ql/lib/semmle/code/java/Statement.qll

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -557,13 +557,7 @@ class ThrowStmt extends Stmt, @throwstmt {
557557
}
558558

559559
/** A `break`, `yield` or `continue` statement. */
560-
class JumpStmt extends Stmt {
561-
JumpStmt() {
562-
this instanceof BreakStmt or
563-
this instanceof YieldStmt or
564-
this instanceof ContinueStmt
565-
}
566-
560+
abstract class JumpStmt extends Stmt {
567561
/**
568562
* Gets the labeled statement that this `break` or
569563
* `continue` statement refers to, if any.
@@ -584,12 +578,7 @@ class JumpStmt extends Stmt {
584578
)
585579
}
586580

587-
private SwitchExpr getSwitchExprTarget() { result = this.(YieldStmt).getParent+() }
588-
589581
private StmtParent getEnclosingTarget() {
590-
result = this.getSwitchExprTarget()
591-
or
592-
not exists(this.getSwitchExprTarget()) and
593582
result = this.getAPotentialTarget() and
594583
not exists(Stmt other | other = this.getAPotentialTarget() | other.getEnclosingStmt+() = result)
595584
}
@@ -598,14 +587,15 @@ class JumpStmt extends Stmt {
598587
* Gets the statement or `switch` expression that this `break`, `yield` or `continue` jumps to.
599588
*/
600589
StmtParent getTarget() {
590+
// Note: This implementation only considers `break` and `continue`; YieldStmt overrides this predicate
601591
result = this.getLabelTarget()
602592
or
603593
not exists(this.getLabelTarget()) and result = this.getEnclosingTarget()
604594
}
605595
}
606596

607597
/** A `break` statement. */
608-
class BreakStmt extends Stmt, @breakstmt {
598+
class BreakStmt extends JumpStmt, @breakstmt {
609599
/** Gets the label targeted by this `break` statement, if any. */
610600
string getLabel() { namestrings(result, _, this) }
611601

@@ -626,12 +616,21 @@ class BreakStmt extends Stmt, @breakstmt {
626616
/**
627617
* A `yield` statement.
628618
*/
629-
class YieldStmt extends Stmt, @yieldstmt {
619+
class YieldStmt extends JumpStmt, @yieldstmt {
630620
/**
631621
* Gets the value of this `yield` statement.
632622
*/
633623
Expr getValue() { result.getParent() = this }
634624

625+
/**
626+
* Gets the `switch` expression target of this `yield` statement.
627+
*/
628+
override SwitchExpr getTarget() {
629+
// Get the innermost enclosing SwitchExpr; this works because getParent() is defined for Stmt and
630+
// therefore won't proceed after the innermost SwitchExpr (due to it being an Expr)
631+
result = this.getParent+()
632+
}
633+
635634
override string pp() { result = "yield ..." }
636635

637636
override string toString() { result = "yield ..." }
@@ -642,7 +641,7 @@ class YieldStmt extends Stmt, @yieldstmt {
642641
}
643642

644643
/** A `continue` statement. */
645-
class ContinueStmt extends Stmt, @continuestmt {
644+
class ContinueStmt extends JumpStmt, @continuestmt {
646645
/** Gets the label targeted by this `continue` statement, if any. */
647646
string getLabel() { namestrings(result, _, this) }
648647

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ import java
1616
from DoStmt do, ContinueStmt continue
1717
where
1818
do.getCondition().(BooleanLiteral).getBooleanValue() = false and
19-
continue.(JumpStmt).getTarget() = do
19+
continue.getTarget() = do
2020
select continue, "This 'continue' never re-runs the loop - the $@ is always false.",
2121
do.getCondition(), "loop condition"

java/ql/src/Likely Bugs/Termination/ConstantLoopCondition.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ predicate loopExit(LoopStmt loop, Stmt exit) {
3232
exit.getEnclosingStmt*() = loop.getBody() and
3333
(
3434
exit instanceof ReturnStmt or
35-
exit.(BreakStmt).(JumpStmt).getTarget() = loop.getEnclosingStmt*()
35+
exit.(BreakStmt).getTarget() = loop.getEnclosingStmt*()
3636
)
3737
}
3838

java/ql/src/Security/CWE/CWE-129/ArraySizing.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ class PointlessLoop extends WhileStmt {
4444
PointlessLoop() {
4545
this.getCondition().(BooleanLiteral).getBooleanValue() = true and
4646
// The only `break` must be the last statement.
47-
forall(BreakStmt break | break.(JumpStmt).getTarget() = this |
47+
forall(BreakStmt break | break.getTarget() = this |
4848
this.getStmt().(BlockStmt).getLastStmt() = break
4949
) and
5050
// No `continue` statements.
51-
not exists(ContinueStmt continue | continue.(JumpStmt).getTarget() = this)
51+
not exists(ContinueStmt continue | continue.getTarget() = this)
5252
}
5353
}
5454

java/ql/src/Security/CWE/CWE-835/InfiniteLoop.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ predicate loopCondition(LoopStmt loop, Expr cond, boolean polarity) {
2626
ifstmt.getEnclosingStmt*() = loop.getBody() and
2727
ifstmt.getCondition() = cond and
2828
(
29-
exit.(BreakStmt).(JumpStmt).getTarget() = loop or
29+
exit.(BreakStmt).getTarget() = loop or
3030
exit.(ReturnStmt).getEnclosingStmt*() = loop.getBody()
3131
) and
3232
(

java/ql/test/library-tests/stmts/JumpTargets.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
| stmts/A.java:18:5:18:12 | yield ... | stmts/A.java:16:10:16:18 | switch (...) |
2+
| stmts/A.java:22:6:22:13 | yield ... | stmts/A.java:20:14:20:22 | switch (...) |
3+
| stmts/A.java:25:6:25:13 | yield ... | stmts/A.java:20:14:20:22 | switch (...) |
4+
| stmts/A.java:34:7:34:14 | yield ... | stmts/A.java:16:10:16:18 | switch (...) |
5+
| stmts/A.java:37:5:37:13 | yield ... | stmts/A.java:16:10:16:18 | switch (...) |
16
| stmts/B.java:8:5:8:10 | break | stmts/B.java:6:3:6:26 | for (...;...;...) |
27
| stmts/B.java:10:5:10:13 | continue | stmts/B.java:6:3:6:26 | for (...;...;...) |
38
| stmts/B.java:13:6:13:11 | break | stmts/B.java:11:4:11:17 | while (...) |
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
| stmts/A.java:6:3:6:10 | case ... |
22
| stmts/A.java:8:3:8:10 | case ... |
33
| stmts/A.java:10:3:10:10 | default |
4+
| stmts/A.java:17:4:17:12 | case ... |
5+
| stmts/A.java:20:4:20:12 | case ... |
6+
| stmts/A.java:21:5:21:13 | case ... |
7+
| stmts/A.java:24:5:24:14 | default |
8+
| stmts/A.java:28:4:28:12 | case ... |
9+
| stmts/A.java:29:4:29:13 | default |
10+
| stmts/A.java:32:6:32:14 | case ... |
11+
| stmts/A.java:33:6:33:14 | case ... |
412
| stmts/B.java:21:5:21:12 | case ... |
513
| stmts/B.java:23:5:23:12 | case ... |
614
| stmts/B.java:25:5:25:12 | case ... |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//semmle-extractor-options: --javac-args -source 14 -target 14

0 commit comments

Comments
 (0)