DecodeAll rejects valid frames when appending to non-empty destination buffers

perfloop-oss/compress · Unit mismatch

https://perfloop.ai/t/oss/case_22vnxdm1g3

Verdict

VERIFIED · settled 2026-11-25 · merged as klauspost/compress#1358

What happened: The check fails before this change and passes with it.

Hypothesis

When decoding a Zstandard frame that declares a content size into a non-empty destination slice, frameDec.runDecoder adds the existing prefix length to the frame's content size before checking it against the configured decoded-size limit and against the remaining slice capacity. Because Decoder.DecodeAll is documented to append decoded output to dst, callers that configure WithDecoderMaxMemory to guard against oversized payloads and append records into a growing buffer receive false ErrDecoderSizeExceeded failures even when the decoded frame is small (including zero-byte frames). Real-world reach is estimated as common_config: engineering prior assumes services decoding untrusted or bounded Zstandard payloads routinely configure WithDecoderMaxMemory (the documented safety option against hostile input) and use DecodeAll's append-to-dst contract to assemble batched output or framed responses; the default 64 GiB max memory makes this rare_input under defaults alone, while any lower configured cap triggers it whenever len(dst) + FrameContentSize exceeds the cap.

In zstd/framedec.go:328-346, frameDec.runDecoder initializes maxSyncLen as uint64(cap(dst) - len(dst)) (remaining capacity) when d.o.limitToCap is true, then on lines 334-335 compares and assigns d.FrameContentSize + uint64(len(dst)) (total slice length including the caller's existing prefix), and on line 337 rejects the frame with ErrDecoderSizeExceeded if maxSyncLen > d.o.maxDecodedSize. Every other size limit check in Decoder.DecodeAll (zstd/decoder.go:379, 421, 460) and frameDec.runDecoder (zstd/framedec.go:328, 368, 378, 392) subtracts initialSize or crcStart so the limit applies only to bytes decoded by this call, and sequenceDecs.useSafeDecodeSync (zstd/seqdec_asm.go:108) compares maxSyncLen against cap(s.out) - len(s.out) - compressedBlockOverAlloc (remaining slack). Git history shows the + uint64(len(dst)) term was originally added to size an internal make([]byte, len(dst), maxSyncLen+compressedBlockOverAlloc) allocation that was later removed, leaving the mismatched unit behind. The same bug also breaks the WithDecodeAllCapLimit + WithDecoderMaxMemory combination when cap(dst) - len(dst) exceeds maxDecodedSize and len(dst) + FrameContentSize >= cap(dst) - len(dst), and overstates maxSyncLen on concatenated frames even when initial len(dst) == 0.

A bounded grid search in a sandbox test over maxDecodedSize in {1024, 2048, 4096}, payloadSize in {0, 1, 256, 512, 1024, 1500, 2048, 3000}, prefixLen in {0, 1, 256, 600, 1024, 2048, 4096}, remaining capacity in {0, 512, 1024, 2048, 4096, 8192}, limitToCap in {false, true}, and known vs unknown FrameContentSize found 429 false rejections (all on known-FCS frames; unknown-FCS frames with the identical payload succeeded). An experimental two-line change replacing d.FrameContentSize+uint64(len(dst)) with d.FrameContentSize reduced those 429 failures to 0.

Upstream check: open PR https://github.com/klauspost/compress/pull/1313 modifies zstd/framedec.go to enforce WithDecodeAllCapLimit on raw, RLE, and sequence blocks for frames without a declared content size, and does not change lines 333-342 or the d.FrameContentSize + uint64(len(dst)) calculation, so this defect is distinct.

The attached provisional check TestDecodeAllPrefixWithMaxMemory deterministically reproduces both the default limitToCap=false false rejection and the limitToCap=true false rejection when appending a 256-byte frame to a 1024-byte prefix under WithDecoderMaxMemory(1024), while confirming that payloads exceeding maxMem are still rejected with ErrDecoderSizeExceeded. Case confirmation should verify that removing + uint64(len(dst)) passes the full zstd test suite (including TestWithDecodeAllCapLimit, TestMaxSyncLenTracksEveryBlockType, and seqdec OOB/ASan checks) and preserves exact remaining-byte maxSyncLen tracking across single and concatenated frames with non-empty dst.

Change to test: Set maxSyncLen in frameDec.runDecoder from d.FrameContentSize (the frame's remaining decoded bytes) rather than d.FrameContentSize + uint64(len(dst)), so cap-limit comparisons, the maxDecodedSize guard, and sequence-decoder slack checks use the same per-frame byte unit. Keep rejecting frames whose decoded bytes exceed WithDecoderMaxMemory or WithDecodeAllCapLimit.

Where it lives

perfloop-oss/compress · zstd/framedec.go

Evidence

The check fails before this change and passes with it: `For known-size frames, DecodeAll appends empty output, a 256-byte payload, or two concatenated 512-byte payloads to a 2048-byte prefix under a 1024-byte decoded-size limit, preserving that prefix with either capacity-limit setting.`

DecodeAll Go files SpeedDefault · 26 sample pairs

metric baseline candidate paired median change confidence range required result
ns/op 52853208 52945271 +220314 −2988808 to +2010001 no regression (noise ≤ 0.05 × |baseline median|) PASSED

DecodeAll prefix Go files SpeedDefault · 11 sample pairs

metric baseline candidate paired median change confidence range required result
ns/op 59607288 58462362 −1260048 −5012790 to +2755449 no regression (noise ≤ 0.05 × |baseline median|) PASSED

DecodeAll cap concat Go files SpeedDefault · 52 sample pairs

metric baseline candidate paired median change confidence range required result
ns/op 121155738 122333901 +1696314 −6384629 to +5593012 no regression (noise ≤ 0.05 × |baseline median|) PASSED

DecodeAll Go files SpeedBestCompression · 13 sample pairs

metric baseline candidate paired median change confidence range required result
ns/op 56068752 54741257 −725298 −3455390 to +1041924 no regression (noise ≤ 0.05 × |baseline median|) PASSED

DecodeAll prefix Go files SpeedBestCompression · 27 sample pairs

metric baseline candidate paired median change confidence range required result
ns/op 58893907 58737479 −37428 −1566863 to +2030131 no regression (noise ≤ 0.05 × |baseline median|) PASSED

DecodeAll cap concat Go files SpeedBestCompression · 170 sample pairs

metric baseline candidate paired median change confidence range required result
ns/op 123561083 125640364 +2481811 −3389579 to +5806270 no regression (noise ≤ 0.05 × |baseline median|) PASSED

DecodeAll stack traces SpeedDefault · 28 sample pairs

metric baseline candidate paired median change confidence range required result
ns/op 2770698 2756150 −679 −191181 to +114396 no regression (noise ≤ 0.05 × |baseline median|) PASSED

DecodeAll prefix stack traces SpeedDefault · 36 sample pairs

metric baseline candidate paired median change confidence range required result
ns/op 3315018 3319378 −5571 −119662 to +156548 no regression (noise ≤ 0.05 × |baseline median|) PASSED

DecodeAll cap concat stack traces SpeedDefault · 15 sample pairs

metric baseline candidate paired median change confidence range required result
ns/op 7247135 7145199 −82491 −383050 to +131707 no regression (noise ≤ 0.05 × |baseline median|) PASSED

DecodeAll stack traces SpeedBestCompression · 37 sample pairs

metric baseline candidate paired median change confidence range required result
ns/op 2549130 2550103 −11766 −168679 to +122516 no regression (noise ≤ 0.05 × |baseline median|) PASSED

DecodeAll prefix stack traces SpeedBestCompression · 26 sample pairs

metric baseline candidate paired median change confidence range required result
ns/op 3035412 3041968 +22382 −221981 to +121106 no regression (noise ≤ 0.05 × |baseline median|) PASSED

DecodeAll cap concat stack traces SpeedBestCompression · 30 sample pairs

metric baseline candidate paired median change confidence range required result
ns/op 6477366 6458872 −22875 −324193 to +300975 no regression (noise ≤ 0.05 × |baseline median|) PASSED

Checks: 9 of 9 passed. Verification: no defect found.

Timeline