Skip to content

Decode every frame of concatenated zstd input - #8724

Merged
littledivy merged 4 commits into
mainfrom
littledivy/2026-11-20-node-zstd-multiframe
Nov 21, 2026
Merged

littledivy merged 4 commits into
mainfrom
littledivy/2026-11-20-node-zstd-multiframe

Conversation

@littledivy

@littledivy littledivy commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

The zstd decoder called ZSTD_decompressStream once per write. When a frame ended with input left and output space free, the sync loop stopped, so zstdDecompressSync and zstdDecompress returned only the first frame of concatenated input. A leading skippable frame, which pzstd writes before every frame, also ends with a 0 result, so the real frame after it was dropped and the output was empty. In a stream, the unread bytes after the first frame were lost, and the next write failed with "Unknown frame descriptor" when the second frame started partway into the previous write. With maxOutputLength equal to the first frame's size, the first frame came back with no error.

In this patch the decoder works like Node's: it keeps decoding while a frame has just ended and input is left. The first 4 bytes after a frame are checked against the zstd and skippable frame magic numbers, across writes, and input that is neither is ignored as trailing junk, as Node does by default. Ignored input doesn't count against maxOutputLength, so a frame that exactly fills it still decodes. A frame that was cut short still throws "unexpected end of file", and an empty input still decodes to an empty buffer (Node throws there). The new tests in zlib-zstd-nodejs-test.js cover concatenated, skippable and split frames, trailing junk, a truncated second frame and maxOutputLength across frames, through the sync, callback and stream APIs. The ones that check new behaviour fail without the fix, and the same tests pass in Node 26.12 apart from empty input.

This builds on #8598, whose two commits come first here until it merges.

zstdDecompressSync and zstdDecompress threw "unexpected end of file" for
a valid frame whose decompressed size exactly filled the sync output
buffer: 40960 bytes, and 81920 and 163840 as the buffer doubles (#7923).
A full output buffer makes the sync loop call ZSTD_decompressStream once
more after all input is consumed. For that call zstd returns the header
size of the next frame, and the decoder took any non-zero result as a
frame still in progress, because its guard checked input_.size, which
is the whole input, not what is left of it.

In this patch the decoder marks a frame as in progress only when the
call consumed input, and as finished when zstd returns 0. Truncated
input is still reported.
zstdDecompressSyncOutputFillsBufferTest decodes frames of 1x to 4x the
40 KiB first sync buffer size, plus and minus one byte, through
zstdDecompressSync and zstdDecompress. It fails with "unexpected end of
file" without the previous commit.
@littledivy
littledivy requested review from a team as code owners November 20, 2026 09:24

@clanky-the-clanker clanky-the-clanker Bot 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.

Superseded by the review of 3e3ab344.

This review, of 0285d9cf

💬 Approved with comments — The multi-frame decoding logic is correct for the cases the change targets. The only finding is an INFO about output that exactly fills maxOutputLength when ignorable bytes follow it.

Findings: 1 info (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: 💬 1 info

The multi-frame state machine is correct. One edge case at the maxOutputLength limit is noted.

Recommendation: 💬 Optional Nits

  • [INFO] Output that exactly fills maxOutputLength and is followed only by trailing junk or a skippable frame now throws a memory-limit error. Before this change it returned the data. (src/workerd/api/compression.c++:819)
View full review

The checks found no defect in tracking the frame prefix across writes, in empty input still decoding to an empty buffer, in reporting truncation only after a full 4-byte prefix, in resetStream, or in calling the sync loop again after the output fills exactly.

One edge case: a frame whose decompressed size equals maxOutputLength, followed only by trailing junk or a skippable frame. work() returns early in BETWEEN_FRAMES because the output is full, so the bytes after the frame are never inspected. isStreamEnd() then returns false because input is left over, and the sync path throws a memory-limit error. Before this change, the same input returned the decoded data.


✅ All other checks passed

🔒 Security: ✅ LGTM

No security issues found.


📖 AGENTS.md: ✅ LGTM

This change does not need an AGENTS.md update.


⚡ Performance: ✅ LGTM

No performance issues found.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 0285d9cf

Comment thread src/workerd/api/compression.c++ Resolved
The zstd decoder called ZSTD_decompressStream once per write. When a
frame ended with input left and output space free, the sync loop
stopped, so zstdDecompressSync and zstdDecompress returned only the
first frame. A leading skippable frame, as pzstd writes before every
frame, also ends with a 0 result, so the real frame after it was
dropped and the output was empty. In a stream, the unread bytes after
the first frame were lost, and the next write failed with "Unknown
frame descriptor" when the second frame started partway into the
previous write. With maxOutputLength equal to the first frame's size,
the first frame came back with no error.

In this patch the decoder works like Node's: it keeps decoding while a
frame has just ended and input is left. The first 4
bytes after a frame are checked against the zstd and skippable frame
magic numbers, across writes, and input that is neither is ignored as
trailing junk. "unexpected end of file" still reports a frame that was
cut short, and an empty input still decodes to an empty buffer. Input
that is ignored after a frame doesn't count against maxOutputLength, so a
frame that exactly fills it still decodes.
The new tests cover two and three frames back to back, skippable frames
before, between and after real ones, a second frame split across two
stream writes at offsets 1 to 8, trailing junk, a truncated second
frame, maxOutputLength across frames and a frame that fills
maxOutputLength before ignored input, through zstdDecompressSync,
zstdDecompress and createZstdDecompress. The trailing junk, empty input
and filled maxOutputLength cases check that existing behaviour stays the
same; the others fail without the previous commit. The same tests pass
in Node 26.12 except for empty input, where Node throws.
@littledivy
littledivy force-pushed the littledivy/2026-11-20-node-zstd-multiframe branch from 0285d9c to 3e3ab34 Compare November 20, 2026 09:34

@clanky-the-clanker clanky-the-clanker Bot 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.

✅ Approved — Multi-frame, skippable-frame and trailing-junk handling is correct, and the earlier maxOutputLength edge case is fixed and tested. No issues remain.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The prefix check reads at most 4 bytes and stays within the input buffer. Output is still bounded by the existing maxOutputLength checks, and every loop iteration either consumes input or returns.


📖 AGENTS.md: ✅ LGTM

This change does not need an AGENTS.md update.


📋 Code Quality: ✅ LGTM

work() keeps looping between frames even when the output is full, so trailing junk and skippable frames after a frame that exactly fills maxOutputLength are now handled, and isStreamEnd() returns true. The frame prefix carries over correctly across writes, the loop always terminates, truncation is reported only after a full 4-byte prefix, and resetStream() restores the initial state.


⚡ Performance: ✅ LGTM

Decoding cost stays linear in the input size, and the extra work at each frame boundary is a check of at most 4 bytes.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 3e3ab344

@littledivy
littledivy merged commit 84c16be into main Nov 21, 2026
24 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-20-node-zstd-multiframe branch November 21, 2026 01:39
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.

2 participants