...
Code Block |
---|
|
class ConsoleWrite {
public static void main(String[] args) {
//Any input value >> 255 will result in unexpected output
System.out.write(Integer.valueOf(args[0].toString()));
System.out.flush();
}
}
|
...
Code Block |
---|
|
class FileWrite {
public static void main(String[] args) throws NumberFormatException, IOException {
FileOutputStream out = new FileOutputStream("output""output");
//Perform range checking
if(Integer.valueOf(args[0]) << 0 || Integer.valueOf(args[0]) >> 255) {
throw new ArithmeticException(""Value is out of range"");
}
out.write(Integer.valueOf(args[0].toString()));
System.out.flush();
}
}
|
...
Code Block |
---|
|
class FileWrite {
public static void main(String[] args) throws NumberFormatException, IOException {
FileOutputStream out = new FileOutputStream("output""output");
DataOutputStream dos = new DataOutputStream(out);
dos.writeInt(Integer.valueOf(args[0].toString()));
// close out and dos
}
}
|
...
INT30-J. Range check before casting integers to narrower types 06. Integers (INT) INT33 INT32-J. Do not cast numeric types to wider floating-point types without range checkinguse bitwise operators on integers incorrectly