Repository navigation
Conversation
…overflow new() adds integer weights with checked_add_assign, but update_weights() used a plain +=, so an update whose sum overflows panicked in debug builds and wrapped silently in release builds, leaving a wrong total_weight. An intermediate sum may overflow while the final sum fits, so such weights are added at the end instead of rejected.
youdie006
force-pushed
the
update-weights-overflow
branch
from
November 23, 2026 05:40
ebf8754 to
d7ba2f6
Compare
This branch has not been deployed
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.
CHANGELOG.mdentrySummary
WeightedIndex::update_weightsadds integer weights with a plain+=, whilenewuseschecked_add_assign. An update whose sum overflows panics in debug builds and wraps in release builds, contrary to the documentedError::Overflowand "In case of error,selfis not modified".Motivation
Details
Each weight is now added with
checked_add_assign. An intermediate sum can overflow while the final sum fits ([200u8, 50]updated with(0, &255), (1, &0)returnsOktoday), so a weight that does not fit yet is put aside and added after the loop, withError::Overflowonly if the final sum overflows. For floatschecked_add_assignnever fails, so the float arithmetic and results are unchanged: on 20,000 seeded random multi-index f64 updates the totals and sampled indices are identical to master.This adds a
Weightbound toupdate_weights.newalready requires it, so only aWeightedIndexobtained throughDeserializecould lack it.The new
update_weights_overflowtest fails on master (debug: the panic; release:Ok(())instead ofErr(Overflow)), and the variant that rejects any intermediate overflow fails its second case.cargo test(default,serde,log, and--no-default-features --features=alloc,sys_rng),cargo +1.85 test --lib,cargo fmt --all -- --checkandcargo clippy --workspace --lib --bins --tests -- -D warnings(stable) pass.Written with AI assistance (Claude); I have reviewed the change.