ArrayIndexOutOfBoundsException: 4096 while reading gif file

Venkat Janyavula picture Venkat Janyavula · Mar 7, 2014 · Viewed 8k times · Source

I am able to read png file. But getting ArrayIndexOutOfBoundsException: 4096 while reading gif file.

byte[] fileData = imageFile.getFileData();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fileData);
RenderedImage image = ImageIO.read(byteArrayInputStream)

Exception thrown looks like

java.lang.ArrayIndexOutOfBoundsException: 4096
    at com.sun.imageio.plugins.gif.GIFImageReader.read(Unknown Source)
    at javax.imageio.ImageIO.read(Unknown Source)
    at javax.imageio.ImageIO.read(Unknown Source)

what could be the issue and what is the resolution?

Answer

Jack picture Jack · May 25, 2014

Update 3: Solution

I ended up developing my own GifDecoder and released it as open source under the Apache License 2.0. You can get it from here: https://github.com/DhyanB/Open-Imaging. It does not suffer from the ArrayIndexOutOfBoundsException issue and delivers decent performance.

Any feedback is highly appreciated. In particular, I'd like to know if it works correctly for all of your images and if you are happy with its speed.

I hope this is helpful to you (:

Initial answer

Maybe this bug report is related to or describes the same problem: https://bugs.openjdk.java.net/browse/JDK-7132728.

Quote:

FULL PRODUCT VERSION :
java version "1.7.0_02"
Java(TM) SE Runtime Environment (build 1.7.0_02-b13)
Java HotSpot(TM) 64-Bit Server VM (build 22.0-b10, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

A DESCRIPTION OF THE PROBLEM :
according to specification
http://www.w3.org/Graphics/GIF/spec-gif89a.txt
> There is not a requirement to send a clear code when the string table is full.

However, GIFImageReader requires the clear code when the string table is full.
GIFImageReader violates the specification, clearly.
In the real world, sometimes people finds such high compressed gif image.
so you should fix this bug.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
javac -cp .;PATH_TO_COMMONS_CODEC GIF_OverflowStringList_Test.java
java -cp .;PATH_TO_COMMONS_CODEC GIF_OverflowStringList_Test

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
complete normally. no output
ACTUAL -
ArrayIndexOutOfBounds occurs.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4096
        at com.sun.imageio.plugins.gif.GIFImageReader.read(GIFImageReader.java:1
075)
        at javax.imageio.ImageIO.read(ImageIO.java:1400)
        at javax.imageio.ImageIO.read(ImageIO.java:1322)
        at GIF_OverflowStringList_Test.main(GIF_OverflowStringList_Test.java:8)


REPRODUCIBILITY :
This bug can be reproduced always.

The bug report also provides code to reproduce the bug.

Update 1

And here is an image that causes the bug in my own code:

enter image description here

Update 2

I tried to read the same image using Apache Commons Imaging, which led to the following exception:

java.io.IOException: AddStringToTable: codes: 4096 code_size: 12
    at org.apache.commons.imaging.common.mylzw.MyLzwDecompressor.addStringToTable(MyLzwDecompressor.java:112)
    at org.apache.commons.imaging.common.mylzw.MyLzwDecompressor.decompress(MyLzwDecompressor.java:168)
    at org.apache.commons.imaging.formats.gif.GifImageParser.readImageDescriptor(GifImageParser.java:388)
    at org.apache.commons.imaging.formats.gif.GifImageParser.readBlocks(GifImageParser.java:251)
    at org.apache.commons.imaging.formats.gif.GifImageParser.readFile(GifImageParser.java:455)
    at org.apache.commons.imaging.formats.gif.GifImageParser.readFile(GifImageParser.java:435)
    at org.apache.commons.imaging.formats.gif.GifImageParser.getBufferedImage(GifImageParser.java:646)
    at org.apache.commons.imaging.Imaging.getBufferedImage(Imaging.java:1378)
    at org.apache.commons.imaging.Imaging.getBufferedImage(Imaging.java:1292)

That looks very similar to the problem we have with ImageIO, so I reported the bug at the Apache Commons JIRA: https://issues.apache.org/jira/browse/IMAGING-130.