Skip to content

Read the store the way C does for an out-of-range block-size range or btype - #215

Closed
kunpai wants to merge 1 commit into
google:masterfrom
kunpai:blocksize-out-of-range
Closed

kunpai wants to merge 1 commit into
google:masterfrom
kunpai:blocksize-out-of-range

Conversation

@kunpai

@kunpai kunpai commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Read the store the way C does for an out-of-range block-size range or btype

Problem

ZopfliCalculateBlockSize (C, Rust) and ZopfliCalculateBlockSizeAutoType (C, Rust) size a block over the command range [lstart, lend). C checks nothing: it reads the store's arrays and cumulative histograms at whatever lstart/lend it is given, and treats any btype other than 0 or 1 as dynamic. The port returns 0.0 when lstart > lend or lend > lz77.size, and returns 0.0 for a btype outside 0..2. For example, over a near-empty store:

# C Rust
1 lstart 2^64-1, lend 864, btype 1 reads the cumulative counts past the store and segfaults (SIGSEGV) returns 0.0
2 lstart 0, lend 865, btype -1 takes the dynamic path, reads past the store and segfaults (SIGSEGV) returns 0.0
3 lstart 1, lend 1, btype 2^31-1 takes the dynamic path and returns a block size returns 0.0
4 lstart = lend = 2^63-1, btype 0 (32-bit only) on a 32-bit target the range check in ZopfliLZ77GetHistogram wraps, so AutoType reads ~4 GiB past the store and segfaults returns 0.0

Question

Which behaviour do we want: C's or the Rust port's? Specifically, for an out-of-range [lstart, lend) or a btype outside 0..2, should ZopfliCalculateBlockSize[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:

  • drops the lstart > lend || lend > lz77.size guard and the btype range match in both functions, passing btype straight through (C already treats anything but 0/1 as dynamic);
  • borrows the store through a new oob_view that 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's lz77->field[i] does;
  • turns the lstart + ZOPFLI_NUM_LL * 3 > lend comparison into wrapping_add (squeeze.rs, lz77.rs), reproducing C's size_t wrap instead of panicking on overflow.

Replayed before and after (plain = x86-64, arm32 = 32-bit):

# C Rust main Rust patched
1 SIGSEGV (11) 0.0 SIGSEGV (11), both arches
2 SIGSEGV (11) 0.0 SIGSEGV (11), both arches
3 block size 4636103972657037312 (as raw f64 bits) 0.0 4636103972657037312, both arches, exact match
4 x86: 0.0 (no divergence); arm32: SIGSEGV (11) 0.0 x86: 0.0; arm32: still 0.0 (not reproduced)

Records 1–3 close: the port leaves its safe return 0.0 and 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 ZopfliCalculateBlockSizeAutoType to reach the histogram, which it does only because C's (lz77->size > 1000) test reads the real store size (0). The patched port's oob_view makes 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: ZopfliGzipCompress on the repository's README gives the same 831-byte stream from the port before and after this patch, and gzip.decompress restores the file. C's stream is also 831 bytes but differs in a few bytes, for a separate reason (#213).

Notes for review:

  • This patch reproduces C's undefined behaviour and must not be merged. It removes the port's range and btype checks 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 or btype, not to ship.

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 1c3t3a left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want Rust behavior, crashing is not wanted

@1c3t3a 1c3t3a closed this Nov 20, 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.

2 participants