Skip to content

picture: refuse a JPEG whose decode stops before every pixel is written - #2759

Merged
ashhart merged 2 commits into
mainfrom
lead/jpeg-unwritten
Nov 10, 2026
Merged

ashhart merged 2 commits into
mainfrom
lead/jpeg-unwritten

Conversation

@ashhart

@ashhart ashhart commented Nov 10, 2026

Copy link
Copy Markdown
Owner

What this changes

A damaged JPEG could decode "successfully" with rows libjpeg never wrote. TurboJPEG 3 reports a fatal error as a warning when a warning came earlier in the same call, and the decoder took any warning as success, keeping an output buffer that hadn't been cleared. Such a file is now refused with the decoder's usual error.Invalid, like any other undecodable image.

  • The output buffer is cleared before decoding.
  • When the library answers "warning", the same bytes are decoded again into a buffer with a different fill. A byte the library wrote is the same in both; a byte it skipped isn't. If they differ, the picture is refused.
  • A clean decode keeps exactly its bytes, and only damaged images that draw a warning pay for the second decode. A warning after the last row is written (a stray byte, a wrong restart marker, an empty trailing scan) still decodes as before.

Checks

  • zig build test-picture-host with the pinned libjpeg-turbo: new tests that four small damaged files are refused (they fail without the change) and that four warning-only files still decode to their expected pixels. Before: 29 pass, 1 skip, 1 fail; after: 30 pass, 1 skip (WebP, no library on the test machine).
  • Every golden and edge JPEG in the repository decodes to the same bytes as before.
  • Over about 20,000 damaged copies of those JPEGs, the only outcomes that changed are the ones whose output depended on what was in memory before the decode; each of those is now refused.
  • test-lfm2-load-host, test-codec-diagnostics-host: pass.

Thanks to a community tester who reported this privately and sent a fix along the same lines.

  • Synthena (Ash's Agent)

ashhart added a commit that referenced this pull request Nov 10, 2026
@ashhart
ashhart force-pushed the lead/jpeg-unwritten branch 2 times, most recently from 1519ab7 to 90b0841 Compare November 10, 2026 15:32
TurboJPEG reports a fatal error as a warning when a warning came first,
so a decode could return success over rows libjpeg never wrote. Decode
over zeros and, on a warned decode, again over 0xFF: refuse unless both
agree. Clean decodes keep their bytes.
@ashhart
ashhart force-pushed the lead/jpeg-unwritten branch from 3a7b491 to 74447b8 Compare November 10, 2026 20:24
@ashhart
ashhart merged commit d829cca into main Nov 10, 2026
1 of 8 checks passed
@ashhart
ashhart deleted the lead/jpeg-unwritten branch November 10, 2026 20:24
ashhart added a commit that referenced this pull request Nov 10, 2026
ashhart added a commit that referenced this pull request Nov 10, 2026
ashhart added a commit to laudney/TensorFold that referenced this pull request Nov 10, 2026
ashhart added a commit that referenced this pull request Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant