Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8279638

DataInputStream.readFully(byte[], int, int) does not throw expected IndexOutOfBoundsExceptions

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 16
    • core-libs
    • None
    • behavioral
    • low
    • Exceptions could be thrown where previously there were silent failures.
    • Java API
    • SE

      Summary

      Modify the implementation of DataInputStream.readFully(byte[] b, int off, int len) to throw NullPointerException and IndexOutOfBoundsException as described in its specification,

      Problem

      DataInputStream.readFully(byte[] b, int off, int len) is specified to throw a NullPointerException if b is null, and an IndexOutOfBoundsException if off is negative, len is negative, or len is greater than b.length - off. For example if b was null and len was zero, then the method would fail silently without throwing a NullPointerException. Also if b was non-null, off was negative and len was zero, then the method would fail silently without throwing an IndexOutOfBoundsException.

      Solution

      Use Objects.checkFromIndexSize(int,int,int) to validate the parameters.

      --- a/src/java.base/share/classes/java/io/DataInputStream.java  Fri Aug 07 20:39:10 2020 +0100
      +++ b/src/java.base/share/classes/java/io/DataInputStream.java  Fri Aug 07 12:58:40 2020 -0700
      @@ -25,6 +25,8 @@
      +import java.util.Objects;
      +
      /**
       * A data input stream lets an application read primitive Java data
       * types from an underlying input stream in a machine-independent
      @@ -192,8 +194,7 @@
          * @see        java.io.FilterInputStream#in
          */
          public final void readFully(byte b[], int off, int len) throws IOException {
      -        if (len < 0)
      -            throw new IndexOutOfBoundsException();
      +        Objects.checkFromIndexSize(off, len, b.length);
               int n = 0;
               while (n < len) {
                   int count = in.read(b, off + n, len - n);

      Specification

      No specification change.

            bpb Brian Burkhalter
            bpb Brian Burkhalter
            Alan Bateman
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: