`SizeHint` addition overflows (panics in debug builds, wraps in release)

Đang mở Phù hợp với người mới
#183 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
backend-api-design

Hướng nghiên cứu

Issue trỏ đến src/size_hint.rs, nơi Add for SizeHint bị tràn số khi kết hợp các giới hạn. Trước tiên, hãy đọc phần triển khai đó và các bài kiểm thử gần đó, sau đó kiểm tra xem giới hạn dưới bão hòa được đề xuất và giới hạn trên có kiểm tra phù hợp với hành vi hiện tại như thế nào. Công việc hoàn tất khi phép cộng bị tràn không còn gây panic hoặc quay vòng, và các bài kiểm thử bao quát các giới hạn thu được.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Rust
Star
173
Fork
71
Merge trung bình
1 ngày 12 giờ
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của hyperium/http-body

Tất cả issue của hyperium/http-body

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.