File tree 3 files changed +17
-1
lines changed 3 files changed +17
-1
lines changed 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).
8
8
9
9
You may also find the [ Upgrade Guide] ( https://rust-random.github.io/book/update.html ) useful.
10
10
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
+
11
16
## [ 0.8.1] - 2020-12-31
12
17
### Other
13
18
- Enable all stable features in the playground (#1081 )
Original file line number Diff line number Diff line change 1
1
[package ]
2
2
name = " rand"
3
- version = " 0.8.1 "
3
+ version = " 0.8.2 "
4
4
authors = [" The Rand Project Developers" , " The Rust Project Developers" ]
5
5
license = " MIT OR Apache-2.0"
6
6
readme = " README.md"
Original file line number Diff line number Diff line change @@ -521,6 +521,12 @@ macro_rules! uniform_int_impl {
521
521
let high = * high_b. borrow( ) ;
522
522
assert!( low <= high, "UniformSampler::sample_single_inclusive: low > high" ) ;
523
523
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
+
524
530
let zone = if :: core:: $unsigned:: MAX <= :: core:: u16 :: MAX as $unsigned {
525
531
// Using a modulus is faster than the approximation for
526
532
// i8 and i16. I suppose we trade the cost of one
@@ -1235,6 +1241,11 @@ mod tests {
1235
1241
let v = <$ty as SampleUniform >:: Sampler :: sample_single( low, high, & mut rng) ;
1236
1242
assert!( $le( low, v) && $lt( v, high) ) ;
1237
1243
}
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
+ }
1238
1249
}
1239
1250
} } ;
1240
1251
You can’t perform that action at this time.
0 commit comments