Skip to content

Return Error::Overflow from WeightedIndex::update_weights on integer overflow - #1846

Open
youdie006 wants to merge 2 commits into
rust-random:masterfrom
youdie006:update-weights-overflow
Open

youdie006 wants to merge 2 commits into
rust-random:masterfrom
youdie006:update-weights-overflow

Conversation

@youdie006

@youdie006 youdie006 commented Nov 23, 2026 •

Copy link
Copy Markdown
  • Added a CHANGELOG.md entry

Summary

WeightedIndex::update_weights adds integer weights with a plain +=, while new uses checked_add_assign. An update whose sum overflows panics in debug builds and wraps in release builds, contrary to the documented Error::Overflow and "In case of error, self is not modified".

Motivation

let mut w = WeightedIndex::new([200u8, 50]).unwrap();
w.update_weights(&[(1, &100)])
before, debug:    panicked at src/distr/weighted/weighted_index.rs:201:13: attempt to add with overflow
before, release:  Ok(()), total_weight = 44; index 1 is never sampled again (10000 samples: [10000, 0])
after:            Err(Overflow), total_weight = 250; 10000 samples: [8022, 1978]

Details

Each weight is now added with checked_add_assign, as new does, and any overflow returns Error::Overflow, including an intermediate one in a multi-index update. For floats checked_add_assign never fails, so the float arithmetic is unchanged.

This adds a Weight bound to update_weights. new already requires it, so only a WeightedIndex obtained through Deserialize could lack it.

The new update_weights_overflow test fails on master (debug: the panic; release: Ok(()) instead of Err(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 -- --check and cargo clippy --workspace --lib --bins --tests -- -D warnings (stable) pass.

Written with AI assistance (Claude); I have reviewed the change.

…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
youdie006 force-pushed the update-weights-overflow branch from ebf8754 to d7ba2f6 Compare November 23, 2026 05:40
Comment thread src/distr/weighted/weighted_index.rs Outdated
Comment on lines +207 to +208
} else if let Err(()) = deferred.checked_add_assign(w) {
return Err(Error::Overflow);

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.

Do you really think it's important to have a secondary processing path for overflows?

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.

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.

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.

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.

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.

2 participants