From 96589e7264fae59a04ef47d3e5dc04fdabc9441f Mon Sep 17 00:00:00 2001 From: Robert Irelan Date: Thu, 31 Oct 2013 19:26:33 -0500 Subject: [PATCH] Fix infinite recursion in `fill_bytes()` 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. --- src/libstd/rand/reseeding.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/libstd/rand/reseeding.rs b/src/libstd/rand/reseeding.rs index 3b4919392fc29..7055b7af3ded6 100644 --- a/src/libstd/rand/reseeding.rs +++ b/src/libstd/rand/reseeding.rs @@ -72,7 +72,7 @@ impl> Rng for ReseedingRng { fn fill_bytes(&mut self, dest: &mut [u8]) { self.reseed_if_necessary(); self.bytes_generated += dest.len(); - self.fill_bytes(dest) + self.rng.fill_bytes(dest) } } @@ -201,4 +201,24 @@ mod test { let string2 = r.gen_ascii_str(100); assert_eq!(string1, string2); } + + static fill_bytes_v_len: uint = 13579; + #[test] + fn test_rng_fill_bytes() { + use rand::task_rng; + let mut v = ~[0u8, .. fill_bytes_v_len]; + task_rng().fill_bytes(v); + + // Sanity test: if we've gotten here, `fill_bytes` has not infinitely + // recursed. + assert_eq!(v.len(), fill_bytes_v_len); + + // To test that `fill_bytes` actually did something, check that the + // average of `v` is not 0. + let mut sum = 0.0; + for &x in v.iter() { + sum += x as f64; + } + assert!(sum / v.len() as f64 != 0.0); + } }