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

OutputStream.write(byte[],int,int) could have fewer parameter bounds checks

XMLWordPrintable

    • b34
    • Not verified

      OutputStream.write(byte[],int,int) has the bounds checks

              if ((off < 0) || (off > b.length) || (len < 0) ||
                         ((off + len) > b.length) || ((off + len) < 0)) {
                  throw new IndexOutOfBoundsException();
              }

      whereas InputStream.read(byte[],int,int) has

              if (off < 0 || len < 0 || len > b.length - off) {
                  throw new IndexOutOfBoundsException();
              }

      which appear to subsume the checks in OS::write. The write() checks should be changed to the set used in read(). With this change, the OutputStream::write documentation

           * If <code>off</code> is negative, or <code>len</code> is negative, or
           * <code>off+len</code> is greater than the length of the array
           * {@code b}, then an {@code IndexOutOfBoundsException} is thrown.

      would no longer correspond exactly to the implementation but would not be strictly inaccurate so no change to it should be necessary.

            bpb Brian Burkhalter
            bpb Brian Burkhalter
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: