You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 78 Next »

The contracts of the read methods for the InputStream and Reader families are complicated. According to the Java API [[API 2006]] for the class InputStream, the read(byte[], int, int) method provides the following behavior:

The default implementation of this method blocks until the requested amount of input data len has been read, end of file is detected, or an exception is thrown. Subclasses are encouraged to provide a more efficient implementation of this method.

However, the read(byte[]) method states that it

Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. The number of bytes read is, at most, equal to the length of b.

The read() methods return as soon as they find available input data. As a result, these methods can stop reading data before the array is filled because there might not be enough data available to fill the array.

Ignoring the result returned by the read() methods is a violation of rule EXP00-J. Do not ignore values returned by methods. Security issues can arise even when return values are considered because the default behavior of the read() methods lacks any guarantee that the entire buffer array is filled. Consequently, when using read() to fill an array, the program must check the return value of read(), and handle the case where the array is only partially filled. In such cases, the program may try to fill the rest of the array, or work only with the subset of the array that was filled, or throw an exception.

This rule applies only to read() methods that take an array argument. To read a single byte, use the InputStream.read() method that takes no arguments and returns an int. To read a single character, use a Reader.read() method that takes no arguments and returns the character read as an int.

Noncompliant Code Example (read())

This noncompliant code example attempts to read 1024 bytes encoded in UTF-8 from an InputStream and to return them as a String. It explicitly specifies the the encoding to build the string, in compliance with IDS13-J. Use compatible encodings on both sides of file or network IO.

public static String readBytes(InputStream in) throws IOException {
  byte[] data = new byte[1024];
  if (in.read(data) == -1) {
    throw new EOFException();
  }
  return new String(data, "UTF-8");
}

The programmer's misunderstanding of the general contract of the read() method can result in failure to read the intended data in full. It is possible that the data is less than 1024 bytes long, with additional data available from the InputStream.

Compliant Solution (Multiple Calls to read())

This compliant solution reads all the desired bytes into its buffer, accounting for the total number of bytes read and adjusting the remaining bytes' offset, consequently ensuring that the required data is read in full. It also avoids splitting multibyte encoded characters across buffers by deferring construction of the result string until the data has been fully read. (See IDS10-J. Do not assume every character in a string is the same size for more information.)

public static String readBytes(InputStream in) throws IOException {
  int offset = 0;
  int bytesRead = 0;
  byte[] data = new byte[1024];
  while ((bytesRead = in.read(data, offset, data.length - offset)) != -1) {
    offset += bytesRead;
    if (offset >= data.length)
      break;
  }
  String str = new String(data, "UTF-8");
  return str;
}

Compliant Solution (readFully())

The no-argument and one-argument readFully() methods of the DataInputStream class guarantee that they read all of the requested data or throw an exception. These methods throw EOFException if they detect the end of input before the required number of bytes have been read; they throw IOException if some other input/output error occurs.

public static String readBytes(FileInputStream fis) throws IOException {
  byte[] data = new byte[1024];
  DataInputStream dis = new DataInputStream(fis);
  dis.readFully(data);
  String str = new String(data, "UTF-8");
  return str;
}

Risk Assessment

Failure to comply with this rule can result in the wrong number of bytes being read or character sequences being interpreted incorrectly.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

FIO10-J

low

unlikely

medium

P2

L3

Related Guidelines

MITRE CWE

CWE-135, "Incorrect Calculation of Multi-Byte String Length"

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="d231c78b-660e-4d51-b28d-bf8054553753"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API 06]]

Class InputStream, DataInputStream

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

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4a947cdf-695e-4e30-bca4-d423d526efcd"><ac:plain-text-body><![CDATA[

[[Chess 2007

AA. Bibliography#Chess 07]]

8.1 Handling Errors with Return Codes

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

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="cf35f451-c3c0-4d3b-881a-c667a592d173"><ac:plain-text-body><![CDATA[

[[Harold 1999

AA. Bibliography#Harold 99]]

Chapter 7: Data Streams, Reading Byte Arrays

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

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="dae66ca7-0644-4a9f-aa8a-f82f5215934d"><ac:plain-text-body><![CDATA[

[[Phillips 2005

AA. Bibliography#Phillips 05]]

 

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


      12. Input Output (FIO)      FIO11-J. Do not attempt to read raw binary data as character data

  • No labels