CWE-484¶
Omitted Break Statement in Switch. [Behavioral-Problems, Improper-Adherence-To-Coding-Standards]
Required inputs: IR
Demonstrative Examples
Example 1
In both of these examples, a message is printed based on the month passed into the function:
Example Language:Java (Unsupported language for documentation only)
public void printMessage(int month){
switch (month) {
case 1: print("January");
case 2: print("February");
case 3: print("March");
case 4: print("April");
case 5: print("May");
case 6: print("June");
case 7: print("July");
case 8: print("August");
case 9: print("September");
case 10: print("October");
case 11: print("November");
case 12: print("December");
}
println(" is a great month");
}
Example Language:C
void printMessage(int month){
switch (month) {
case 1: printf("January");
case 2: printf("February");
case 3: printf("March");
case 4: printf("April");
case 5: printf("May");
case 6: printf("June");
case 7: printf("July");
case 8: printf("August");
case 9: printf("September");
case 10: printf("October");
case 11: printf("November");
case 12: printf("December");
}
printf(" is a great month");
}
Both examples do not use a break statement after each case, which leads to unintended fall-through behavior. For example, calling "printMessage(10)" will result in the text "OctoberNovemberDecember is a great month" being printed.
Excerpts from CWE [https://cwe.mitre.org], Copyright (C) 2006-2026, the MITRE Corporation. See section 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¶
This rule shares the following common options: exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
acceptable_end_items¶
acceptable_end_items : set[bauhaus.ir.PIR_Class_Name] = {'Exit_Switch'}
acceptable_middle_items¶
acceptable_middle_items : set[bauhaus.ir.PIR_Class_Name] = {'Exit_Switch', 'Nondefault_Case_Label'}
allow_empty_statements¶
allow_empty_statements : bool = False
allow_fallthrough_comment¶
allow_fallthrough_comment : bool = True
allow_fallthrough_macro¶
allow_fallthrough_macro : bool = True
__fallthrough macro as marker for allowed fallthrough.
allow_if¶
allow_if : bool = False
allow_noreturn_function_calls¶
allow_noreturn_function_calls : bool = False
[[noreturn]] function is considered a
valid termination.
allow_switch¶
allow_switch : bool = False
comment¶
comment : set[str] = set()