Skip to content

Fix integer overflow in combinations size-hint tests - #1131

Open
00200200 wants to merge 1 commit into
rust-itertools:masterfrom
00200200:fix-combinations-size-hint-overflow
Open

00200200 wants to merge 1 commit into
rust-itertools:masterfrom
00200200:fix-combinations-size-hint-overflow

Conversation

@00200200

@00200200 00200200 commented Sep 29, 2026 •

Copy link
Copy Markdown

Fixes #995.

combinations_inexact_size_hints panics on 32-bit because the test helper computes binomial coefficients with a naive product-then-divide. That overflows usize even when the result fits (C(27, 7) on 32-bit; C(21, 19) even on 64-bit). Use the same overflow-safe algorithm as checked_binomial.

Test plan

  • cargo test --test test_std binomial_avoids_intermediate_overflow — failed (overflow panic) before the helper change, passes after
  • cargo test --test test_std combinations — combinations_inexact_size_hints and combinations_range_count pass
  • cargo test --tests — all integration tests pass
  • cargo fmt --all

The naive product-then-divide binomial helper overflowed usize (C(27, 7) on 32-bit, C(21, 19) even on 64-bit). Use the same overflow-safe algorithm as checked_binomial.
@codecov

codecov Bot commented Sep 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.83%. Comparing base (6814180) to head (30a5656).
⚠️ Report is 220 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1131      +/-   ##
==========================================
- Coverage   94.38%   93.83%   -0.56%     
==========================================
  Files          48       52       +4     
  Lines        6665     6491     -174     
==========================================
- Hits         6291     6091     -200     
- Misses        374      400      +26     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread tests/quick.rs
if n < k {
return 0;
}
// Same algorithm as `checked_binomial` in `src/adaptors/mod.rs`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's the same algorithm, can't we just call checked_binomial?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because checked_binomial is pub(crate) inside the private adaptors module, it isn't visible to the integration test crates in tests/.

If you'd prefer to share the implementation across tests rather than keeping the local helper, I can expose it with #[doc(hidden)] pub use crate::adaptors::checked_binomial; in lib.rs and call it here as itertools::checked_binomial(n, k).unwrap_or(0). Would you like me to do that?

This branch has not been deployed

No deployments
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.

test failure on 32-bit.

2 participants