Skip to content

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

Closed

Description

@vpacher

Summary

libnsgif's LZW code reader (libobs/graphics/libnsgif/lzw.c.h:175) asks for one byte too many on its slow path when a code ends exactly on a byte boundary (current_bit + code_size is a multiple of 8). If that code is the last one in the image data, there is no next sub-block, so the code is lost. The frame decode still reports success, and the last pixels stay empty.

Upstream fixed it on 2026-10-25 in netsurf-browser/libnsgif@2255b2f74 ("lzw: Fix off-by-one on code reader slow path", one line), with a regression test in 9abf1190d (test/data/lzweod.gif and .ppm). Our copy is libnsgif 1.0.0 (commit e0027ee4a, from upstream OBS) without it. The Rust image file (#1907) calls the same C, so the hybrid build has it too.

Not a vulnerability: every read stays inside the current sub-block (ctx->sb_bit < ctx->sb_bit_count is checked before each read). The effect is a wrong image.

Evidence

A small harness (unity build of our lzw.c.h + gif.c.h, NSGIF_BITMAP_FMT_R8G8B8A8) decodes upstream's lzweod.gif (3×1, one frame):

current copy:      scan: Success, frame 0 decode: Success, RGB 255 0 0  0 255 0  0 0 0
with 2255b2f74:    scan: Success, frame 0 decode: Success, RGB 255 0 0  0 255 0  0 0 255
lzweod.ppm:                                                    255 0 0  0 255 0  0 0 255

Fix

  1. RED: a cmocka case (in test_image_file, or a small test_nsgif_lzw if that fits the one-binary-per-category rule better) that decodes lzweod.gif and compares frame 0 with the .ppm values. Fails on the current copy: the last pixel is 0 0 0.
  2. GREEN: apply upstream's one-line change to lzw.c.h. Add the two test files with their origin (libnsgif, MIT, as LICENSE.libnsgif).

Size S. This also gives the Rust port of the LZW decoder (follow-up issue) the fixed C as its oracle.

Activity

  1. vpacher commented on Oct 26, 2026

    @vpacher
    MemberAuthor

    Claiming: #2465

    1. RED: a cmocka case that decodes upstream's lzweod.gif and compares frame 0 with lzweod.ppm (fails now: the last pixel is 0 0 0).
    2. GREEN: upstream's one-line change in lzw.c.h, plus the two test files with their origin.

    PR today.

  2. added
    ready
    on Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ready

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions