Repository navigation
Conversation
C appends a byte only when the bit pointer is 0, so an empty output with a non-zero bit pointer writes out[-1]. The port also appended a byte when the output was empty. This reproduces C's out-of-bounds write and is not meant to be merged.
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.
Write the last output byte the way C's AddBit does when the output is empty
Problem
AddBit(C, Rustadd_bit) appends one bit to the output. C appends a fresh byte only when the bit pointer is 0, then ORs the bit into(*out)[*outsize - 1]. When the output is empty and the caller passes a non-zero bit pointer, there is no last byte, so C writes toout[-1], before the buffer. The port also appends a byte when the output is empty, so it never writes before the buffer. For example,ZopfliDeflatewith an empty output andbp = 146(sobp & 7 = 2):out[-1], before the buffer, and segfaults (SIGSEGV)Question
Which behaviour do we want: C's or the Rust port's? Specifically, when the output is empty and the bit pointer is non-zero, should
AddBitwrite to the byte before the buffer (like the C source), or start a new output byte (like the Rust port)?This PR implements C's behaviour.
Fix
add_bitappends a byte only when*bp == 0(dropping the|| out.is_empty()), then writes the last byte through an unchecked pointer, so an empty output writesout[-1]as C does:Replayed on the empty-output,
bp = 146record, before and after:Both sides now write before the buffer 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:
out[-1]) through a raw pointer, which is memory-unsafe; safe Rust starts a new byte instead. The PR exists only to show the divergence and ask whetherAddBitshould be called with a non-zero bit pointer and an empty output, not to ship.AddBitpath: forbp ≥ 8, C shifts the BFINAL bit out of the byte, and the port masks the bit pointer first and keeps it. Write BFINAL with the caller's bit pointer, as C's AddBit does #219 covers it.