[OpenJDK 2D-Dev] [11] RFR JDK-8191073: JpegImageReader throws IndexOutOfBoundsException when trying to read image data from tables-only image

Jayathirth D V jayathirth.d.v at oracle.com
Thu Jan 4 07:06:59 UTC 2018

Hello All,


Please review the following fix in JDK11 :


Bug : https://bugs.openjdk.java.net/browse/JDK-8191073 

Webrev : http://cr.openjdk.java.net/~jdv/8191073/webrev.00/ 


Issue: When we try to read image data from JPEG input stream having tables-only information JPEGImageReader throws IndexOutOfBoundsException.


Exception in thread "main" java.lang.IndexOutOfBoundsException: Index -1 out-of-bounds for length 0 
        at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) 
        at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) 
        at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) 
        at java.base/java.util.Objects.checkIndex(Objects.java:372) 
        at java.base/java.util.ArrayList.get(ArrayList.java:440) 
        at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageReader.checkTablesOnly(JPEGImageReader.java:378) 
        at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageReader.gotoImage(JPEGImageReader.java:493) 
        at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageReader.readHeader(JPEGImageReader.java:716) 
        at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1173) 
        at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1153) 
        at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1468) 
        at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1363) 
        at JpegReaderOOBIssue.main(JpegReaderOOBIssue.java:25)


Root cause: readNativeHeader() function returns the first header as tables-only image in checkTablesOnly() function. Because of this we don't update the start of this stream position as one of the imagePositions. After that in checkTablesOnly() we try to find any image data after stream metadata using hasNextImage() and it also returns false. So we have a input stream which contains only streamMetadata(tables-only image).
In the same checkTablesOnly() we try to get initial imagePosition for this input stream in seekForwardOnly case. But since it is empty and we try "imagePositions.get(imagePositions.size()-1)" it throws IndexOutOfBoundsException.



We should make changes at 2 places to fix this issue :

1) In checkTablesOnly() function if seekForward flag is enabled and the input stream is just tables-only we should not try to access imagePositions variable as it will be empty. imagePositions list is in 1:1 relationship with number on image indices available in given input stream. 

2) In checkTablesOnly() function when we get to know that the given input stream is just tables only image we should maintain that state in a boolean variable like "tablesOnlyStream" so that if we try to access image information in gotoImage() we should throw an IIOException mentioning that there is no image data available.




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/2d-dev/attachments/20180103/6dcf22be/attachment.html>

More information about the 2d-dev mailing list