Repository navigation
Description
The GIF decoder in VellumPdf.Kernel was written without the specification in hand and cites none.
Three defects follow from that, two of them silent. Reported by the VellumPdfShowcase project, which
withheld a GIF sample because it could not be rendered.
The GIF89a specification, CompuServe Incorporated, 31 July 1990, is freely published by W3C and is
now held in the standards library.
1. The code width grows one code too late
Appendix F, clause 4: "Whenever the LZW code value would exceed the current code length, the code
length is increased by one." A width of n expresses 0 to 2^n-1, so the width must grow as the next
code to assign reaches 2^n. The decoder grew at 2^n+1, so it kept reading at the narrower width
after the encoder had widened, desynchronised, and threw "Invalid GIF LZW code".
Only images whose dictionary never passed the boundary decoded. That is why a flat colour loads and
ordinary content does not.
2. Interlaced images are never deinterlaced
Appendix E defines the four-pass row order. Nothing in the decoder read the interlace flag, so those
images decoded with rows in storage order. No error, just a scrambled picture. A flat colour is
unaffected, which is part of why it went unnoticed.
3. A code not yet in the table puts its repeated byte at the wrong end
That code stands for the previous string followed by that string's own first byte. The decoder
emitted the byte before the string. One pixel per occurrence: on a 48x48 image of three-pixel
vertical bars, 120 of 2304 pixels, each sitting on a bar boundary.
Why the tests did not catch any of it
The fixture helper in ImageFormatTests emits literal codes at a fixed width and never widens them,
which is not a conformant stream. The decoder's matching off-by-one meant fixture and decoder agreed
with each other and with nothing else. Meanwhile HardeningV151Tests contains a real encoder that
has the rule right, and TiffLzwDecoder's own header states GIF's rule correctly while contrasting
TIFF's early change against it. The knowledge was in the tree; nothing compared it with the decoder.
Process gap this exposes
eng/clean-room-check.ps1 enforces the clean-room policy in one direction only: it greps for banned
implementation names. Nothing checks that an implementation has a published specification behind it.
The acquisition ledger audits the documents the code cites, so a file citing nothing cannot appear
as a gap, which is why its "still missing and free: nothing" was false. BmpImageLoader,
TiffImageLoader and TiffLzwDecoder cite nothing normative today and are in the same position.
Activity
fix: refuse the malformed GIF input the class contract promised to re…
fix(kernel): derive the GIF decoder from the specification, and add a…
fix: refuse the malformed GIF input the class contract promised to re…
[-]Kernel: the GIF decoder refuses most real files and silently corrupts others[/-][+]Kernel: the GIF decoder refuses a third of real files and silently corrupts others[/+]Tim81 commentedon Sep 12, 2026
Retitled: most was never measured. On a 200-file sweep from an independent encoder (Pillow 12.3.0), spanning five content kinds, five sizes and four palette sizes, the decoder refused 71, returned wrong pixels for 23, and decoded 106 correctly. So a third refused, not most.
The related claim that a flat colour worked and anything with detail did not is also false: all 40 flat files decoded, but so did 66 of the 160 with detail. What decided it was whether that file's dictionary happened to cross a code-width boundary.
Follow-ups from the review round, both deferred to this milestone rather than fixed in #492: #498 (the logical screen is ignored, so an offset frame decodes at the wrong size) and #499 (the version byte is a constant where section 17.d.ii asks for the earliest that fits).
fix: make the GIF code-stream tests able to fail
fix(kernel): derive the GIF decoder from the specification, and add a…
release: v2.3.3 - changelog, READMEs, VersionPrefix, and the shipped …