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

FileImageOutputStream NegativeArraySizeException for large arrays

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 1.4.0, 7u25
    • Fix Version/s: tbd
    • Component/s: client-libs
    • Labels:

      Description

      FULL PRODUCT VERSION :
      java version " 1.7.0_25 "
      Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
      Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux 3.4.47-2.38-desktop #1 SMP PREEMPT Fri May 31 20:17:40 UTC 2013 (3961086) x86_64 x86_64 x86_64 GNU/Linux
      Window 8
      All other OSes

      A DESCRIPTION OF THE PROBLEM :
      The largest array size you can allocate in Java is Integer.MAX_VALUE as arrays are int indexed, for any array type (byte, int, float, double, etc.)
      However javax.imageio.stream.FileImageOutputStream while trying to write the array is using int for array size in bytes.
      Therefore, you can write any working array length of type byte (even byte[Integer.MAX_VALUE]) but you get the java.lang.NegativeArraySizeException when trying to write float arrays longer than Integer.MAX_VALUE/4 using writeFloats method, or double arrays longer than Integer.MAX_VALUE/8 using writeDoubles method.




      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Try allocating float array of sie larger than Integer.MAX_VALUE/4 and writting it with javax.imageio.stream.FileImageOutputStream.writeFloats, or double array of sie larger than Integer.MAX_VALUE/8 and writting it with javax.imageio.stream.FileImageOutputStream.writeDoubles

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      javax.imageio.stream.FileImageOutputStream.writeFloats and javax.imageio.stream.FileImageOutputStream.writeDoubles methods should work correctly for arrays up to Integer.MAX_VALUE length.
      In the worst case scenario an Exception informing on too large size of array should be thrown.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread " main " java.lang.NegativeArraySizeException
      at javax.imageio.stream.ImageOutputStreamImpl.writeFloats(ImageOutputStreamImpl.java:328)
      at javaapplication4.WriteTest.main(WriteTest.java:24)
      Java Result: 1

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      int N = Integer.MAX_VALUE/4+1;
      float[] largeFloatArray = new float[N];
      FileImageOutputStream outFloat = new FileImageOutputStream(new File( " /tmp/testFloat.dat " ));
      outFloat.writeFloats(largeFloatArray, 0, largeFloatArray.length);
      outFloat.close();

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Write in chunks of smaller size in loop.

        Attachments

          Activity

            People

            • Assignee:
              jdv Jayathirth D V
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated: