Skip to content

Commit a300314

Browse files
committed
auto merge of #10213 : telotortium/rust/rand-fill_bytes-stack-overflow, r=huonw
Fix the implementation of `std::rand::Rng::fill_bytes()` for `std::rand::reseeding::ReseedingRng` to call the `fill_bytes()` method of the underlying RNG rather than itself, which causes infinite recursion. Fixes #10202.
2 parents 04c58c9 + 96589e7 commit a300314

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/libstd/rand/reseeding.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl<R: Rng, Rsdr: Reseeder<R>> Rng for ReseedingRng<R, Rsdr> {
7272
fn fill_bytes(&mut self, dest: &mut [u8]) {
7373
self.reseed_if_necessary();
7474
self.bytes_generated += dest.len();
75-
self.fill_bytes(dest)
75+
self.rng.fill_bytes(dest)
7676
}
7777
}
7878

@@ -201,4 +201,24 @@ mod test {
201201
let string2 = r.gen_ascii_str(100);
202202
assert_eq!(string1, string2);
203203
}
204+
205+
static fill_bytes_v_len: uint = 13579;
206+
#[test]
207+
fn test_rng_fill_bytes() {
208+
use rand::task_rng;
209+
let mut v = ~[0u8, .. fill_bytes_v_len];
210+
task_rng().fill_bytes(v);
211+
212+
// Sanity test: if we've gotten here, `fill_bytes` has not infinitely
213+
// recursed.
214+
assert_eq!(v.len(), fill_bytes_v_len);
215+
216+
// To test that `fill_bytes` actually did something, check that the
217+
// average of `v` is not 0.
218+
let mut sum = 0.0;
219+
for &x in v.iter() {
220+
sum += x as f64;
221+
}
222+
assert!(sum / v.len() as f64 != 0.0);
223+
}
204224
}

0 commit comments

Comments
 (0)