Fix GIF decode failure on streams that fill the LZW code table - #141
Merged
Conversation
GIF89a caps LZW codes at 12 bits, so the code table stops at 4096 entries. compress_lzw has always honoured that -- it emits a Clear Code rather than adding entry 4096 -- but decompress_lzw widened straight off its own counter with no ceiling. The decoder trails the encoder by exactly one table entry, so it reaches counter == 4096 while reading the last code the encoder wrote before that Clear. get_bits_number_for(4096) is 13, so the next read took 13 bits out of a stream still written in 12. Every code after that was shifted: a Clear Code was mis-detected, the table reset, and the reader then asked for a code past the end of it. In the editor that faulted with "Nonexistent function add in base Nil" at lzw.gd:193. In a release build decompress_lzw simply abandoned the frame, so load_gif returned a SpriteFrames whose get_frame_count() looked healthy while the textures were empty -- failing much later and much further away from the cause. Twitch first-party emotes are small enough never to approach the boundary, which is why this went unnoticed; the reported trigger was a 318-frame 7TV emote that genuinely uses the full 12-bit space. Adds a generated GIF fixture that reproduces it end to end. Without this fix, test_round_trips_a_stream_that_fills_the_code_table and test_decodes_a_gif_that_fills_the_lzw_code_table both fail. Fixes #132 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes: #132