Repository navigation
Conversation
…t > inend C never checks instart <= inend: the stored-block size inend - pos wraps and C reads in[pos + i] past the input. The port returned early. The guard is dropped and add_non_compressed_block wraps and reads through a raw pointer as C does. This reproduces C's out-of-bounds read and is not meant to be merged.
1c3t3a
reviewed
Nov 20, 2026
1c3t3a
left a comment
Collaborator
There was a problem hiding this comment.
We want Rust behavior as crashing is a bug.
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.
Read the input the way C does when ZopfliDeflatePart gets instart > inend
Problem
ZopfliDeflatePart(C, Rust) compresses the input range[instart, inend). C never checks thatinstart <= inend: for a stored block it computes the block size asinend - pos, which wraps wheninstart > inend, and then reads the input atin[pos + i]well past its end. The port returns early wheninstart > inendand writes nothing. For example,ZopfliDeflatePartwithbtype = 0,instart = 2^63 - 1,inend = 1, over a 1-byte input:instart2^63-1,inend1inpast its end and segfaults (SIGSEGV)Question
Which behaviour do we want: C's or the Rust port's? Specifically, when
instart > inend, shouldZopfliDeflatePartcompute the wrapped range and read past the input (like the C source), or return without writing anything (like the Rust port)?This PR implements C's behaviour.
Fix
The patch drops the
instart > inendguard inRust_ZopfliDeflatePartand makesadd_non_compressed_blockmirror C's wrapping arithmetic and unchecked input read:Rust_ZopfliDeflatePart: removeif instart > inend { return; }.add_non_compressed_block:pos + blocksizeandinend - posbecomewrapping_add/wrapping_sub, andin_data[pos + i]becomes a raw-pointer read*in_data.as_ptr().wrapping_add(pos + i), so a wrapped block size reads past the input as C'sin[pos + i]does.Replayed on the
instart = 2^63-1, inend = 1record, before and after:Both sides now read past the input and segfault, and the divergence closes.
Downstream check. The normal path is untouched:
ZopfliGzipCompresson the repository'sREADMEgives the same 831-byte stream from the port before and after this patch, andgzip.decompressrestores the file. C's stream is also 831 bytes but differs in a few bytes, for a separate reason (#213).Notes for review:
ZopfliDeflatePartshould acceptinstart > inend, not to ship.