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

java.io.XXXXOutputStream.write(b,off,len) methods don't work with len

XMLWordPrintable

    • 1.2beta4
    • sparc
    • solaris_2.5
    • Not verified



      Name: saf@russia Date: 09/09/96


      This bug was found by St.Petersburg Java SQE team (by Stanislav Avzan).

      The java.io.PipedOutputStream.write(b,off,len) method does not work with incorrect
      values of len according to the Java language specification.

      The Java Language specification
      (Version 1.0 - August 1, 1996)
      says the following (please see item 22.3.3):

      "22.15.3 public void write(byte[] b, int off, int
      len)
      throws IOException, NullPointerException,
      IndexOutOfBoundsException

      The general contract for write(b, off, len) is that some of the bytes
      in the array b are written to the output stream as if one at a time, in order;
      element b[off] is the first byte written and b[off+len-1] is the last byte
      written by this operation.

      If b is null, a NullPointerException is thrown.

      If off is negative, or len is negative, or off+len is greater than the
      length of the array b, then an IndexOutOfBoundsException is thrown.
      <....>"

      So this method should throw IndexOutOfBoundsException and perform no writings
      if len is negative or len is greater than b.length-off.
      But in fact it does not.
       
      Here is the minimized test demonstrating the bug:
          

      ----- test14.java ---------------------------------------
      import java.io.*;

      public class test14 {

         public static void main( String[] argv ) {
           PipedOutputStream os = new PipedOutputStream();
           PipedInputStream is = new PipedInputStream();
           try {
             is.connect(os);
           } catch(Throwable e) {
             System.out.println("Test failed: unexpected <"+e+"> thrown");
           }
           System.out.println("Testing len equals to -5 and 15 for array of size 10");
           byte[] b = new byte[10];
           try {
             // should throw IndexOutOfBoundsException
             os.write(b,0,-5);
             System.out.println("len = -5: IndexOutOfBoundsException not thrown");

           } catch(IndexOutOfBoundsException e) {
             System.out.println("len = -5: IndexOutOfBoundsException thrown");
           } catch(Throwable e) {
             System.out.println("Test failed: unexpected <"+e+"> thrown");
           }
           PipedOutputStream os2 = new PipedOutputStream();
           PipedInputStream is2 = new PipedInputStream();
           try {
             is2.connect(os2);
           } catch(Throwable e) {
             System.out.println("Test failed: unexpected <"+e+"> thrown");
           }
           try {
             // should throw IndexOutOfBoundsException
             os2.write(b,0,15);
             System.out.println("len = 15: IndexOutOfBoundsException not thrown");

           } catch(IndexOutOfBoundsException e) {
             try {
               // needed exception is thrown. Let's test no bytes are written
               // available() must print 0
               System.out.println("len = 15: available() returns "+is2.available());
             } catch(IOException e1) {}
           } catch(Throwable e) {
             System.out.println("Test failed: unexpected <"+e+"> thrown");
           }
           
         }
      }

      ----- The output of the test: -------------------------

      $JAVA test14
      Testing len equals to -5 and 15 for array of size 10
      len = -5: IndexOutOfBoundsException not thrown
      len = 15: available() returns 10

      -------------------------------------------------------


      Workaround:
      None
      ======================================================================

      Name: saC57035 Date: 11/27/96


      This bug was found by St.Petersburg Java SQE team (by Stanislav Avzan).

      The java.io.OutputStream.write(b,off,len) and java.io.PipedOutputStream.write(b,off,len)
      methods do not work with incorrect values of len according to the Java language
      specification.

      The Java Language specification
      (Version 1.0 - August 1, 1996)
      says the following (please see item 22.3.3):

      "22.15.3 public void write(byte[] b, int off, int
      len)
      throws IOException, NullPointerException,
      IndexOutOfBoundsException

      The general contract for write(b, off, len) is that some of the bytes
      in the array b are written to the output stream as if one at a time, in order;
      element b[off] is the first byte written and b[off+len-1] is the last byte
      written by this operation.

      If b is null, a NullPointerException is thrown.

      If off is negative, or len is negative, or off+len is greater than the
      length of the array b, then an IndexOutOfBoundsException is thrown.
      <....>"

      So this method should throw IndexOutOfBoundsException and perform no writings
      if len is negative or len is greater than b.length-off.
      But in fact it does not.
       
      Here is the minimized test demonstrating the bug for PipedOutputStream:
          

      ----- test14.java ---------------------------------------
      import java.io.*;

      public class test14 {

         public static void main( String[] argv ) {
           PipedOutputStream os = new PipedOutputStream();
           PipedInputStream is = new PipedInputStream();
           try {
             is.connect(os);
           } catch(Throwable e) {
             System.out.println("Test failed: unexpected <"+e+"> thrown");
           }
           System.out.println("Testing len equals to -5 and 15 for array of size 10");
           byte[] b = new byte[10];
           try {
             // should throw IndexOutOfBoundsException
             os.write(b,0,-5);
             System.out.println("len = -5: IndexOutOfBoundsException not thrown");

           } catch(IndexOutOfBoundsException e) {
             System.out.println("len = -5: IndexOutOfBoundsException thrown");
           } catch(Throwable e) {
             System.out.println("Test failed: unexpected <"+e+"> thrown");
           }
           PipedOutputStream os2 = new PipedOutputStream();
           PipedInputStream is2 = new PipedInputStream();
           try {
             is2.connect(os2);
           } catch(Throwable e) {
             System.out.println("Test failed: unexpected <"+e+"> thrown");
           }
           try {
             // should throw IndexOutOfBoundsException
             os2.write(b,0,15);
             System.out.println("len = 15: IndexOutOfBoundsException not thrown");

           } catch(IndexOutOfBoundsException e) {
             try {
               // needed exception is thrown. Let's test no bytes are written
               // available() must print 0
               System.out.println("len = 15: available() returns "+is2.available()+" instead of 0");
             } catch(IOException e1) {}
           } catch(Throwable e) {
             System.out.println("Test failed: unexpected <"+e+"> thrown");
           }
           
         }
      }

      ----- The output of the test: -------------------------

      $JAVA test14
      Testing len equals to -5 and 15 for array of size 10
      len = -5: IndexOutOfBoundsException not thrown
      len = 15: available() returns 10 instead of 0

      ------------------------------------------------------
      ======================================================================

            dviswanasunw Deepa Viswanathan (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: