Repository navigation
decompress: reject unrepresentable decompression bounds - #4954
Merged
Merged
Conversation
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.
Cyan4973
approved these changes
Nov 14, 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 -->
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.