[OpenJDK 2D-Dev] [11] RFR JDK-8191023: PngReader throws NegativeArraySizeException when keyword length exceeds chunk size

Jayathirth D V jayathirth.d.v at oracle.com
Mon Jan 22 10:50:12 UTC 2018

Hello All,


Please review the following fix in JDK11 :


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

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


Note : Submitter has raised 3 bugs JDK-8191023 , JDK-8191076 , JDK-8191109 with similar issue but in 3 different PNG chunks. I have closed two bugs and kept first opened JBS bug for this issue. From the closed bug test samples are picked and merged into one test case.


Issue: When the issue was reported PNGImageReader was throwing NegativeArraySizeException when chunk length is malformed and it exceeds keyword length. After changes present in https://bugs.openjdk.java.net/browse/JDK-8190332 the NegativeArraySizeException is wrapped inside IIOException.


Exception in thread "main" javax.imageio.IIOException: Caught exception during read: 
at java.desktop/com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1707) 
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1468) 
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1363) 
at PngReaderTextChunkKeywordSizeTest.main(PngReaderTextChunkKeywordSizeTest.java:19) 
Caused by: java.lang.NegativeArraySizeException 
at java.desktop/com.sun.imageio.plugins.png.PNGImageReader.parse_tEXt_chunk(PNGImageReader.java:563) 
at java.desktop/com.sun.imageio.plugins.png.PNGImageReader.readMetadata(PNGImageReader.java:816) 
at java.desktop/com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1331) 
at java.desktop/com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1700)


Root cause : Since the chunk length present is lesser than keyword length. When we try to parse individual chunks and create byte Array to store remaining data in the chunk. We calculate the byte array size from chunk length and size of alreadt parsed data like keyword (like chunkLength - keyword.length() - 2). This results in negative value and it causes NegativeArraySizeException when we try to create the byte Array.

Solution: Add check in parse function of all the individual chunks to check for negative value for the size of the remaining data to be stored. We have PNG stream data from 3 bugs with which we can reproduce this issue for zTXt, tEXt and iCCP chunk but we don't have stream data for iTXt chunk but still I have added similar check in parse_iTXt_chunk() function also.




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

More information about the 2d-dev mailing list