Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java: fix unreachable basic blocks in const switch stmt #17988

Merged
Merged
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
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* 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.
14 changes: 8 additions & 6 deletions java/ql/lib/semmle/code/java/controlflow/UnreachableBlocks.qll
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,15 @@ class UnreachableBasicBlock extends BasicBlock {
not this instanceof CatchClause
or
// Switch statements with a constant comparison expression may have unreachable cases.
exists(ConstSwitchStmt constSwitchStmt, BasicBlock failingCaseBlock |
failingCaseBlock = constSwitchStmt.getAFailingCase().getBasicBlock()
|
exists(ConstSwitchStmt constSwitchStmt, BasicBlock unreachableCaseBlock |
// Not accessible from the switch expression
unreachableCaseBlock = constSwitchStmt.getAFailingCase().getBasicBlock() and
// Not accessible from the successful case
not constSwitchStmt.getMatchingCase().getBasicBlock().getABBSuccessor*() = failingCaseBlock and
// Blocks dominated by the failing case block are unreachable
constSwitchStmt.getAFailingCase().getBasicBlock().bbDominates(this)
not constSwitchStmt.getMatchingCase().getBasicBlock().getABBSuccessor*() =
unreachableCaseBlock
|
// Blocks dominated by an unreachable case block are unreachable
unreachableCaseBlock.bbDominates(this)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
| unreachableblocks/Unreachable.java:12:22:14:3 | { ... } |
| unreachableblocks/Unreachable.java:17:3:17:9 | case ... |
| unreachableblocks/Unreachable.java:19:3:19:9 | case ... |
| unreachableblocks/Unreachable.java:22:3:22:9 | case ... |
| unreachableblocks/Unreachable.java:24:3:24:9 | case ... |
| unreachableblocks/Unreachable.java:26:3:26:10 | case ... |
| unreachableblocks/Unreachable.java:27:3:27:10 | default |