Versions Compared

Key

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

The write() method, defined in the class java.io.OutputStream, takes an argument of type int intended to be between 0 and the value of which must be in the range 0 to 255. Because a value of type int could be outside this range, failure to range check can result in the truncation of the higher-order bits of the inputargument.

Wiki MarkupThe general contract for the {{write()}} method says that it writes one byte to the output stream. The byte to be written constitutes the eight lower -order bits of the argument {{b}}, passed to the {{write()}} method; the 24 high-order bits of {{b}} are ignored (see \[[API 2006|AA. Bibliography#API 06]\] [{{(see java.io.OutputStream.write()}}|http://download.oracle.com/javase/6/docs/api/java/io/OutputStream.html#write(int)] for more [API 2014] for more information).

Noncompliant Code Example

This noncompliant code example accepts a value from the user without validating it. Any value that is not in the range of 0 to 255 is truncated. For instance, write(303) prints / on ASCII-based systems because the lower-order 8 bits of 303 are used while the 24 high-order bits are ignored (303 % 256 = 47, which is the ASCII code for /). That is, the result is the remainder of the input divided by 256.

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 (Range-Check Inputs)

Perform range checking to be compliant. This compliant solution prints the corresponding character only if the input integer is in the proper range. If the input is outside the representable range of an int, the Integer.valueOf() method throws a NumberFormatException. If the input can be represented by an int, but is outside the range required by write(), this code throws an ArithmeticException.

Code Block
bgColor#ccccff

class FileWrite {
  public static void main(String[] args) throws NumberFormatException, IOException { 

                       FileOutputStream out = new FileOutputStream("output");   

throws NumberFormatException, IOException { 
    // Perform range checking  
    int value = Integer.valueOf(args[0]);
    if (value < 0 || value > 255) {
      throw new ArithmeticException("Value is out of range");
    }
  
    System.out.write(value);
    System.out.flush(); 
  }
}

Compliant Solution (

...

writeInt())

This compliant solution uses the writeInt() method of the DataOutputStream class, which is capable of handling can output the entire range of values representable as an int.:

Code Block
bgColor#ccccff

class FileWrite {
  public static void main(String[] args)
                          throws NumberFormatException, IOException { 
    DataOutputStream dos = new DataOutputStream(System.out);
    dos.writeInt(Integer.valueOf(args[0].toString()));
    System.out.flush(); 
  }     
}

...

Using the write() method to output integers writes only the low-order 8 bits of the integers. This truncation can result in unexpected valuesoutside the range 0 to 255 will result in truncation.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

FIO09-J

low

Low

unlikely

Unlikely

medium

Medium

P2

L3

Automated Detection

Automated detection of all uses of the write() method is straightforward. Sound determination of whether the truncating behavior is correct is not feasible in the general case. Heuristic checks could be useful.

Related Guidelines

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="69c593e9-b211-4b00-a10d-1ccbf84ba278"><ac:plain-text-body><![CDATA[

[[MITRE 2009

AA. Bibliography#MITRE 09]]

[CWE-252

http://cwe.mitre.org/data/definitions/252.html] "Unchecked Return Value"

]]></ac:plain-text-body></ac:structured-macro>

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="565effcd-e188-4d79-a9e3-ca19e4b58ad9"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API 06]]

method [write()

http://java.sun.com/javase/6/docs/api/java/io/OutputStream.html#write(int)]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b77b3cc1-5103-4383-b1a8-5590f6cc435c"><ac:plain-text-body><![CDATA[

[[Harold 1999

AA. Bibliography#Harold 99]]

 

]]></ac:plain-text-body></ac:structured-macro>

Tool
Version
Checker
Description
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V

JAVA.NULL.RET.UNCHECKED

Call Might Return Null (Java)

Coverity7.5CHECKED_RETURNImplemented
Parasoft Jtest

Include Page
Parasoft_V
Parasoft_V

CERT.FIO09.ARGWRITEDo not rely on the write() method to output integers outside the range 0 to 255

Related Guidelines

MITRE CWE

CWE-252, Unchecked Return Value

Bibliography


...

Image Added Image Added FIO08-J. Use an int to capture the return value of methods that read a character or byte      12. Input Output (FIO)      Image Modified