File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -8,6 +8,11 @@ A [separate changelog is kept for rand_core](rand_core/CHANGELOG.md).
88
99You may also find the [ Upgrade Guide] ( https://rust-random.github.io/book/update.html ) useful.
1010
11+ ## [ 0.8.2] - 2021-01-12
12+ ### Fixes
13+ - Fix panic in ` UniformInt::sample_single_inclusive ` and ` Rng::gen_range ` when
14+ providing a full integer range (eg ` 0..=MAX ` ) (#1087 )
15+
1116## [ 0.8.1] - 2020-12-31
1217### Other
1318- Enable all stable features in the playground (#1081 )
Original file line number Diff line number Diff line change 11[package ]
22name = " rand"
3- version = " 0.8.1 "
3+ version = " 0.8.2 "
44authors = [" The Rand Project Developers" , " The Rust Project Developers" ]
55license = " MIT OR Apache-2.0"
66readme = " README.md"
Original file line number Diff line number Diff line change @@ -521,6 +521,12 @@ macro_rules! uniform_int_impl {
521521 let high = * high_b. borrow( ) ;
522522 assert!( low <= high, "UniformSampler::sample_single_inclusive: low > high" ) ;
523523 let range = high. wrapping_sub( low) . wrapping_add( 1 ) as $unsigned as $u_large;
524+ // If the above resulted in wrap-around to 0, the range is $ty::MIN..=$ty::MAX,
525+ // and any integer will do.
526+ if range == 0 {
527+ return rng. gen ( ) ;
528+ }
529+
524530 let zone = if :: core:: $unsigned:: MAX <= :: core:: u16 :: MAX as $unsigned {
525531 // Using a modulus is faster than the approximation for
526532 // i8 and i16. I suppose we trade the cost of one
@@ -1235,6 +1241,11 @@ mod tests {
12351241 let v = <$ty as SampleUniform >:: Sampler :: sample_single( low, high, & mut rng) ;
12361242 assert!( $le( low, v) && $lt( v, high) ) ;
12371243 }
1244+
1245+ for _ in 0 ..1000 {
1246+ let v = <$ty as SampleUniform >:: Sampler :: sample_single_inclusive( low, high, & mut rng) ;
1247+ assert!( $le( low, v) && $le( v, high) ) ;
1248+ }
12381249 }
12391250 } } ;
12401251
You can’t perform that action at this time.
0 commit comments