Skip to content

libnsgif: Fix the LZW code reader's off-by-one on the slow path (upstream 2255b2f74) (#2465) - #2468

Merged
zackees merged 4 commits into
masterfrom
fix/2465-lzw-eod
Oct 26, 2026
Merged

zackees merged 4 commits into
masterfrom
fix/2465-lzw-eod

Conversation

@vpacher

@vpacher vpacher commented Oct 26, 2026

Copy link
Copy Markdown
Member

Fixes #2465

What and why

The vendored libnsgif (1.0.0) lost the last LZW code of an image when that code ends exactly on a byte boundary at the end of the data: the code reader's slow path asked for one byte more than the code needs, and there is no next sub-block. The decode reported success, and the last pixels stayed empty. This backports upstream's one-line fix, netsurf-browser/libnsgif@2255b2f74 (2026-10-25), with upstream's regression image.

Not a memory error: every read stays inside the current sub-block (ctx->sb_bit < ctx->sb_bit_count is checked before each read).

  • libobs/graphics/libnsgif/lzw.c.h:175: (current_bit + code_size - 1) >> 3, as upstream.
  • New test_nsgif_decode (in build/test-graph.json): decodes GIFs with libnsgif directly and compares frame 0 with a reference PPM. A one-frame GIF goes to FFmpeg in gs_image_file, so test_image_file does not reach libnsgif for it. The LZW port (rust: Port the libnsgif LZW decoder (graphics/libnsgif/lzw.c.h) to obs-graphics (libnsgif slice 1) #2466) adds its characterization cases here.
  • test/cmocka/data/libnsgif-lzweod.{gif,ppm}: libnsgif's test/data/lzweod.{gif,ppm} from 9abf1190d (MIT, libobs/graphics/libnsgif/LICENSE.libnsgif).

RED → GREEN

  • RED 9c183035f: lzw_code_on_byte_boundary_at_end_of_data fails with "pixel 2 channel 2: 0, expected 255" (the last pixel black instead of blue; the decode itself reports success).
  • GREEN f0bddbe31: passes. test_nsgif_decode, test_image_file, test_media_gif_last_frame and the other GIF tests pass (5/5, Linux x86_64).
  • Lint lane (ci/local_gate.py --lane lint) passes, including test_cmocka_windows_diagnostics (the test prints with fprintf(stderr, …) and calls fail(), not fail_msg()).
  • Real app: not applicable (the decoder output is the test).

Checklist

  • Base is master
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits
  • User-visible change: its line in docs/features.md is added (Fixed upstream bugs, Sources and filters)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes: not applicable
  • Intentional differences from C: not applicable

vpacher and others added 3 commits October 26, 2026 10:01
…ary)

A new cmocka test decodes GIFs with the vendored libnsgif directly (a
one-frame GIF goes to FFmpeg in gs_image_file, so test_image_file does
not reach libnsgif for it) and compares frame 0 with a reference PPM.

The first case is libnsgif's own regression test for upstream
2255b2f74: test/data/lzweod.gif and .ppm from netsurf-browser/libnsgif
9abf1190d (MIT, see libobs/graphics/libnsgif/LICENSE.libnsgif), added as
test/cmocka/data/libnsgif-lzweod.{gif,ppm}. The last LZW code ends on a
byte boundary at the end of the data.

RED: the decode reports success, but the last pixel is 0 0 0, expected
0 0 255.

Refs #2465

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ream 2255b2f74)

Backport of netsurf-browser/libnsgif 2255b2f74 ("lzw: Fix off-by-one on
code reader slow path", 2026-10-25), one line. When a code ends exactly
on a byte boundary, the slow path asked for one more byte than the code
needs. For the last code of the image data there is no next sub-block,
so it was lost: the decode reported success and the last pixels stayed
empty. Reads were always inside the sub-block, so this is a wrong image,
not a memory error.

GREEN: test_nsgif_decode decodes libnsgif-lzweod.gif as its reference.

Fixes #2465

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vpacher
vpacher requested a review from zackees as a code owner October 26, 2026 09:04
@zackees

zackees commented Oct 26, 2026

Copy link
Copy Markdown
Member

Coordinator review: a one-line backport of libnsgif 2255b2f74 with upstream's regression image, and every read stays bounds-checked inside the sub-block. The new decode test is in test-graph.json, no existing test changes, and the ledger line is in. Queuing.

@zackees
zackees enabled auto-merge October 26, 2026 09:12
@zackees
zackees added this pull request to the merge queue Oct 26, 2026
Merged via the queue into master with commit cf6d92b Oct 26, 2026
36 checks passed
@zackees
zackees deleted the fix/2465-lzw-eod branch October 26, 2026 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: libnsgif: the last LZW code is lost when it ends on a byte boundary at the end of the data (upstream fix 2255b2f74)

2 participants