Skip to content

Fix infinite recursion in fill_bytes() #10213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/libstd/rand/reseeding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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);
}
}