Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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]));
    System.out.flush();
  }
}

Compliant Solution

Use alternate alternative means to output integers such as the System.out.print* methods. Again, performing input validation is extremely critical

Code Block
bgColor#ccccff

class ConsoleWrite {
  public static void main(String[] args) { 
    System.out.println(args[0]);
  }
}

Compliant Solution 2

Alternatively, perform input validation.

Code Block
bgColor#ccccff
class ConsoleWrite {
  public static void main(String[] args) { 
    //Perform input validation
    if(Integer.valueOf(args[0]) <= 255) {
      System.out.write(Integer.printvalueOf(args[0]));
      System.out.flush();
    }
    else {
      //handle error 
      throw new ArithmeticException("Value is out of range");
    } 
  }
}

Risk Assessment

Using the write() method to output integers may result in unexpected values.

...