Skip to content

fix(c-api): refuse a decompression bound the result cannot carry - #668

Merged
polaz merged 2 commits into
mainfrom
fix/#663-decompress-bound-sentinel
Nov 14, 2026
Merged

polaz merged 2 commits into
mainfrom
fix/#663-decompress-bound-sentinel

Conversation

@polaz

@polaz polaz commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

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 decompress: reject unrepresentable decompression bounds 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

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.

- ZSTD_decompressBound returned a total of exactly u64::MAX as it was,
  which reads as ZSTD_CONTENTSIZE_UNKNOWN, a value this function never
  returns; a single frame declaring that size did the same. The largest
  bound returned is now ZSTD_CONTENTSIZE_ERROR - 1 and anything at or
  past the sentinels is ZSTD_CONTENTSIZE_ERROR, the boundary upstream
  closes in meta/zstd#4954.
- The sum over frames moves into the codec as decoding::decompressed_bound
  (the whole of u64 is a bound there, a sum past it an overflow), so the
  C ABI only maps the result onto its sentinels.
- A frame's on-disk length and its bound come from one walk of its
  blocks; the bound of a run of frames walked each frame twice.
- Regression tests: 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.

Closes #663
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-14T10:08:05.524046Z 79733f8 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Explain Change →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 52 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between cb467b7 and 79733f8.

Files selected for processing: 1

  • zstd/src/decoding/mod.rs

No actionable comments were generated in the recent review.

Walkthrough

The decoder now calculates and sums decompressed bounds across concatenated frames. ZSTD_decompressBound delegates that calculation and returns CONTENTSIZE_ERROR when the total cannot be represented below the sentinel.

Changes: Decompression Bounds

Layer / File(s) Summary
Frame extent and bound accumulation
zstd/src/decoding/mod.rs, zstd/src/decoding/frame_inspection_tests.rs
Shared frame inspection returns each frame’s compressed length and decompressed bound. The new decompressed_bound function sums frame bounds with checked addition. Tests cover skippable frames, frames without declared content sizes, overflow, and malformed input.
C API sentinel mapping and coverage
c-api/src/frame.rs, c-api/src/tests.rs, c-api/src/tests/legacy.rs
ZSTD_decompressBound uses decompressed_bound and returns CONTENTSIZE_ERROR for errors or totals at or above that value. Tests cover boundary totals, multiple frames, skippable frames, legacy frames, and malformed input.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Low

Merge Risk: Minimal · previous review cb467

ZSTD_decompressBound now returns the error sentinel when the summed bound cannot be represented, instead of a misleading numeric value. The frame-bound logic moves into the codec with matching tests. I found no concrete merge-blocking risk; the full test suite and platform matrix still run in CI.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the change: the C ABI now refuses a decompression bound that the result cannot carry. The changeset matches this. The decompressed_bound function in the codec sums frame bounds, and …
Linked Issues check Passed Issue #663 has one active direct link and no coding requirement is unmet.

1. Summation in the codec: decoding::decompressed_bound (zstd/src/decoding/mod.rs) sums each frame's bound with `checked_ad…
Out of Scope Changes check Passed Every changed area ties to #663 or to its direct support.

- zstd/src/decoding/mod.rs: frame_extent shares one block walk between the compressed length and the bound. decompressed_bound is the cod…
Full details: Title check

Explanation

The title names the change: the C ABI now refuses a decompression bound that the result cannot carry. The changeset matches this. The decompressed_bound function in the codec sums frame bounds, and ZSTD_decompressBound returns ZSTD_CONTENTSIZE_ERROR for totals at or above that sentinel. The title is short, specific, and follows a conventional commit prefix. It is a single sentence of 9 words and 64 characters.

Full details: Linked Issues check

Explanation

Issue #663 has one active direct link and no coding requirement is unmet.

  1. Summation in the codec: decoding::decompressed_bound (zstd/src/decoding/mod.rs) sums each frame's bound with checked_add. A sum past u64::MAX returns DecompressedSizeError::Overflow. Skippable frames add 0. Malformed or truncated frames and trailing bytes return DecompressedSizeError::Frame. The C ABI no longer holds the loop.
  2. Sentinel mapping: ZSTD_decompressBound (c-api/src/frame.rs) returns the total only when total < CONTENTSIZE_ERROR. Any error or any total at or above that value returns CONTENTSIZE_ERROR. The largest returned value is CONTENTSIZE_ERROR - 1. The function cannot return ZSTD_CONTENTSIZE_UNKNOWN, which is u64::MAX.
  3. Boundary cases: a two-frame sum of 0xFFFFFFFFFFFFFF00 and 0xFF equals u64::MAX, which is at or above the sentinel, so the result is CONTENTSIZE_ERROR. The three-frame case uses the running total, because the loop checks each frame's bound against the accumulated sum.
  4. No declared size: frame_extent returns walk.count * block_size_max (saturating) as the bound.
  5. Legacy tail: a test in c-api/src/tests/legacy.rs covers a v0.5 to v0.7 tail frame when legacy is enabled.
  6. Tests: c-api/src/tests.rs and zstd/src/decoding/frame_inspection_tests.rs add the boundary, skippable, truncated, and trailing-byte cases.

The shared walk in frame_extent keeps find_frame_compressed_size and frame_decompressed_bound behavior for the paths shown in the diff. This review read only the diffs of mod.rs and frame.rs. The test bodies were not read, so their assertions rest on the summary.

Full details: Out of Scope Changes check

Explanation

Every changed area ties to #663 or to its direct support.

  • zstd/src/decoding/mod.rs: frame_extent shares one block walk between the compressed length and the bound. decompressed_bound is the codec sum that #663 asks for. DecompressedSizeError doc wording changes to match the sum.
  • c-api/src/frame.rs: the summation moves out of the C ABI, the sentinel mapping is added, and the doc comment states the new limit. Imports change to match.
  • Tests in c-api/src/tests.rs, c-api/src/tests/legacy.rs, and zstd/src/decoding/frame_inspection_tests.rs cover the #663 acceptance cases.

The shared walk is a refactor needed by decompressed_bound. It does not add behavior beyond the summed bound. No unrelated file or feature appears in the diff.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between b5b1b77 and cb467b7.

Files selected for processing: 5

  • c-api/src/frame.rs
  • c-api/src/tests.rs
  • c-api/src/tests/legacy.rs
  • zstd/src/decoding/frame_inspection_tests.rs
  • zstd/src/decoding/mod.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cb467b7abc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread zstd/src/decoding/mod.rs Resolved
The sums over concatenated frames step by each frame's on-disk length,
which is at least its header and ends inside the buffer. The C ABI loop
this replaced tested that at run time; the walk guarantees it, so the
test stays as a debug assertion in both sums.

Part of #663
@polaz

polaz commented Nov 14, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 79733f8ba3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@polaz
polaz enabled auto-merge (squash) November 14, 2026 10:08
@codecov

codecov Bot commented Nov 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@polaz
polaz merged commit b120521 into main Nov 14, 2026
36 checks passed
@polaz
polaz deleted the fix/#663-decompress-bound-sentinel branch November 14, 2026 10:27
@sw-release-bot sw-release-bot Bot mentioned this pull request Nov 14, 2026
polaz pushed a commit that referenced this pull request Nov 14, 2026
## 🤖 New release

* `structured-zstd`: 0.3.0 -> 0.4.0

<details><summary><i><b>Changelog</b></i></summary><p>

<blockquote>

##
[0.4.0](v0.3.0...v0.4.0)
- 2026-11-14

### Added

- *(decoding)* [**breaking**] hold dictionary content in the caller's
buffer
([#649](#649))
- *(encode)* let the caller choose how a frame uses a dictionary
([#641](#641))

### Fixed

- *(c-api)* refuse a decompression bound the result cannot carry
([#668](#668))

### Performance

- *(encode)* one dictionary index per dictionary for attached and copied
frames
([#662](#662))
- *(decoding)* decode small frames' long matches without libc memcpy
([#658](#658))
- *(encode)* trim dfast tail seeding and the optimal parser's per-node
work
([#656](#656))
- *(encode)* one page fault per table page on a fresh workspace
([#655](#655))
- *(decoding)* close the x86 decode gap to libzstd
([#652](#652))

### Testing

- *(bench)* measure ours with and without the raw skip
([#644](#644))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/release-plz/release-plz/).

Co-authored-by: sw-release-bot[bot] <255865126+sw-release-bot[bot]@users.noreply.github.com>
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.

fix(c-api): return an error for a decompression bound the result cannot represent

1 participant