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

java.io.ByteArrayOutputStream.write(b,off,len) works wrong with big len

XMLWordPrintable

    • 1.2beta2
    • sparc
    • solaris_2.5
    • Verified



      Name: saC57035 Date: 12/01/96


      This bug was found by St.Petersburg Java SQE team (by Alexander Kuzmin).


      java.io.ByteArrayOutputStream write(byte[] b,int off,int len) works wrong
      with huge len. It throws OutOfMemoryError instead of IndexOutOfBoundsException
      in this case.


      The Java Language Specification says:

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

      Elements b[off] through b[off+len-1] are appended to the internal buffer.

      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.

      Overrides the write method of OutputStream (§22.15.3)."


      Here is the example demonstrating the bug:

      _______________________bug_test.java______________________________________
      import java.io.*;

      public class bug_test {
        public static void main( String argv[] ) {

       ByteArrayOutputStream y1;
       byte array1[]={'1','2','3','4','5'};// Simple array
       
         y1 = new ByteArrayOutputStream(5); //Create new ByteArrayOutputStream object
       
       try {
       y1.write(array1,0,Integer.MAX_VALUE);// Out of bounds
       } catch (IndexOutOfBoundsException e) // Must throw IndexOutOfBoundsException
        { System.out.println("OKAY");
        }
         catch (Throwable e)
        { System.out.println(" Not IndexOutOfBoundsException thrown, but "+e);
        }
         
        // Otherwise
        System.out.println("Otherwise:");
       

       try {
       y1.write(array1,0,array1.length+100);// Out of bounds
       } catch (IndexOutOfBoundsException e) // Must throw IndexOutOfBoundsException
        { System.out.println("OKAY");
        }
         catch (Throwable e)
        { System.out.println(" Not IndexOutOfBoundsException thrown, but "+e);
        }
       

        }
      }

      ____________________________ Output __________________________________

       Not IndexOutOfBoundsException thrown, but java.lang.OutOfMemoryError
      Otherwise:
      OKAY
      ______________________________________________________________________

      ======================================================================

            dviswanasunw Deepa Viswanathan (Inactive)
            savzan Stanislav Avzan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: