Software vulnerabilities can result when a programmer fails to consider all possible data states.
...
Note that adding a default case to a switch
statement, even when all possible switch labels are specified, is an exception (MSC07-EX1) to MSC07-C. Detect and remove dead code.
An alternative compliant solution to the noncompliant code example is to provide a return
statement after the switch
statement. Note, however, that this solution may not be appropriate in all situations.
...
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
| 12 S | Fully implemented. | |||||||
GCC |
| Can detect some violations of this recommendation when the | |||||||
Compass/ROSE | Can detect some violations of this recommendation. In particular, it flags switch statements that do not have a default clause. ROSE should detect "fake switches" as well (that is, a chain of if (x > 0) { /* ... */ } else if (x < 0) { /* ... */ } else if (x == 0) { /* ... */ }
| ||||||||
Klocwork |
| LA_UNUSED | |||||||
PRQA QA-C |
| 0597 1460 1470 1472 2002 2004 | Fully implemented |
Related Vulnerabilities
...
ISO/IEC TR 17961 (Draft) Use of an implied default in a switch statement [swtchdflt]
ISO/IEC TR 24772 "CLL Switch statements and static analysis"
Sources
[Hatton 1995] Section 2.7.2, "Errors of omission and addition"
[Viega 2005] Section 5.2.17, "Failure to account for default case in switch"
[http://www.aeroxp.org/2009/01/lesson-on-infinite-loops] for analysis on the Zune 30 bug
...