Visitar URL original
legacy/v0.5: fix streaming regression from the wrap history fix by ManoharPaturi · Pull Request #4816 · facebook/zstd · GitHub
Skip to content

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

Open
ManoharPaturi wants to merge 1 commit into
facebook:devfrom
ManoharPaturi:fix-legacy-v05-stream-wrap
Open

ManoharPaturi wants to merge 1 commit into
facebook:devfrom
ManoharPaturi:fix-legacy-v05-stream-wrap

Conversation

@ManoharPaturi

Copy link
Copy Markdown

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.

The fix for facebook#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
facebook#4553 case fixed (the wrap no longer happens mid window) and restores
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 facebook#4553.
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.

This branch has not been deployed

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

2 participants