...
This noncompliant code example fails to test for conditions where a
is neither b
nor c
. This behavior may be the correct behavior in this case, but failure to account for all the values of a
can result in logic errors if a
unexpectedly assumes a different value.
...
Microsoft Visual C++ .NET with /W4
does not warn when assigning an integer value to an enum
type or when the switch
statement does not contain all possible values of the enumeration.
...
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.
...
These two practices have now been merged. A switch
on an enum
type should now contain a case
label for each enum
value but should also contain a default
label for safety. This is not more difficult to analyze staticallypractice does not add difficulty to static analysis.
Existing implementations are in transition, with some not yet analyzing switch
statements with default
labels. Developers must take extra care to check their own switch
statements until the new practice becomes universal.
...
Compliant Solution (Zune 30)
This The following proposed rewrite is provided by http://www.aeroxp.org/2009/01/lesson-on-infinite-loops. The loop is guaranteed to exit, as because days
decreases for each iteration of the loop, unless the while
condition fails, and the loop terminates.
...
This compliant solution is for illustrative purposes and is not necessarily the solution implemented by Microsoft.
Risk Assessment
Failing to take into account all to account for all possibilities within a logic statement can lead to a corrupted running state, potentially resulting in unintentional information disclosure or abnormal termination.
...
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
...