Skip to content

Read the input the way C does when ZopfliDeflatePart gets instart > inend - #216

Closed
kunpai wants to merge 1 commit into
google:masterfrom
kunpai:deflate-part-instart-past-inend
Closed

kunpai wants to merge 1 commit into
google:masterfrom
kunpai:deflate-part-instart-past-inend

Conversation

@kunpai

@kunpai kunpai commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

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 that instart <= inend: for a stored block it computes the block size as inend - pos, which wraps when instart > inend, and then reads the input at in[pos + i] well past its end. The port returns early when instart > inend and writes nothing. For example, ZopfliDeflatePart with btype = 0, instart = 2^63 - 1, inend = 1, over a 1-byte input:

C Rust
instart 2^63-1, inend 1 the stored-block size wraps, C reads in past its end and segfaults (SIGSEGV) returns immediately, nothing written

Question

Which behaviour do we want: C's or the Rust port's? Specifically, when instart > inend, should ZopfliDeflatePart compute 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 > inend guard in Rust_ZopfliDeflatePart and makes add_non_compressed_block mirror C's wrapping arithmetic and unchecked input read:

  • Rust_ZopfliDeflatePart: remove if instart > inend { return; }.
  • add_non_compressed_block: pos + blocksize and inend - pos become wrapping_add/wrapping_sub, and in_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's in[pos + i] does.

Replayed on the instart = 2^63-1, inend = 1 record, before and after:

C Rust main Rust patched
trap SIGSEGV (11) none (returns, writes nothing) SIGSEGV (11)

Both sides now read past the input and segfault, and the divergence closes.

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 bounds check and reads the input buffer out of bounds with a raw pointer, which is memory-unsafe; safe Rust returns early or bounds-checks instead. The PR exists only to show the divergence and ask whether ZopfliDeflatePart should accept instart > inend, not to ship.

…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 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 as crashing is a bug.

@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