Versions Compared

Key

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

...

The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored. Java Specification[JLS 05]

Noncompliant Code Example

The noncompliant code example accepts a value from the user without validating it. If this value is greater than 255, it will result in a wrap around. For instance, write(305) will print '1' since the lower order bits of 305 are preserved while the top 24 order bits are lost. That is, the result is remainder modulo 256 of the absolute value of the input.

Code Block
bgColor#FFcccc
class console_writeConsoleWrite {
  public static void main(String[] args) { 
    //Any input value > 255 will result in unexpected output
    System.out.write(args[0]);
  }
}

Compliant Solution

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

Code Block
bgColor#ccccff
class console_writeConsoleWrite {
  public static void main(String[] args) { 
    //Perform input validation
    if(args[0] <= 255) {
      System.out.print(args[0]);
    }
    else {
      //handle error 
    } 
  }
}

Risk Assessment

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

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

INT31 INT01-J

?? low ??

unlikely

?? medium

P??

L??

Automated Detection

TODO

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

References

Wiki Markup
Java I/O, Elliotte Rusty Harold
\[[Harold 99|AA. Java References#Harold 99]\]