[OpenJDK 2D-Dev] RFR JDK-8191174: PngReader throws IllegalArgumentException because ScanlineStride calculation logic is not proper

Jayathirth D V jayathirth.d.v at oracle.com
Thu Dec 14 08:18:01 UTC 2017

Hello All,


Please review the following fix in JDK :


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

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


Issue : When we try to read PNG image with large width we throw undocumented IllegalArgumentException with message "Pixel stride times width must be less than or equal to the scanline stride".


Exception in thread "main" java.lang.IllegalArgumentException: Pixel stride times width must be less than or equal to the scanline stride

                at java.desktop/java.awt.image.PixelInterleavedSampleModel.<init>(PixelInterleavedSampleModel.java:101)

                at java.desktop/java.awt.image.Raster.createInterleavedRaster(Raster.java:642)

                at java.desktop/com.sun.imageio.plugins.png.PNGImageReader.createRaster(PNGImageReader.java:974)

                at java.desktop/com.sun.imageio.plugins.png.PNGImageReader.decodePass(PNGImageReader.java:1099)

                at java.desktop/com.sun.imageio.plugins.png.PNGImageReader.decodeImage(PNGImageReader.java:1295)

                at java.desktop/com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1420)

                at java.desktop/com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1699)

                at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1468)

                at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1363)

                at PngReaderLargeWidthStrideTest.main(PngReaderLargeWidthStrideTest.java:50)


Root cause : We use large width present in IHDR and calculate elements per row which is passed as scanlinestride for creating the required raster and its corresponding sample model.  When the call reaches creation of PixelInterleavedSampleModel it checks the condition of (pixelStride * w) > scanlineStride. Since in our case we pass this condition it throws IllegalArgumentException. We have invalid scanlineStride value because when we calculate elements per row/bytes per row value in PNGImageReader the integer variable buffer is overflowed and we maintain invalid value for bytesPerRow.


Solution : We can maintain the intermediate bitsPerRow value in long datatype and calculate bytesPerRow using the long value and then typecast it to int bytesPerRow variable. By doing this we will maintain the required scanlineStride/eltsPerRow value properly. After this solution we will throw proper IIOException because of changes present in HYPERLINK "https://bugs.openjdk.java.net/browse/JDK-8190332"JDK-8190332 and not the undocumented IllegalArgumentException.




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/2d-dev/attachments/20171214/945c5aa5/attachment-0001.html>

More information about the 2d-dev mailing list