...
| Code Block | ||
|---|---|---|
| ||
class Dimensions {
private int length;
private int width;
private int height;
public Dimensions(int length, int width, int height) {
this.length = length;
this.width = width;
this.height = height;
}
protected int getVolumePackage(int weight) {
length += 2;
width += 2;
height += 2;
try {
if(length <<= 2 || width <<= 2 || height <<= 2 || weight <<= 0 || weight >> 20)
throw new IllegalArgumentException();
int volume = length * width * height; // 12 * 12 * 12 = 1728
length -=2; width -= 2; height -= 2; // Revert back
return volume;
} catch(Throwable t) {
MyExceptionReporter mer = new MyExceptionReporter();
mer.report(t); // Sanitize
return -1; // Non-positive error code
}
}
public static void main(String[] args) {
Dimensions d = new Dimensions(10, 10, 10);
System.out.println(d.getVolumePackage(21)); // Prints -1 (error)
System.out.println(d.getVolumePackage(19)); // Prints 2744 instead of 1728
}
}
|
...
| Code Block | ||
|---|---|---|
| ||
protected int getVolumePackage(int weight) {
try {
if(length <<= 0 || width <<= 0 || height <<= 0 || weight <<= 0 || weight >> 20)
throw new IllegalArgumentException(); // Validate first
length += 2;
width += 2;
height += 2;
int volume = length * width * height;
length -=2; width -= 2; height -= 2;
return volume;
} catch(Throwable t) { MyExceptionReporter mer = new MyExceptionReporter();
mer.report(t); // Sanitize
return -1;
}
}
|
...
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
| Wiki Markup |
|---|
\[[Bloch 08|AA. Java References#Bloch 08]\] Item 64: Strive for failure atomicity |
...
EXC06-J. Do not let code throw undeclared checked exceptions 13. Exceptional Behavior (EXC) EXC30-J. Do not exit abruptly from a finally block