Skip to content

Commit e2f17c6

Browse files
authored
Merge pull request #17988 from owen-mc/java/fix-unreachable-blocks-in-const-switch-stmt
Java: fix unreachable basic blocks in const switch stmt
2 parents c5bec1c + ba239a1 commit e2f17c6

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* In a switch statement with a constant switch expression, all non-matching cases were being marked as unreachable, including those that can be reached by falling through from the matching case. This has now been fixed.

java/ql/lib/semmle/code/java/controlflow/UnreachableBlocks.qll

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,15 @@ class UnreachableBasicBlock extends BasicBlock {
217217
not this instanceof CatchClause
218218
or
219219
// Switch statements with a constant comparison expression may have unreachable cases.
220-
exists(ConstSwitchStmt constSwitchStmt, BasicBlock failingCaseBlock |
221-
failingCaseBlock = constSwitchStmt.getAFailingCase().getBasicBlock()
222-
|
220+
exists(ConstSwitchStmt constSwitchStmt, BasicBlock unreachableCaseBlock |
221+
// Not accessible from the switch expression
222+
unreachableCaseBlock = constSwitchStmt.getAFailingCase().getBasicBlock() and
223223
// Not accessible from the successful case
224-
not constSwitchStmt.getMatchingCase().getBasicBlock().getABBSuccessor*() = failingCaseBlock and
225-
// Blocks dominated by the failing case block are unreachable
226-
constSwitchStmt.getAFailingCase().getBasicBlock().bbDominates(this)
224+
not constSwitchStmt.getMatchingCase().getBasicBlock().getABBSuccessor*() =
225+
unreachableCaseBlock
226+
|
227+
// Blocks dominated by an unreachable case block are unreachable
228+
unreachableCaseBlock.bbDominates(this)
227229
)
228230
}
229231
}

java/ql/test/library-tests/unreachableblocks/UnreachableBlocks.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
| unreachableblocks/Unreachable.java:12:22:14:3 | { ... } |
44
| unreachableblocks/Unreachable.java:17:3:17:9 | case ... |
55
| unreachableblocks/Unreachable.java:19:3:19:9 | case ... |
6-
| unreachableblocks/Unreachable.java:22:3:22:9 | case ... |
76
| unreachableblocks/Unreachable.java:24:3:24:9 | case ... |
87
| unreachableblocks/Unreachable.java:26:3:26:10 | case ... |
98
| unreachableblocks/Unreachable.java:27:3:27:10 | default |

0 commit comments

Comments
 (0)