From d2ff1baff0bbffce1c27172f4db622c36083d1d9 Mon Sep 17 00:00:00 2001 From: Chris Smowton Date: Thu, 16 Nov 2023 14:23:56 +0000 Subject: [PATCH] Replace getDefaultOrNullDefaultCase with getDefaultCase --- java/ql/examples/snippets/switchcase.ql | 2 +- java/ql/lib/semmle/code/java/ControlFlowGraph.qll | 2 +- java/ql/lib/semmle/code/java/Expr.qll | 11 ++++++----- java/ql/lib/semmle/code/java/Statement.qll | 11 ++++++----- .../code/java/controlflow/UnreachableBlocks.qll | 2 +- .../src/Advisory/Statements/MissingDefaultInSwitch.ql | 2 +- .../src/Likely Bugs/Statements/MissingEnumInSwitch.ql | 2 +- 7 files changed, 17 insertions(+), 15 deletions(-) diff --git a/java/ql/examples/snippets/switchcase.ql b/java/ql/examples/snippets/switchcase.ql index 12a6ae021f3..d425d5686f4 100644 --- a/java/ql/examples/snippets/switchcase.ql +++ b/java/ql/examples/snippets/switchcase.ql @@ -14,5 +14,5 @@ where switch.getExpr().getType() = enum and missing.getDeclaringType() = enum and not switch.getAConstCase().getValue() = missing.getAnAccess() and - not exists(switch.getDefaultOrNullDefaultCase()) + not exists(switch.getDefaultCase()) select switch diff --git a/java/ql/lib/semmle/code/java/ControlFlowGraph.qll b/java/ql/lib/semmle/code/java/ControlFlowGraph.qll index 7486736ecfc..9b6e17634d9 100644 --- a/java/ql/lib/semmle/code/java/ControlFlowGraph.qll +++ b/java/ql/lib/semmle/code/java/ControlFlowGraph.qll @@ -923,7 +923,7 @@ private module ControlFlowGraphImpl { completion = NormalCompletion() or // if no default case exists, then normal completion of the expression may terminate the switch - not exists(switch.getDefaultOrNullDefaultCase()) and + not exists(switch.getDefaultCase()) and last(switch.getExpr(), last, completion) and completion = NormalCompletion() ) diff --git a/java/ql/lib/semmle/code/java/Expr.qll b/java/ql/lib/semmle/code/java/Expr.qll index 59a4652f449..1ea7899d0da 100644 --- a/java/ql/lib/semmle/code/java/Expr.qll +++ b/java/ql/lib/semmle/code/java/Expr.qll @@ -1530,11 +1530,12 @@ class SwitchExpr extends Expr, StmtParent, @switchexpr { /** Gets a (non-default) pattern `case` of this `switch` expression. */ PatternCase getAPatternCase() { result = this.getACase() } - /** Gets the `default` case of this switch expression, if any. */ - DefaultCase getDefaultCase() { result = this.getACase() } - - /** Gets the `default` or `case null, default` case of this switch statement, if any. */ - SwitchCase getDefaultOrNullDefaultCase() { result = this.getACase() and result.hasDefaultLabel() } + /** + * Gets the `default` case of this switch statement, if any. + * + * Note this may be `default` or `case null, default`. + */ + SwitchCase getDefaultCase() { result = this.getACase() and result.hasDefaultLabel() } /** Gets the expression of this `switch` expression. */ Expr getExpr() { result.getParent() = this } diff --git a/java/ql/lib/semmle/code/java/Statement.qll b/java/ql/lib/semmle/code/java/Statement.qll index 87605e57691..fccf1cec0d4 100644 --- a/java/ql/lib/semmle/code/java/Statement.qll +++ b/java/ql/lib/semmle/code/java/Statement.qll @@ -403,11 +403,12 @@ class SwitchStmt extends Stmt, @switchstmt { /** Gets a (non-default) pattern `case` of this `switch` statement. */ PatternCase getAPatternCase() { result = this.getACase() } - /** Gets the `default` case of this switch statement, if any. */ - DefaultCase getDefaultCase() { result = this.getACase() } - - /** Gets the `default` or `case null, default` case of this switch statement, if any. */ - SwitchCase getDefaultOrNullDefaultCase() { result = this.getACase() and result.hasDefaultLabel() } + /** + * Gets the `default` case of this switch statement, if any. + * + * Note this may be `default` or `case null, default`. + */ + SwitchCase getDefaultCase() { result = this.getACase() and result.hasDefaultLabel() } /** Gets the expression of this `switch` statement. */ Expr getExpr() { result.getParent() = this } diff --git a/java/ql/lib/semmle/code/java/controlflow/UnreachableBlocks.qll b/java/ql/lib/semmle/code/java/controlflow/UnreachableBlocks.qll index 4a13ad93e9f..3145371561a 100644 --- a/java/ql/lib/semmle/code/java/controlflow/UnreachableBlocks.qll +++ b/java/ql/lib/semmle/code/java/controlflow/UnreachableBlocks.qll @@ -174,7 +174,7 @@ class ConstSwitchStmt extends SwitchStmt { exists(this.getExpr().(ConstantExpr).getIntValue()) and if exists(this.getMatchingConstCase()) then result = this.getMatchingConstCase() - else result = this.getDefaultOrNullDefaultCase() + else result = this.getDefaultCase() } /** diff --git a/java/ql/src/Advisory/Statements/MissingDefaultInSwitch.ql b/java/ql/src/Advisory/Statements/MissingDefaultInSwitch.ql index ce2aa08035b..eab8a596f0e 100644 --- a/java/ql/src/Advisory/Statements/MissingDefaultInSwitch.ql +++ b/java/ql/src/Advisory/Statements/MissingDefaultInSwitch.ql @@ -15,5 +15,5 @@ import java from SwitchStmt switch where not switch.getExpr().getType() instanceof EnumType and - not exists(switch.getDefaultOrNullDefaultCase()) + not exists(switch.getDefaultCase()) select switch, "Switch statement does not have a default case." diff --git a/java/ql/src/Likely Bugs/Statements/MissingEnumInSwitch.ql b/java/ql/src/Likely Bugs/Statements/MissingEnumInSwitch.ql index 2f76e6f2e31..dfea3ad72d9 100644 --- a/java/ql/src/Likely Bugs/Statements/MissingEnumInSwitch.ql +++ b/java/ql/src/Likely Bugs/Statements/MissingEnumInSwitch.ql @@ -17,6 +17,6 @@ from SwitchStmt switch, EnumType enum, EnumConstant missing where switch.getExpr().getType() = enum and missing.getDeclaringType() = enum and - not exists(switch.getDefaultOrNullDefaultCase()) and + not exists(switch.getDefaultCase()) and not switch.getAConstCase().getValue() = missing.getAnAccess() select switch, "Switch statement does not have a case for $@.", missing, missing.getName()