CertC-MSC17

Finish every set of statements associated with a case label with a break statement

Required inputs: IR

A 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 widget_type is WE_W lacks a break statement. Consequently, statements that should be executed only when widget_type is WE_X are executed even when widget_type is WE_W .

enum WidgetEnum { WE_W, WE_X, WE_Y, WE_Z } widget_type;
widget_type = WE_X;

switch (widget_type) {
  case WE_W:
    /* ... */
  case WE_X:
    /* ... */
    break;
  case WE_Y:
  case WE_Z:
    /* ... */
    break;
  default: /* Can't happen */
     /* Handle error condition */
}
Compliant Solution

In this compliant solution, each sequence of statements following a case label ends with a break statement:

enum WidgetEnum { WE_W, WE_X, WE_Y, WE_Z } widget_type;
widget_type = WE_X;

switch (widget_type) {
  case WE_W:
    /* ... */
    break;
  case WE_X:
    /* ... */
    break;
  case WE_Y:
  case WE_Z:
    /* ... */
    break;
  default: /* Can't happen */
     /* Handle error condition */
}

A break statement is not required following the case where widget_type is WE_Y because there are no statements before the next case label, indicating that both WE_Y and WE_Z should be handled in the same fashion.

A break statement is not required following the default case because it would not affect the control flow.

Exceptions

MSC17-C-EX1: The last label in a switch statement requires no final break . It will conventionally be the default label.

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 made clear, such as by adding the [[fallthrough]] C2x attribute, the __attribute__((__fallthrough__)) GNU extension, or a simple comment.

enum WidgetEnum { WE_W, WE_X, WE_Y, WE_Z } widget_type;
widget_type = WE_X;

switch (widget_type) {
  case WE_W:
    /* ... */
    /* No break; fall through to the WE_X case */
  case WE_X:
    /* ... */
    break;
  case WE_Y: case WE_Z:
    /* ... */
    break;
  default: /* Can't happen */
     /* Handle error condition */
}
Risk Assessment

Failure to include break statements leads to unexpected control flow.

Recommendation Severity Likelihood Detectable Repairable Priority Level
MSC17-C Medium Likely Yes Yes P18 L1
Related Guidelines
SEI CERT C++ Coding Standard VOID MSC18-CPP. Finish every set of statements associated with a case label with a break statement
CERT Oracle Secure Coding Standard for Java MSC52-J. Finish every set of statements associated with a case label with a break statement
Excerpt from SEI CERT C Coding Standard: Rules for Developing Safe, Reliable, and Secure Systems (2016 Edition) and SEI CERT C Coding Standard [https://cmu-sei.github.io/secure-coding-standards/sei-cert-c-coding-standard/recommendations/miscellaneous-msc/msc17-c], Copyright (C) 1995-2026 Carnegie Mellon University. See 9.4 3rd-Party Licenses in the documentation for full details.

Possible Messages

Key

Text

Severity

Disabled

unterminated_case

This switch-case is not {}, last statement is {}.

None

False

unterminated_empty_case

This switch-case is empty, not {}.

None

False

Options

acceptable_end_items

acceptable_end_items : set[bauhaus.ir.PIR_Class_Name] = {'Unconditional_Branch'}

Allowed last items at the end of a switch.
 

acceptable_middle_items

acceptable_middle_items : set[bauhaus.ir.PIR_Class_Name] = {'Switch_Case_Label', 'Unconditional_Branch'}

Allowed last items in the middle of a switch.
 

allow_empty_statements

allow_empty_statements : bool = False

Whether empty statements at the end of switch-case should be ignored.
 

allow_fallthrough_comment

allow_fallthrough_comment : bool = True

Allow comments with text including both "fall" and "through" simultaneously.
 

allow_fallthrough_macro

allow_fallthrough_macro : bool = True

Allows the __fallthrough macro as marker for allowed fallthrough.
 

allow_if

allow_if : bool = False

Whether an if containing acceptable items at the end of both branches is accepted.
 

allow_noreturn_function_calls

allow_noreturn_function_calls : bool = True

Whether a function call to a [[noreturn]] function is considered a valid termination.
 

allow_switch

allow_switch : bool = False

Whether a switch containing acceptable items at the end of all cases is accepted.
 

allow_unterminated_last_clause

allow_unterminated_last_clause : bool = True

Whether the last switch-clause (typically the default label) is exempt from needing a terminator at all, as it cannot fall through into anything. MISRA requires a terminator there too (default False); CERT MSC17-C-EX1 exempts it.
 

comment

comment : set[str] = set()

The comment that indicates an intentional fallthrough. Can be a set of strings.