MISRAC++2023-9.4.2_b
Synopsis
(Required) The structure of a switch statement shall be appropriate
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
Non-empty switch cases were found that are not terminated by a break. This check is identical to MISRAC++2008-6-4-5, MISRAC2004-15.2, MISRAC2012-Rule-16.3.
Coding standards
- CERT MSC17-C
Finish every set of statements associated with a case label with a break statement
- CWE 484
Omitted Break Statement in Switch
- MISRA C:2004 15.2
(Required) An unconditional break statement shall terminate every non-empty switch clause.
- MISRA C:2012 Rule-16.3
(Required) An unconditional break statement shall terminate every switch-clause
- MISRA C++ 2008 6-4-5
(Required) An unconditional throw or break statement shall terminate every non-empty switch-clause.
Code examples
The following code example fails the check and will give a warning:
#include <stdlib.h>
void example(int input) {
switch(input) {
case 0:
if (rand()) {
break;
}
default:
break;
}
}
The following code example passes the check and will not give a warning about this issue:
#include <stdlib.h>
void example(int input) {
switch(input) {
case 0:
if (rand()) {
break;
}
break;
default:
break;
}
}