From efb34aea45331adabba07c5556b0ac4c77011b6e Mon Sep 17 00:00:00 2001 From: Owen Mansel-Chan Date: Thu, 14 Nov 2024 14:50:25 +0000 Subject: [PATCH 1/3] Fix bug in UnreachableBlocks --- java/ql/lib/semmle/code/java/controlflow/UnreachableBlocks.qll | 2 +- .../library-tests/unreachableblocks/UnreachableBlocks.expected | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/java/ql/lib/semmle/code/java/controlflow/UnreachableBlocks.qll b/java/ql/lib/semmle/code/java/controlflow/UnreachableBlocks.qll index 3145371561a7..08ab199af585 100644 --- a/java/ql/lib/semmle/code/java/controlflow/UnreachableBlocks.qll +++ b/java/ql/lib/semmle/code/java/controlflow/UnreachableBlocks.qll @@ -223,7 +223,7 @@ class UnreachableBasicBlock extends BasicBlock { // 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) + failingCaseBlock.bbDominates(this) ) } } diff --git a/java/ql/test/library-tests/unreachableblocks/UnreachableBlocks.expected b/java/ql/test/library-tests/unreachableblocks/UnreachableBlocks.expected index 35178b0a3493..6bd76ea4b96e 100644 --- a/java/ql/test/library-tests/unreachableblocks/UnreachableBlocks.expected +++ b/java/ql/test/library-tests/unreachableblocks/UnreachableBlocks.expected @@ -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 | From bf0fba6c4944927af80f6a40559c83e618c10963 Mon Sep 17 00:00:00 2001 From: Owen Mansel-Chan Date: Thu, 14 Nov 2024 14:53:12 +0000 Subject: [PATCH 2/3] Refactor UnreachableBasicBlock to make it clearer --- .../code/java/controlflow/UnreachableBlocks.qll | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/java/ql/lib/semmle/code/java/controlflow/UnreachableBlocks.qll b/java/ql/lib/semmle/code/java/controlflow/UnreachableBlocks.qll index 08ab199af585..f34ace10d314 100644 --- a/java/ql/lib/semmle/code/java/controlflow/UnreachableBlocks.qll +++ b/java/ql/lib/semmle/code/java/controlflow/UnreachableBlocks.qll @@ -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 - failingCaseBlock.bbDominates(this) + not constSwitchStmt.getMatchingCase().getBasicBlock().getABBSuccessor*() = + unreachableCaseBlock + | + // Blocks dominated by an unreachable case block are unreachable + unreachableCaseBlock.bbDominates(this) ) } } From ba239a1be06b17ffde85e6af8990312d8c4ea8d1 Mon Sep 17 00:00:00 2001 From: Owen Mansel-Chan Date: Thu, 14 Nov 2024 15:02:21 +0000 Subject: [PATCH 3/3] Add change note --- ...14-unreachable-basic-block-in-constant-switch-statement.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 java/ql/lib/change-notes/2024-11-14-unreachable-basic-block-in-constant-switch-statement.md diff --git a/java/ql/lib/change-notes/2024-11-14-unreachable-basic-block-in-constant-switch-statement.md b/java/ql/lib/change-notes/2024-11-14-unreachable-basic-block-in-constant-switch-statement.md new file mode 100644 index 000000000000..50df55a4c1a9 --- /dev/null +++ b/java/ql/lib/change-notes/2024-11-14-unreachable-basic-block-in-constant-switch-statement.md @@ -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.