redwarp / gifdecoder

An implementation of a gif decoder written 100% in Kotlin, plus an associated Drawable for Android
Apache License 2.0
47 stars 6 forks source link

ArrayIndexOutOfBoundsException: length=4096; index=4096 at LzwDecoder.decode() #29

Open vince-styling opened 7 months ago

vince-styling commented 7 months ago

Hi, the decode step would cause ArrayIndexOutOfBoundsException sometime

private const val MAX_STACK_SIZE = 4096
private val prefix = ShortArray(MAX_STACK_SIZE)

fun decode(imageData: ByteArray, destination: ByteArray, pixelCount: Int) {
val lzwMinimumCodeSize = imageData[dataIndex]
val clear: Int = 1.shl(lzwMinimumCodeSize.toInt())

for (code in 0 until clear) {
    prefix[code] = 0
    suffix[code] = code.toByte()
}

once lzwMinimumCodeSize's value greater than 12, I can't make sure that gif file is corrupted or not due to it happened from my user's devices, but it is infrequent, the last report was two months ago, about 20 times totally, probably the files was broken.

just to ask if you have any idea.

redwarp commented 7 months ago

Seems about right! I believe the gif to be corrupted: lzw minimum code size can't be > 12, according to lzw specs. So, if for some reason, it is, then a failure would be expected.

vince-styling commented 5 months ago

1718881878793 hi,I found an example eventually for this issue,take a look

redwarp commented 5 months ago

Thanks! I'll take a look.