Skip to content

Don't report a truncated zstd frame when the output fills exactly - #8598

Merged
2 commits merged into
mainfrom
littledivy/2026-11-18-node-zstd-eof
Nov 21, 2026
Merged

2 commits merged into
mainfrom
littledivy/2026-11-18-node-zstd-eof

Conversation

@littledivy

Copy link
Copy Markdown
Member

Fixes #7923.

zstdDecompressSync and zstdDecompress threw "unexpected end of file" for a valid frame when the decompressed size exactly filled the sync output buffer. That buffer starts at 40960 bytes and doubles, so 81920 and 163840 failed too. When the output buffer is full, the sync loop calls ZSTD_decompressStream once more after all the input has been read. On that call zstd returns the header size of the next frame. The decoder treated any non-zero result as a frame still in progress, because its guard checked input_.size, which is the size of the whole input rather than what is left of it.

In this patch the decoder marks a frame as in progress only when the call actually read input, and marks it finished when zstd returns 0. Truncated input still raises the error. The new zstdDecompressSyncOutputFillsBufferTest tries sizes around 1x to 4x the first buffer size with both zstdDecompressSync and zstdDecompress, and it fails without the fix.

@littledivy
littledivy requested review from a team as code owners November 18, 2026 08:18

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

This review, of 1f9c301d

✅ Approved — The fix to frame-in-progress tracking in ZstdDecoderContext::work() is correct and has a regression test. No issues found.

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


✅ All checks passed

📋 Code Quality: ✅ LGTM

The new frame-state logic is correct: a return value of 0 ends the frame, a call that reads input marks it in progress, and a call that reads nothing leaves the state unchanged. The regression test covers the buffer-boundary sizes.


📝 Documentation: ✅ LGTM

The new code comment describes the current behaviour accurately.


Reviewed commit: 1f9c301d

@maschwenk

Copy link
Copy Markdown
Contributor

tested with a differential harness against node 26.13 on arm64 and x86_64. the 40960/81920/163840 failures are gone from sync and callback and nothing else changed

still broken in the same decoder: input with several frames decodes to just the first one, input starting with a skippable frame decodes to an empty buffer (pzstd writes one before every frame), and a stream fails with "Unknown frame descriptor" when the second frame starts partway into one write and continues into the next. isStreamEnd() also still means the last call returned 0, so a maxOutputLength equal to the first frame's size drops the second frame instead of throwing ERR_BUFFER_TOO_LARGE

I ported node's frame loop from ZstdDecompressContext in a commit on top of this. can open it after this lands or fold it in here: https://github.com/maschwenk/workerd/commits/zstd-decoder-frames

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 force-pushed the littledivy/2026-11-18-node-zstd-eof branch from 1f9c301 to f2c20c2 Compare November 19, 2026 07:57

@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 — The fix to frame-in-progress tracking in ZstdDecoderContext::work() is correct and has a regression test. No issues found.

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


✅ All checks passed

📋 Code Quality: ✅ LGTM

The new frame-state logic is correct: a return value of 0 ends the frame, a call that reads input marks it in progress, and a call that reads nothing leaves the state unchanged. The regression test covers the buffer-boundary sizes.


📝 Documentation: ✅ LGTM

The new code comment describes the current behaviour accurately.


Reviewed commit: f2c20c21 — same changes as 1f9c301d, so this is that commit's review (no new review was run; line numbers are as of 1f9c301d)

@littledivy littledivy closed this pull request by merging all changes into main in 84c16be Nov 21, 2026
@littledivy
littledivy deleted the littledivy/2026-11-18-node-zstd-eof 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.

🐛 Bug Report — Runtime APIs: node:zlib zstdDecompressSync throws "unexpected end of file" for valid frames of 40960, 81920, 163840… bytes

3 participants