Skip to content

Fix safe_dist under-reporting for multi-block inputs - #26

Open
meeq wants to merge 1 commit into
emmanuel-marty:masterfrom
meeq:fix-safe-dist-multiblock
Open

Fix safe_dist under-reporting for multi-block inputs#26
meeq wants to merge 1 commit into
emmanuel-marty:masterfrom
meeq:fix-safe-dist-multiblock

Conversation

@meeq

@meeq meeq commented Jun 5, 2026

Copy link
Copy Markdown

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_blockapultra_optimize_and_write_blockapultra_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.

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.
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.

1 participant