Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Parasoft Jtest 2022.2

A switch block comprises several case labels and an optional but highly recommended default label. Statements that follow each case label must end with a break statement, which is responsible for transferring the control to the end of the switch block. When omitted, the statements in the subsequent case label are executed. Because the break statement is optional, omitting it produces no compiler warnings. When this behavior is unintentional, it can cause unexpected control flow.

Noncompliant Code Example

In this noncompliant code example, the case wherein where the card is 11 lacks a break statement. As a result, execution continues with the statements for card = 12.

Code Block
bgColor#FFCCCC

int card = 11;

switch (card) {
  /* ... */
  case 11: 
    System.out.println("Jack");
  case 12: 
    System.out.println("Queen"); 
    break;
  case 13: 
    System.out.println("King"); 
    break;
  default: 
    System.out.println("Invalid Card"); 
    break;
}

Compliant Solution

This compliant solution terminates each case (including the default case) with a break statement.:

Code Block
bgColor#CCCCFF

int card = 11;

switch (card) {
  /* ... */
  case 11: 
    System.out.println("Jack");
    break;
  case 12: 
    System.out.println("Queen"); 
    break;
  case 13: 
    System.out.println("King"); 
    break;
  default: 
    System.out.println("Invalid Card"); 
    break;
}

Exceptions

Wiki Markup
*MSC09-EX0*: The {{break}} statement at the end of the final case in a {{switch}} statement may be omitted. By convention, this is the {{default}} label. The {{break}} statement serves to transfer control to the end of the {{switch}} block. Fall-through behavior also causes control to arrive at the end of the {{switch}} block. Consequently, control transfers to the statements following the {{switch}} block without regard to the presence or absence of the {{break}} statement. Nevertheless, the final case in a {{switch}} statement should end with a {{break}} statement in accordance with good programming style (see \[[Rogue 2000|AA. References#Rogue 00]\]).

Applicability

Failure to include break statements can cause unexpected control flow.

The break statement at the end of the final case in a switch statement may be omitted. By convention, this is the default label. The break statement serves to transfer control to the end of the switch block. Fall-through behavior also causes control to arrive at the end of the switch block. Consequently, control transfers to the statements following the switch block without regard to the presence or absence of the break statement. Nevertheless, the final case in a switch statement should end with a break statement in accordance with good programming style [Vermeulen 2000].

Exceptionally, when MSC09-EX1: When multiple cases require execution of identical code, then break statements may be omitted from all cases except the last one. Similarly, when processing for one case is a proper prefix of processing for one or more other cases, the break statement may be omitted from the prefix case. This should be clearly indicated with a comment. For example:

Code Block
bgColor#CCCCFF

int card = 11;
int value;

// Cases 11,12,13 fall through to the same case 
switch (card) {
  // Processing for this case requires a prefix
  // of the actions for the following three
  case 10:
    do_something(card);
    // MSC13-J:EX2: theseIntentional fall-through
    // These three cases are treated identically 
  case 11:        // breakBreak not required
  case 12:        // breakBreak not required
  case 13: 
    value = 10; 
    break;        // breakBreak required
  default: 
    // Handle Errorerror Conditioncondition 
}

MSC09-EX2: When Also, when a case ends with a return or throw statement, the break statement may be omitted.

Risk Assessment

Failure to include break statements can cause unexpected control flow.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

MSC09-J

medium

unlikely

low

P6

L2

Related Guidelines

CERT C Secure Coding Standard

MSC17-C. Finish every set of statements associated with a case label with a break statement

CERT C++ Secure Coding Standard

MSC18-CPP. Finish every set of statements associated with a case label with a break statement

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="c568192c-de66-4f69-8645-9cdae3f190f8"><ac:plain-text-body><![CDATA[

[ISO/IEC TR 24772:2010

http://www.aitcnet.org/isai/]

"Switch Statements and Static Analysis [java:CLL]"

]]></ac:plain-text-body></ac:structured-macro>

MITRE CWE

CWE-484, "Omitted Break Statement in Switch"

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="11bc7d9c-b8e5-49e5-9ba5-175244e16e60"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. References#JLS 05]]

[Section 14.11 The switch Statement

http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.11]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8627e036-8036-478f-b00b-2611be2cca99"><ac:plain-text-body><![CDATA[

[[Rogue 2000

AA. References#Rogue 00]]

[The Elements of Java Style

http://www.ambysoft.com/books/elementsJavaStyle.html], Rule 78.

]]></ac:plain-text-body></ac:structured-macro>

Automated Detection

ToolVersionCheckerDescription
Parasoft Jtest
Include Page
Parasoft_V
Parasoft_V
CERT.MSC52.SBCDo not use a "switch" statement with a bad "case"
SonarQube
Include Page
SonarQube_V
SonarQube_V
S128


Bibliography


...

Image Added Image Added Image AddedImage Removed 49. Miscellaneous (MSC) MSC57-J. Use inequality operators to terminate loops whose counter changes by more than one