Java Reference
In-Depth Information
/* Handle error condition */
}
Noncompliant Code Example ( switch )
Even though x is supposed to represent a bit (0 or 1) in this noncompliant code example,
some previous error may have allowed x to assume a different value. Detecting and deal-
ing with that inconsistent state sooner rather than later makes the error easier to find.
switch (x) {
case 0: foo(); break;
case 1: bar(); break;
}
Compliant Solution ( switch )
This compliant solution provides the default label to handle all possible values of type
int :
Click here to view code image
switch (x) {
case 0: foo(); break;
case 1: bar(); break;
default: /* Handle error */ break;
}
Noncompliant Code Example (Zune 30)
This noncompliant code example is adapted from C code that appeared in the Zune 30
media player,causing manyplayers tolockuponDecember 30,2008,atmidnight PST.It
contains incomplete logic that causes a denial of service when converting dates.
Click here to view code image
final static int ORIGIN_YEAR = 1980;
/* Number of days since January 1, 1980 */
public void convertDays(long days){
int year = ORIGIN_YEAR;
/* ... */
while (days > 365) {
Search WWH ::




Custom Search