Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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.

...

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 \[java:[Rogue 2000|AA. References#Rogue 00]\]).

...

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="1e2d75f82c877a65-c8ae0f09-42fa477e-976bab1e-d4f47a4ae5809646135cc03a"><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"

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="f967263d4f750759-b0b313e4-4b6c42e5-86b2822c-9869821b3d4e1bfe7dda364b"><ac:plain-text-body><![CDATA[

[java:[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="2838e086a6cb10fa-b1cd2f0b-4ecf4abb-b0f087f0-1111cb1c785f0aab006386bd"><ac:plain-text-body><![CDATA[

[java:[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>

...

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