...
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; } } |
...
EXC06-J. Do not let code throw undeclared checked exceptions 13. Exceptional Behavior (EXC) EXC30 EXC08-J. Do not exit abruptly from a finally blockUse a logging API to log critical security exceptions