Skip to content

legacy/v0.5: fix streaming regression from the wrap history fix - #4816

Merged
Cyan4973 merged 2 commits into
meta:devfrom
ManoharPaturi:fix-legacy-v05-stream-wrap
Nov 17, 2026
Merged

Cyan4973 merged 2 commits into
meta:devfrom
ManoharPaturi:fix-legacy-v05-stream-wrap

Conversation

@ManoharPaturi

Copy link
Copy Markdown
Contributor

The fix for #4553 (commit 3a3c506) preserved the last windowSize bytes with a memmove when the streaming output buffer wraps. Two configurations were missed.

When outEnd equals windowSize at the wrap, the preserved window fills the whole buffer, leaving no room for the next block, so the streaming decoder rejects valid frames with dstSize_tooSmall.

When outEnd exceeds windowSize, the memmove relocates history that the decoder still addresses through its vBase and dictEnd pointer arithmetic, which assumes history bytes stay in place, so external dictionary match sources read from the wrong location.

The first case is now the common one: any v0.5 frame whose streaming decode crosses a full window boundary fails, because the wrap first fires exactly when the buffer is full. A frame produced by the v0.5.1 command line itself, decodable by the one shot API and by every streaming decoder before that commit, now returns:

ZSTD_decompressStream: Destination buffer is too small

The v0.6 and v0.7 decoders already solve the underlying problem by sizing the output buffer at windowSize plus one block and keeping the wrap as a plain reset. The buffer then only wraps once outStart has moved a full window past its start, at which point every offset within the window resolves before the reset point, so the reset drops nothing a later block can reference. This change brings the v0.5 decoder to that design: the wrap no longer happens mid window, which keeps #4553 fixed, and the frames the memmove broke decode again.

Test evidence, all through the public API on unfixed dev versus this change:

issue #4553 reproducer: passes before, passes after
frame from v0.5.1, 1.2 MB decoded across several windows: dstSize_tooSmall before, matches the one shot decoder after
crafted frame, block filling the window exactly: dstSize_tooSmall before, matches after
crafted frame in the #4553 shape: passes before, passes after

The two crafted frames are added to tests/legacy.c, gated on ZSTD_LEGACY_SUPPORT at or below 5, and the existing legacy tests pass at every level from 1 to 7.

Copilot AI balanced review requested due to automatic review settings October 4, 2026 02:53
@meta-cla meta-cla Bot added the CLA Signed label Oct 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@ManoharPaturi

Copy link
Copy Markdown
Contributor Author

@Cyan4973 these legacy decoder fixes (#4816 among the v0.2 through v0.7 batch) have been green on all checks for a few weeks now with no reviewer assigned yet. Each one is a narrow correctness fix in one legacy format with a round-trip regression test in tests/legacy.c. Could you or another maintainer take a look when the queue allows?

@Cyan4973 Cyan4973 self-assigned this Nov 16, 2026
ManoharPaturi and others added 2 commits November 16, 2026 09:58
The fix for meta#4553 preserved the last windowSize bytes with a memmove when
the streaming output buffer wraps. Two configurations were missed:

  when outEnd equals windowSize at the wrap, the preserved window fills
  the whole buffer, leaving no room for the next block, so the streaming
  decoder rejects valid frames with dstSize_tooSmall;

  when outEnd exceeds windowSize, the memmove relocates history that the
  decoder's vBase and dictEnd pointer arithmetic still addresses at its
  old location, so external dictionary match sources read the wrong bytes.

Frames crossing a full window boundary now fail to stream at all: a frame
produced by v0.5.1 itself, decodable by the one-shot API and by every
streaming decoder before that fix, returns dstSize_tooSmall.

Size the output buffer at windowSize plus one block, as the v0.6 and v0.7
decoders already do, and return the wrap to a plain reset: the buffer now
only wraps once outStart has moved a full window past its start, at which
point every offset within the window resolves before the reset point, so
the reset drops nothing the next block can reference. This keeps the
the frames the memmove broke.

The new test frames cover both shapes: a block that fills the window
exactly, and the sub-window shape from meta#4553.
The two crafted frames were 2 KB and 1 KB of hex strings. The second one
didn't reproduce meta#4553: its sequence decodes as litLength 0, offset 1
(repcode) and matchLength 4, instead of a match that reaches back into
the raw block, so it also passes with the decoder from before the meta#4553
fix.

Build the frames at runtime instead, like the v0.1/v0.2 zero offset
test: a raw block, then a compressed block whose match copies 4 bytes
from offset 1000, after 500 literals. Two sizes of raw block cover the
two bugs:
- 600 bytes: the second block overwrites history that its match still
  reads (meta#4553). Fails with the decoder from before 3a3c506.
- 2048 bytes: the raw block fills the window, the regression from
  3a3c506. Fails with the decoder currently on dev.
@Cyan4973
Cyan4973 force-pushed the fix-legacy-v05-stream-wrap branch from e705add to 37619a1 Compare November 16, 2026 22:41

@Cyan4973 Cyan4973 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, the fix is correct.
The regression could be reproduced with frames coming from the v0.5.1 CLI: then, on dev, with v0.5 support enabled, streaming decompression fails with Destination buffer is too small on files larger than the window. This PR decodes all of them.

Two changes were pushed to your branch:

  • it is rebased on current dev, which had new tests in tests/legacy.c. If you keep working on it, reset your local branch first.
  • the test is replaced by a smaller one, which builds its frames at runtime instead of storing them as hex strings.
    • FRAME_V05_WRAP didn't reproduce #4553: its sequence decodes as litLength 0, offset 1 (repcode) and matchLength 4, so it also passes with the decoder from before the #4553 fix.
    • The new test builds a raw block, then a block whose match reaches back into it, with two raw block sizes: 600 bytes fails with the decoder from before the #4553 fix, and 2048 bytes fails with the current dev decoder.

@Cyan4973
Cyan4973 merged commit d7e982e into meta:dev Nov 17, 2026
109 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants