Repository navigation
Conversation
C's ZopfliCalculateBlockSize[AutoType] reads the store at whatever lstart/lend it is given and treats any btype other than 0 or 1 as dynamic. The port returned 0.0 for lstart > lend, lend > size, or a btype outside 0..2. The range is now read through raw-pointer slices and lstart + ZOPFLI_NUM_LL * 3 wraps as C's size_t does. This reproduces C's out-of-bounds reads 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, crashing is not wanted
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 store the way C does for an out-of-range block-size range or btype
Problem
ZopfliCalculateBlockSize(C, Rust) andZopfliCalculateBlockSizeAutoType(C, Rust) size a block over the command range[lstart, lend). C checks nothing: it reads the store's arrays and cumulative histograms at whateverlstart/lendit is given, and treats anybtypeother than 0 or 1 as dynamic. The port returns 0.0 whenlstart > lendorlend > lz77.size, and returns 0.0 for abtypeoutside 0..2. For example, over a near-empty store:lstart2^64-1,lend864, btype 1lstart0,lend865, btype -1lstart1,lend1, btype 2^31-1lstart=lend= 2^63-1, btype 0 (32-bit only)ZopfliLZ77GetHistogramwraps, so AutoType reads ~4 GiB past the store and segfaultsQuestion
Which behaviour do we want: C's or the Rust port's? Specifically, for an out-of-range
[lstart, lend)or abtypeoutside 0..2, shouldZopfliCalculateBlockSize[AutoType]read the store wherever the range points (like the C source), or return 0.0 (like the Rust port)?This PR implements C's behaviour.
Fix
The patch:
lstart > lend || lend > lz77.sizeguard and thebtyperange match in both functions, passingbtypestraight through (C already treats anything but 0/1 as dynamic);oob_viewthat builds the slices from the raw pointers over the whole[lstart, lend)range, so the existing safe indexing reads past the store exactly as C'slz77->field[i]does;lstart + ZOPFLI_NUM_LL * 3 > lendcomparison intowrapping_add(squeeze.rs,lz77.rs), reproducing C'ssize_twrap instead of panicking on overflow.Replayed before and after (plain = x86-64, arm32 = 32-bit):
4636103972657037312(as raw f64 bits)4636103972657037312, both arches, exact matchRecords 1–3 close: the port leaves its safe
return 0.0and enters C's behavioural class: it segfaults reading out of bounds where C does (1, 2), and computes C's exact value where the range is empty and no read happens (3), on both x86-64 and arm32.Record 4 does not reproduce (residual stated). C's crash here needs
ZopfliCalculateBlockSizeAutoTypeto reach the histogram, which it does only because C's(lz77->size > 1000)test reads the real store size (0). The patched port'soob_viewmakes the store look longer than 1000 commands, so AutoType takes its "skip the fixed-cost calculation" branch and never reaches the crashing read. Making record 4 crash would need unchecked reads kept separate from the length the size test sees, a larger change; it is left as a residual.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:
btypechecks and reads the store out of bounds through raw-pointer slices, which is memory-unsafe; safe Rust returns 0.0 instead. The PR exists only to show the divergence and ask whether these functions should accept an out-of-range range orbtype, not to ship.