Skip to content

decompress: reject unrepresentable decompression bounds - #4954

Merged
terrelln merged 1 commit into
meta:devfrom
terrelln:2026-11-12-decompress-bound-overflow
Nov 15, 2026
Merged

terrelln merged 1 commit into
meta:devfrom
terrelln:2026-11-12-decompress-bound-overflow

Conversation

@terrelln

Copy link
Copy Markdown
Contributor

Concatenated frame bounds could wrap to a small value. Check each addition before it reaches the reserved content-size values or overflows, and document the numeric-result limit.

Add modern and legacy concatenation boundary regressions: exercise reserved single-frame sizes and a three-frame running total, and keep the real legacy-estimator boundary test focused on the last accepted and first rejected values. Derive frame offsets from their layouts and document the accumulator invariant and exact numeric cutoff. Assert the numeric-bound-or-error contract in the frame-info fuzz target without coupling it to malformed legacy content-size declarations.

Concatenated frame bounds could wrap to a small value. Check each addition
before it reaches the reserved content-size values or overflows, and document
the numeric-result limit.

Add modern and legacy concatenation boundary regressions: exercise reserved
single-frame sizes and a three-frame running total, and keep the real
legacy-estimator boundary test focused on the last accepted and first rejected
values. Derive frame offsets from their layouts and document the accumulator
invariant and exact numeric cutoff. Assert the numeric-bound-or-error contract
in the frame-info fuzz target without coupling it to malformed legacy
content-size declarations.

Leave the pre-existing ZSTD_findDecompressedSize sentinel behavior unchanged;
the issue concerns ZSTD_decompressBound.
@meta-cla meta-cla Bot added the CLA Signed label Nov 13, 2026
polaz added a commit to structured-world/structured-zstd that referenced this pull request Nov 14, 2026
## Summary

- `ZSTD_decompressBound` returns a number below its sentinels or
`ZSTD_CONTENTSIZE_ERROR`. A total of exactly `u64::MAX` (or a single
frame declaring that size) used to come back as is, which reads as
`ZSTD_CONTENTSIZE_UNKNOWN`, a value this function never returns. The
largest bound returned is `ZSTD_CONTENTSIZE_ERROR - 1`, the boundary
upstream closes in meta/zstd#4954.
- The sum over frames lives in the codec, so a Rust caller gets the same
answer the C ABI maps onto its sentinels.

## Changes

- `decoding::decompressed_bound`: the bound of every frame in a buffer
together, skippable frames counting zero. The whole of `u64` is a bound
there; a sum past it is `DecompressedSizeError::Overflow`, a damaged
frame or stray bytes after the last one that frame's error.
- `ZSTD_decompressBound` calls it and returns `ZSTD_CONTENTSIZE_ERROR`
for any total at or above that value.
- A frame's on-disk length and its bound come from one walk of its
blocks (`find_frame_compressed_size` and `frame_decompressed_bound`
share it); bounding a run of frames walked each frame twice.
- A single frame declaring `u64::MAX` is an error here. Upstream treats
that field value as undeclared and returns the block bound; its
regression only requires that the result is not
`ZSTD_CONTENTSIZE_UNKNOWN`.

## Testing

- The boundary cases of upstream's `test_decompressBoundOverflow`
through the C ABI, the same boundary with a v0.5 to v0.7 frame as the
tail, and the codec's sum; the first failed before the fix.
- C ABI tests, the codec's frame inspection tests with and without the
`legacy` feature, clippy, formatting, the no_std check and the doc
example pass on aarch64; the full suite and the platform matrix run in
CI.

Closes #663


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Bug Fixes**
* Decompression size estimates now account for all frames in
concatenated input, including frames without a declared content size.
* Skippable frames contribute zero to the estimate, while malformed or
incomplete frames produce an error instead of an unreliable result.
* Results that exceed the representable limit are reported as a
content-size error, including when the limit is reached across multiple
frames.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
@terrelln
terrelln merged commit 6404711 into meta:dev Nov 15, 2026
109 checks passed
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