...
Compliant Solution (Switch)
The following ompliant solution takes care to provide This compliant solution provides the default
label to handle all valid values of type int
:
Code Block | ||
---|---|---|
| ||
switch(x) {
case 0: foo(); break;
case 1: bar(); break;
default: /* handle error */ break;
} |
Noncompliant Code Example (Zune 30)
This noncompliant code example shows incomplete logic when converting dates. The code appeared in the Zune 30 media player, causing many players to lock up on December 30, 2008, at midnight PST. This noncompliant code example comes from the ConvertDays
function in the real-time clock (RTC) routines for the MC13783 PMIC RTC. This noncompliant code sample takes the number of days since January 1, 1980, and computes the correct year and number of days since January 1 of the correct year.The flaw in the code occurs when days
has the value 366 because the loop never terminates. This bug manifested itself on the 366th day of 2008, which was the first leap year in which this code was active.It contains incomplete logic that causes a denial of service when converting dates.
Code Block | ||
---|---|---|
| ||
final static int ORIGIN_YEAR = 1980; UINT32 days = /* number of days since January 1, 1980 */ int year = ORIGIN_YEAR; /* ... */ while (days > 365) { if (IsLeapYear(year)) { if (days > 366) { days -= 366; year += 1; } } else { days -= 365; year += 1; } } |
The ConvertDays
method in the real-time clock (RTC) routines for the MC13783 PMIC RTC takes the number of days since January 1, 1980, and computes the correct year and number of days since January 1 of the correct year.
The flaw in the code occurs when days
has the value 366 because the loop never terminates. This bug manifested itself on the 366th day of 2008, which was the first leap year in which this code was active.
Compliant Solution (Zune 30)
...