LibGfx/JBIG2: Validate more things#26191
Merged
nico merged 8 commits intoSerenityOS:masterfrom Sep 6, 2025
Merged
Conversation
Check that all referred-to segments exist, even for segments that aren't needed for decoding an image. This is a bit stricter than before, but it seems to be fine in practice, and it's simpler. It allows us to have each segment store pointers to all segments they refer to.
No behavior change.
Some files in the power jbig2 test set incorrectly do associate a page with EndOfFile, so don't check for that part for now.
nico
added a commit
to nico/serenity
that referenced
this pull request
Sep 9, 2025
The extra validation in SerenityOS#26191 was added at the end of decode_segment_headers(). But in decode_embedded(), that can be called several times, once for each data chunk. We want to decode segment headers from all chunks first before we do validation, so that segments in later chunks can refer to segments in earlier chunks. Add a new complete_decoding_all_segment_headers() to house the validation code, and call it after all segment chunks have been decoded. Unbreaks rendering 0000372.pdf page 11 and 0000857.pdf pages 1-4.
nico
added a commit
that referenced
this pull request
Sep 10, 2025
The extra validation in #26191 was added at the end of decode_segment_headers(). But in decode_embedded(), that can be called several times, once for each data chunk. We want to decode segment headers from all chunks first before we do validation, so that segments in later chunks can refer to segments in earlier chunks. Add a new complete_decoding_all_segment_headers() to house the validation code, and call it after all segment chunks have been decoded. Unbreaks rendering 0000372.pdf page 11 and 0000857.pdf pages 1-4.
nico
added a commit
to nico/serenity
that referenced
this pull request
Dec 27, 2025
jbig2enc, which is used by Google Books, sometimes writes files that are technically invalid. Allow that in permissive mode. To be able to check the referred-to segment's types, switch the loop from iterating over header.referred_to_segment_numbers to iterating over referred_to_segments directly (possible after the third commit of SerenityOS#26191, 72cd151). Fixes SerenityOS#26405.
nico
added a commit
that referenced
this pull request
Dec 27, 2025
jbig2enc, which is used by Google Books, sometimes writes files that are technically invalid. Allow that in permissive mode. To be able to check the referred-to segment's types, switch the loop from iterating over header.referred_to_segment_numbers to iterating over referred_to_segments directly (possible after the third commit of #26191, 72cd151). Fixes #26405.
Hendiadyoin1
pushed a commit
to Hendiadyoin1/serenity
that referenced
this pull request
Jan 2, 2026
jbig2enc, which is used by Google Books, sometimes writes files that are technically invalid. Allow that in permissive mode. To be able to check the referred-to segment's types, switch the loop from iterating over header.referred_to_segment_numbers to iterating over referred_to_segments directly (possible after the third commit of SerenityOS#26191, 72cd151). Fixes SerenityOS#26405.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…and a few minor cleanups.
For writing JBIG2Writer, it's useful if the loader is pretty strict, to catch mistakes. (But not too strict to break real-world files, of course. If this does break any files in practice, we need to weaken it. I'm not aware of any files that this fires on though.)