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
dhardy
reviewed
Nov 26, 2026
Comment on lines
+207
to
+208
| } else if let Err(()) = deferred.checked_add_assign(w) { | ||
| return Err(Error::Overflow); |
Member
There was a problem hiding this comment.
Do you really think it's important to have a secondary processing path for overflows?
Member
There was a problem hiding this comment.
returns Ok today
This is only true in release builds. An application which breaks in debug builds is still broken, so there shouldn't be anyone depending on this behaviour in the current code.
Author
There was a problem hiding this comment.
No, you're right. That case panics in debug builds, so nothing can rely on it. I removed the second path in 7115b46. Each weight is now added with checked_add_assign, as in new, and any overflow returns Error::Overflow. The test now expects Overflow for that update too, with self unchanged.
dhardy
approved these changes
Nov 28, 2026
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, asnewdoes, and any overflow returnsError::Overflow, including an intermediate one in a multi-index update. For floatschecked_add_assignnever fails, so the float arithmetic is unchanged.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)).cargo test(default,serde,log,--lib --tests --no-default-features [--features=alloc,sys_rng],+nightly --all-features),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.