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

"BufferedImage ImageIO.read(File input)" fails, throwing ArrayIndexOutOfBoundsException exception

    XMLWordPrintable

Details

    Description

      FULL PRODUCT VERSION :
      openjdk version "1.8.0_151"
      OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.17.10.2-b12)
      OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Ubuntu 17.10

      A DESCRIPTION OF THE PROBLEM :
      For certain inputs, ImageIO read fails:
      Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
      at java.util.ArrayList.elementData(ArrayList.java:422)
      at java.util.ArrayList.get(ArrayList.java:435)
      at com.sun.imageio.plugins.jpeg.JPEGImageReader.checkTablesOnly(JPEGImageReader.java:378)
      at com.sun.imageio.plugins.jpeg.JPEGImageReader.gotoImage(JPEGImageReader.java:492)
      at com.sun.imageio.plugins.jpeg.JPEGImageReader.readHeader(JPEGImageReader.java:613)
      at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1070)
      at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1050)
      at javax.imageio.ImageIO.read(ImageIO.java:1448)
      at javax.imageio.ImageIO.read(ImageIO.java:1308)
      at image_io_write.Main.main(Main.java:17)



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a file with the following binary input (hexdump output):
      d8ff d8d8 d8d8 d8d8 d8d8 d8d8 d8d8 d8d8
      d8d8 d8d8 d8d8 d8d8 d8d8 d8d8 d8d8 d8d8
      d8d8 d8d8 d8d8 d8d8 d8d8 d8d8 7fd8 ffff

      Execute the following code, where "file" is a File object, with its content described above:

      BufferedImage img = null;
      try {
          img = ImageIO.read(file);
      } catch (IOException e) {
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      read should fail by returning null, or by throwing an IOException, as described in the official documentation.
      ACTUAL -
      fails, throwing ArrayIndexOutOfBoundsException exception

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
      at java.util.ArrayList.elementData(ArrayList.java:422)
      at java.util.ArrayList.get(ArrayList.java:435)
      at com.sun.imageio.plugins.jpeg.JPEGImageReader.checkTablesOnly(JPEGImageReader.java:378)
      at com.sun.imageio.plugins.jpeg.JPEGImageReader.gotoImage(JPEGImageReader.java:492)
      at com.sun.imageio.plugins.jpeg.JPEGImageReader.readHeader(JPEGImageReader.java:613)
      at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1070)
      at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1050)
      at javax.imageio.ImageIO.read(ImageIO.java:1448)
      at javax.imageio.ImageIO.read(ImageIO.java:1308)
      at image_io_write.Main.main(Main.java:17)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      byte[] bytes = {(byte)0xFF,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0xD8,(byte)0x7F,(byte)0xFF,(byte)0xFF};
          
           File file = new File("testfile");
           FileOutputStream fos;
      try {
      fos = new FileOutputStream(file);

      fos.write(bytes);
      fos.flush();
      fos.close();


      BufferedImage img = null;
      try {
      img = ImageIO.read(file);
      } catch (IOException e) {
      }
      System.out.println(img);

      } catch (FileNotFoundException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      ---------- END SOURCE ----------

      Attachments

        1. Main.java
          0.6 kB
        2. testfile
          0.0 kB

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved: