Uh oh!
There was an error while loading. Please reload this page.
GifDecoder: Limit lzw bits to a maximum of 12 bits - #2744
Conversation
| // which may leave a gap in the codes where no colors are assigned. | ||
| // http://www.matthewflickinger.com/lab/whatsinagif/lzw_image_data.asp#lzw_compression | ||
| if (minCodeSize < 2 || clearCode > MaxStackSize) | ||
| if (minCodeSize < 2 || minCodeSize > MaximumLzwBits || clearCode > MaxStackSize) |
There was a problem hiding this comment.
To save a comparison:
| if(minCodeSize<2||minCodeSize>MaximumLzwBits||clearCode>MaxStackSize) | |
| if((uint)minCodeSize-2>MaximumLzwBits-2||clearCode>MaxStackSize) |
(due the Unsafe.Add below I guess this method is hot enough...)
There was a problem hiding this comment.
thanks for the suggestions @gfoidl, but I do not see any difference when I execute gif decoder benchmark.
I do not want to sacrifice the readability here, if it does not really improve the performance.
| // which may leave a gap in the codes where no colors are assigned. | ||
| // http://www.matthewflickinger.com/lab/whatsinagif/lzw_image_data.asp#lzw_compression | ||
| if (minCodeSize < 2 || clearCode > MaxStackSize) | ||
| if (minCodeSize < 2 || minCodeSize > MaximumLzwBits || clearCode > MaxStackSize) |
There was a problem hiding this comment.
| if(minCodeSize<2||minCodeSize>MaximumLzwBits||clearCode>MaxStackSize) | |
| if((uint)minCodeSize-2>MaximumLzwBits-2||clearCode>MaxStackSize) |
or move this check into a helper, than it's more self-documenting what's going on.
brianpopow
commented
Jun 2, 2024
There are some undisposed buffers when throwing the exception. I have trouble figuring out what buffers are not disposed here. |
JimBobSquarePants
commented
Jun 3, 2024
Shouldn't we be clamping rather than throwing? The provided image can be opened in the browser. |
brianpopow
commented
Jun 3, 2024
The specification says 12 bits is the maximum code length, see spec-gif89a.txt in the gif folder. Appendix F, Compression, section 4: ImageMagick raises an error with this image, so I thought we should, too. I am not sure what will happen when we clamp. I am more in favor of throwing an exception, because > 12 bits is against the spec.
The first 7 images are fine, the issue happens with the 8th image. |
JimBobSquarePants
commented
Jun 5, 2024
@brianpopow The best approach here is to actually adopt our standard of attempting to decode as much as possible. This way we don't throw we simply return and preserve the data for all the previous frames. I don't know why I chose to throw for #2012 but that was a bad choice. |
brianpopow
commented
Jun 6, 2024
Ok, makes sense. I think this then ready for a final review. |
brianpopow
commented
Jun 7, 2024
@JimBobSquarePants should we change that here to also return instead of throwing a exception? |
JimBobSquarePants
commented
Jun 7, 2024
I've already made the change. It was the same conditional check in the LZWDecoder. |
Prerequisites
Description
The image provided in #2743 has some invalid lzwCode length. This PR changes the gif lzw decoder to raise an exception when the lzwCode is larger then 12 Bits.