diff options
| author | bors <bors@rust-lang.org> | 2013-11-01 05:21:24 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-11-01 05:21:24 -0700 |
| commit | a300314827899fbf505ede381767583aa6f4504e (patch) | |
| tree | e3ba5e026bcbbfa82ecabfe9823a805ad83e2b6c /src | |
| parent | 04c58c9f81c5d9b6b1847573a41c8b819893a12a (diff) | |
| parent | 96589e7264fae59a04ef47d3e5dc04fdabc9441f (diff) | |
| download | rust-a300314827899fbf505ede381767583aa6f4504e.tar.gz rust-a300314827899fbf505ede381767583aa6f4504e.zip | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/libstd/rand/reseeding.rs | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/libstd/rand/reseeding.rs b/src/libstd/rand/reseeding.rs index 3b4919392fc..7055b7af3de 100644 --- a/src/libstd/rand/reseeding.rs +++ b/src/libstd/rand/reseeding.rs @@ -72,7 +72,7 @@ impl<R: Rng, Rsdr: Reseeder<R>> Rng for ReseedingRng<R, Rsdr> { 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); + } } |
