Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
### Fixes
- Fix `WeightedIndex` panic when the sum of float weights is infinite; return `Error::Overflow` instead ([#1808])
- Fix spurious `Error::NonFinite` from `Uniform::new_inclusive` on large finite float ranges such as `0.0..=f64::MAX` ([#1821])
- Fix possible panic due to sampling a deserialized `Uniform<char>` ([#1831])

[#1808]: https://github.com/rust-random/rand/pull/1808
[#1821]: https://github.com/rust-random/rand/pull/1821
[#1831]: https://github.com/rust-random/rand/pull/1831

## [0.10.2] — 2026-07-02

Expand Down
10 changes: 9 additions & 1 deletion src/distr/uniform_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,15 @@ macro_rules! uniform_int_impl {
#[allow(unused)]
#[inline]
pub(crate) fn max(&self) -> $ty {
self.range.wrapping_sub(1).wrapping_add(self.low)
if self.range == 0 {
return <$ty>::MAX;
} else {
// Wrapping through <$ty>::MIN is possible with a valid
// sampler over signed types. Wrapping through <$ty>::MAX is
// possible with a bad sampler (constructible using serde).
let max = self.low.wrapping_add(self.range.wrapping_sub(1));
if max < self.low { <$ty>::MAX } else { max }
}
Comment thread
dhardy marked this conversation as resolved.
}
}

Expand Down
31 changes: 19 additions & 12 deletions src/distr/uniform_other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,19 +316,26 @@ mod tests {
#[test]
#[cfg(feature = "serde")]
fn test_char_bad_deser() {
let json = r#"{"sampler":{"low":4294967200,"range":0,"thresh":0}}"#;
let result = serde_json::from_str::<Uniform<char>>(json);
assert!(result.is_err());
let err = result.unwrap_err();
assert_eq!(err.classify(), serde_json::error::Category::Data);

#[cfg(feature = "alloc")]
{
assert_eq!(
alloc::string::ToString::to_string(&err),
"bad sampler range for UniformChar at line 1 column 51"
);
fn do_test(json: &str, col: usize) {
let result = serde_json::from_str::<Uniform<char>>(json);
assert!(result.is_err());
let err = result.unwrap_err();
assert_eq!(err.classify(), serde_json::error::Category::Data);

#[cfg(feature = "alloc")]
{
let msg =
alloc::format!("bad sampler range for UniformChar at line 1 column {col}");
assert_eq!(alloc::string::ToString::to_string(&err), msg);
}
}

do_test(r#"{"sampler":{"low":5,"range":0,"thresh":0}}"#, 42);
do_test(r#"{"sampler":{"low":4294967200,"range":0,"thresh":0}}"#, 51);
Comment thread
Copilot marked this conversation as resolved.
do_test(
r#"{"sampler":{"low":4294967280,"range":32,"thresh":0}}"#,
52,
);
}

#[test]
Expand Down