Skip to content

zstd: stream a small buffer when its in-memory decode exceeds the size limit - #1207

Merged
klauspost merged 1 commit into
klauspost:masterfrom
honeycombio:lizf.zstd-reader-shortcut-limit
Sep 8, 2026
Merged

klauspost merged 1 commit into
klauspost:masterfrom
honeycombio:lizf.zstd-reader-shortcut-limit

Conversation

@lizthegrey

@lizthegrey lizthegrey commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1206.

Decoder.Reset decodes a Bytes()/Len() source shorter than WithDecodeBuffersBelow in one go through DecodeAll, where WithDecoderMaxMemory caps the total decoded size. A Reader is documented with the streaming contract, where the option caps the window, so the same Reader followed one rule or the other depending on input length.

Change: when the shortcut's DecodeAll reports ErrDecoderSizeExceeded, fall through to the normal streaming decode of the same reader (the shortcut has not consumed its bytes). Memory stays bounded as documented: the in-memory attempt stops at the cap, its buffer is dropped rather than kept for reuse, and streaming holds a window.

Test: TestDecoderBufferShortcutFallsBackToStreaming encodes 4 MiB with a 64 KiB window (a few hundred compressed bytes), sets a 1 MiB limit, asserts DecodeAll still fails as documented, and asserts a Reader over a bytes.Buffer and over a plain io.Reader both produce the full output. It fails on master with decompressed size exceeds configured limit on the buffer case. The original 1740-byte fuzz reproducer from #1206 also passes the differential harness with this change.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Decoding now automatically falls back to streaming when an optimized in-memory decode exceeds the configured maximum memory limit.
    • Readers backed by in-memory byte sources now behave consistently with standard readers, allowing oversized compressed data to decode successfully without exceeding the configured memory cap.
    • Added coverage to verify successful decoding and output integrity across both decoding paths.

…e limit

Decoder.Reset decodes a source with Bytes and Len shorter than
WithDecodeBuffersBelow in one go through DecodeAll, where
WithDecoderMaxMemory caps the total decoded size. A Reader is documented
with the streaming contract, where the option caps the window instead,
so the same Reader followed one rule or the other depending on input
length: a 1740-byte frame with no content size that expands past a
20 MiB limit failed over a bytes.Buffer and decoded over any other
io.Reader.

When the shortcut's DecodeAll reports ErrDecoderSizeExceeded, fall
through to the normal streaming decode of the same reader, whose bytes
the shortcut has not consumed. Memory stays bounded as documented: the
in-memory attempt stops at the cap, its buffer is dropped, and streaming
holds a window.

Found by a differential fuzz harness while fuzzing klauspost#1204; the stock
fuzzers share the threshold on both sides and cannot see it.

Fixes klauspost#1206

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Team

Run ID: c5be7302-d988-46ac-9fe5-17a46653ffa2

📥 Commits

Reviewing files that changed from the base of the PR and between 75489f9 and 92f1ca8.

📒 Files selected for processing (2)
  • zstd/decoder.go
  • zstd/decoder_test.go

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The decoder now falls back from DecodeAll to streaming when the configured decoded-size limit is exceeded. The change discards the oversized buffer and adds coverage for byte-backed and plain reader sources.

Changes

Decoder shortcut fallback

Layer / File(s) Summary
Synchronous decode fallback and regression coverage
zstd/decoder.go, zstd/decoder_test.go
Decoder.Reset detects ErrDecoderSizeExceeded, drops the shortcut buffer, and continues through streaming decode. The test verifies equivalent output for bytes.Buffer and plain io.Reader sources under WithDecoderMaxMemory.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 92f1c

Small buffered zstd Reader inputs that exceed the in-memory decode limit now continue with bounded streaming decode, matching plain-reader behavior. The targeted regression coverage supports merge readiness with no remaining actionable risk.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: streaming fallback when in-memory decoding exceeds the configured size limit.
Linked Issues check ✅ Passed The changes satisfy issue #1206. Decoder.Reset falls back to streaming after DecodeAll returns ErrDecoderSizeExceeded, preserves the unconsumed input, releases the oversized buffer, and adds cov…
Out of Scope Changes check ✅ Passed The changes are limited to the decoder fallback logic and targeted regression tests. They directly support the linked issue and stated objectives.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@klauspost
klauspost merged commit ac559c1 into klauspost:master Sep 8, 2026
29 checks passed
@lizthegrey
lizthegrey deleted the lizf.zstd-reader-shortcut-limit branch September 8, 2026 23:44
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.

zstd: Reader over a small bytes.Buffer applies the DecodeAll size limit instead of the streaming window limit

2 participants