Skip to content

Commit cdd230d

Browse files
authored
fix(fast_rands): prevent overflow and UB in Rand::between for maximum bounds (#3839)
1 parent eb2ed35 commit cdd230d

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

crates/fast_rands/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ pub trait Rand {
195195
#[inline]
196196
fn between(&mut self, lower_bound_incl: usize, upper_bound_incl: usize) -> usize {
197197
debug_assert!(lower_bound_incl <= upper_bound_incl);
198+
199+
if upper_bound_incl == usize::MAX && lower_bound_incl == 0 {
200+
return self.next() as usize;
201+
}
202+
198203
// # Safety
199204
// We check that the upper_bound_incl <= lower_bound_incl above (alas only in debug), so the below is fine.
200205
// Even if we encounter a 0 in release here, the worst-case scenario should be an invalid return value.

0 commit comments

Comments
 (0)