switch statement consists of several case labels, plus a default label. The default label is optional but recommended. (See MSC01-C. Strive for logical completeness.) A series of statements following a case label conventionally ends with a
break statement; if omitted, control flow falls through to the next case in the
switch statement block. Because the
break statement is not required, omitting it does not produce compiler diagnostics. If the omission was unintentional, it can result in an unexpected control flow.
Noncompliant Code Example
In this noncompliant code example, the case where
WE_W lacks a
break statement. Consequently, statements that should be executed only when
WE_X are executed even when
In this compliant solution, each sequence of statements following a case label ends with a
break statement is not required following the case where
WE_Y because there are no statements before the next case label, indicating that both
WE_Z should be handled in the same fashion.
break statement is not required following the default case because it would not affect the control flow.
MSC17-C-EX1: The last label in a
switch statement requires no final
break. It will conventionally be the
MSC17-C-EX2: When control flow is intended to cross statement labels, it is permissible to omit the
break statement. In these instances, the unusual control flow must be explicitly documented.
Failure to include
break statements leads to unexpected control flow.
Can find instances of missing break statement between cases in
|LDRA tool suite|
|62 S||Fully implemented|
|CERT_C-MSC17-a||Missing break statement between cases in a switch statement|
|CERT C: Rec. MSC17-C||Checks for missing break of switch case (rec. fully covered)|
|SonarQube C/C++ Plugin|
Search for vulnerabilities resulting from the violation of this rule on the CERT website.