Repository navigation
legacy/v0.5: fix streaming regression from the wrap history fix - #4816
Merged
Merged
Conversation
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? |
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
force-pushed
the
fix-legacy-v05-stream-wrap
branch
from
November 16, 2026 22:41
e705add to
37619a1
Compare
Cyan4973
reviewed
Nov 17, 2026
Cyan4973
left a comment
Contributor
There was a problem hiding this comment.
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 intests/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_WRAPdidn'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
devdecoder.
Cyan4973
approved these changes
Nov 17, 2026
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.
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:
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.