Skip to content
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.

## [Unreleased]

### Fixes
- Fix possible memory safety violation due to deserialization of `UniformChar` from bad source ([#1790])

### Changes
- Document required output order of fn `partial_shuffle` and apply `#[must_use]` ([#1769])
- Avoid usage of `unsafe` in contexts where non-local memory corruption could invalidate contract ([#1791])

[#1769]: https://github.com/rust-random/rand/pull/1769
[#1790]: https://github.com/rust-random/rand/pull/1790
[#1791]: https://github.com/rust-random/rand/pull/1791

## [0.10.1] — 2026-02-11
Expand Down
11 changes: 11 additions & 0 deletions src/distr/uniform_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ pub struct UniformInt<X> {

macro_rules! uniform_int_impl {
($ty:ty, $uty:ty, $sample_ty:ident) => {
impl UniformInt<$ty> {
/// Get the maximum possible value
#[allow(unused)]
#[inline]
pub(crate) fn max(&self) -> $ty {
self.range.wrapping_sub(1).wrapping_add(self.low)
}
}

impl SampleUniform for $ty {
type Sampler = UniformInt<$ty>;
}
Expand Down Expand Up @@ -693,6 +702,7 @@ mod tests {
let r = Uniform::try_from(2u32..7).unwrap();
assert_eq!(r.0.low, 2);
assert_eq!(r.0.range, 5);
assert_eq!(r.0.max(), 6);
}

#[test]
Expand All @@ -707,6 +717,7 @@ mod tests {
let r = Uniform::try_from(2u32..=6).unwrap();
assert_eq!(r.0.low, 2);
assert_eq!(r.0.range, 5);
assert_eq!(r.0.max(), 6);
}

#[test]
Expand Down
33 changes: 33 additions & 0 deletions src/distr/uniform_other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,24 @@ impl SampleUniform for char {
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct UniformChar {
#[cfg_attr(feature = "serde", serde(deserialize_with = "deser_sampler"))]
sampler: UniformInt<u32>,
}

#[cfg(feature = "serde")]
fn deser_sampler<'de, D>(d: D) -> Result<UniformInt<u32>, D::Error>
where
D: serde::Deserializer<'de>,
{
let sampler = <UniformInt<u32> as serde::Deserialize>::deserialize(d)?;
if sampler.max() > char::MAX as u32 - CHAR_SURROGATE_LEN {
return Err(serde::de::Error::custom(
"bad sampler range for UniformChar",
));
}
Ok(sampler)
}

/// UTF-16 surrogate range start
const CHAR_SURROGATE_START: u32 = 0xD800;
/// UTF-16 surrogate range size
Expand Down Expand Up @@ -298,6 +313,24 @@ 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"
);
}
}

#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_durations() {
Expand Down
1 change: 1 addition & 0 deletions src/seq/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,7 @@ mod test {

#[test]
#[cfg(feature = "std")]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_multiple_weighted_distributions() {
use super::*;

Expand Down
Loading