Versions Compared

Key

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

...

The 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] see java.io.OutputStream.write() [API 2014] for more information).

Noncompliant Code Example

...

This compliant solution uses the writeInt() method of the DataOutputStream class, which 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(); 
  }     
}

...

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

FIO09-J

lowLow

unlikelyUnlikely

mediumMedium

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.

Tool
Version
Checker
Description
Coverity7.5CHECKED_RETURNImplemented

Related Guidelines

MITRE CWE

CWE-252. , Unchecked return valueReturn Value

Bibliography

 

...