Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by NavBot

...

Code Block
bgColor#FFcccc
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
bgColor#ccccff
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]) &lt;< 0 || Integer.valueOf(args[0]) &gt;> 255) {
      throw new ArithmeticException(&quot;"Value is out of range&quot;");
    }
  
    out.write(Integer.valueOf(args[0].toString()));
    System.out.flush(); 
  }
}

...

Code Block
bgColor#ccccff
class FileWrite {
  public static void main(String[] args) throws NumberFormatException, IOException { 
    FileOutputStream out = new FileOutputStream(&quot;output&quot;"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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      06. Integers (INT)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INT33      INT32-J. Do not cast numeric types to wider floating-point types without range checkinguse bitwise operators on integers incorrectly