Skip to content

Fix GIF decode failure on streams that fill the LZW code table - #141

Merged
kanimaru merged 1 commit into
masterfrom
fix/gif-lzw-12-bit-code-cap
Sep 8, 2026
Merged

Fix GIF decode failure on streams that fill the LZW code table#141
kanimaru merged 1 commit into
masterfrom
fix/gif-lzw-12-bit-code-cap

Conversation

@kanimaru

@kanimaru kanimaru commented Sep 8, 2026

Copy link
Copy Markdown
Owner

closes: #132

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>
@kanimaru kanimaru self-assigned this Sep 8, 2026
@kanimaru kanimaru added the bug Something isn't working label Sep 8, 2026
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown

GUT Results

147 tests  +4   147 ✅ +4   3s ⏱️ -1s
 13 suites +2     0 💤 ±0 
  1 files   ±0     0 ❌ ±0 

Results for commit dc7f2e0. ± Comparison against base commit 86083ab.

@kanimaru
kanimaru merged commit 87b4952 into master Sep 8, 2026
4 checks passed
@kanimaru
kanimaru deleted the fix/gif-lzw-12-bit-code-cap branch September 8, 2026 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vendored gif-lzw decompress_lzw has no 12-bit code cap — faults on large GIFs; upstream rewrote it in Nov 2025

1 participant