Fix safe_dist under-reporting for multi-block inputs - #26
Open
meeq wants to merge 1 commit into
Open
Conversation
apultra_write_block tracked nCurSafeDist as (i - nStartOffset) - nOutOffset: a per-block local delta between input consumption and output emission. Because nOutOffset resets to 0 at the top of each apultra_write_block call (pOutData is already advanced by the caller), and nStartOffset is the current block's offset in the input window, this measure ignores the accumulated decoder-output-vs-decoder-input lead from previous blocks. For single-block inputs (< 1 MiB) the local delta equals the global delta and the bug is invisible. For multi-block inputs the encoder writes a safe_dist that is too small, and any consumer doing in-place decompression (allocating bufsize from safe_dist) sees its output pointer overtake the unread compressed input and corrupt subsequent reads. Fix: thread nPriorDelta — the cumulative (decoder-output - decoder-input) at block start — through apultra_compressor_shrink_block → apultra_optimize_and_write_block → apultra_write_block, and add it to nCurSafeDist. The top-level loop in apultra_compress already has the cumulative nOriginalSize and nCompressedSize, so nPriorDelta is (nOriginalSize - nDictionarySize) - nCompressedSize. Dictionary bytes aren't emitted by the decoder, so they're excluded from the input side. The compressed bitstream content is unchanged by this fix; only the reported safe_dist (and downstream in-place margin) changes.
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.
apultra_write_blocktrackednCurSafeDistas(i - nStartOffset) - nOutOffset: a per-block local delta between input consumption and output emission. BecausenOutOffsetresets to 0 at the top of eachapultra_write_blockcall (pOutDatais already advanced by the caller), andnStartOffsetis the current block's offset in the input window, this measure ignores the accumulated decoder-output-vs-decoder-input lead from previous blocks.For single-block inputs (< 1 MiB) the local delta equals the global delta and the bug is invisible. For multi-block inputs the encoder writes a
safe_distthat is too small, and any consumer doing in-place decompression (allocatingbufsizefromsafe_dist) sees its output pointer overtake the unread compressed input and corrupt subsequent reads.Fix: thread
nPriorDelta(the cumulativedecoder-output - decoder-inputat block start) throughapultra_compressor_shrink_block→apultra_optimize_and_write_block→apultra_write_block, and add it tonCurSafeDist. The top-level loop inapultra_compressalready has the cumulativenOriginalSizeandnCompressedSize, sonPriorDeltais(nOriginalSize - nDictionarySize) - nCompressedSize. Dictionary bytes aren't emitted by the decoder, so they're excluded from the input side.The compressed bitstream content is unchanged by this fix; only the reported
safe_dist(and downstream in-place margin) changes.