`SizeHint` addition overflows (panics in debug builds, wraps in release)
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- rust
- Domain
- backend-api-design
Research direction
The issue points to src/size_hint.rs, where Add for SizeHint overflows while combining bounds. Read that implementation and its nearby tests first, then check how the proposed saturating lower bound and checked upper bound fit the existing behavior. Done means overflowing additions no longer panic or wrap, and tests cover the resulting bounds.
Written by the indexing model from the issue text.
Description
impl Add for SizeHint adds the lower and upper bounds with plain +, so it panics with "attempt to add with overflow" in debug builds when a sum exceeds u64::MAX. In release builds it wraps, and the result can be a hint whose bounds are smaller than either input.
// Cargo.toml: http-body = { git = "https://github.com/hyperium/http-body" }
use http_body::SizeHint;
fn main() {
let s = SizeHint::with_exact(u64::MAX) + SizeHint::with_exact(1);
println!("{:?} {:?}", s.lower(), s.upper());
}
Debug build:
thread 'main' panicked at http-body/src/size_hint.rs:95:20:
attempt to add with overflow
Release build: prints 0 Some(0).
I expected the sum to saturate at u64::MAX (a size hint that cannot be represented exactly is still a valid lower bound when saturated), or the overflow to be documented. Checked on master at c6ca645.
fn add(self, rhs: Self) -> Self::Output {
SizeHint {
lower: self.lower() + rhs.lower(), // overflows
upper: self.upper().and_then(|this| rhs.upper().map(|rhs| this + rhs)), // overflows
}
}
Suggested fix: self.lower().saturating_add(rhs.lower()), and for the upper bound checked_add, giving None (no upper bound) when the sum does not fit.
Kani harness that fails on master (cargo kani --harness add_does_not_overflow, appended to src/size_hint.rs):
#[cfg(kani)]
mod kani_proofs {
use super::SizeHint;
#[kani::proof]
fn add_does_not_overflow() {
let a = SizeHint::with_exact(kani::any());
let b = SizeHint::with_exact(kani::any());
let _ = a + b;
}
}
Failed Checks: attempt to add with overflow
File: "src/size_hint.rs", line 95, in <size_hint::SizeHint as std::ops::Add>::add
VERIFICATION:- FAILED
It takes about 1.6 s and under 200 MB.
This was found with Kani 0.68.0 in an experiment where AI agents verified popular crates using the Kani agent plugin (model-checking/kani#5059). The text was written with AI assistance; the reproducer and the Kani harness were re-run by hand on the commit named above.
- Dominant language
- Rust
- Stars
- 173
- Forks
- 71
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 1
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from hyperium/http-body
-
Difficulty 4/5 3-5 days Newbie friendliness 38/100
-
Feature Proposal: Add `try_clone` method to `Body` traitPossibly taken @pearzl claimed this 380 days ago. Open
Difficulty 5/5 Over a week Newbie friendliness 35/100
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
Empty's Body impl `Error = Infallible` makes `Empty::new()` unable to be used w/ Body constraintsOpen
Difficulty 4/5 3-5 days Newbie friendliness 35/100
All issues in hyperium/http-body
Similar issues
-
An EXE started from the shell with MAXALLOC FFFFh leaves a 0-paragraph free block after its ownOpenbug low-priority
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
dividebysandwich/rust-dos#629 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
eliasstravik/herdr-projects#758 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 1 day
-
enhancement needs:sonnet ready-for-agent
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
kinqsradio/orbyn-engineering#297 ·
Maintainers usually reply within 1 day