Skip to content

Commit

Permalink
Fix panic caused by rand_bytes()
Browse files Browse the repository at this point in the history
Fixes: rust-vmm#178

Signed-off-by: Vibha Acharya <[email protected]>
  • Loading branch information
vibharya committed Nov 16, 2022
1 parent b971c1b commit 6a1579f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# Changelog
## Unreleased

### Changed
- [[#178](https://github.com/rust-vmm/vmm-sys-util/issues/178)]: Fixed a bug in
`rand_bytes` that was triggering a panic when the number of bytes was not a
multiple of 4.

## v0.11.0

Expand Down
9 changes: 5 additions & 4 deletions src/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ fn rand_alphanumerics_impl(rand_fn: &dyn Fn() -> u32, len: usize) -> OsString {

fn rand_bytes_impl(rand_fn: &dyn Fn() -> u32, len: usize) -> Vec<u8> {
let mut buf: Vec<Vec<u8>> = Vec::new();
let mut num = len;
let mut num = if len % 4 == 0 { len / 4 } else { len / 4 + 1 };
while num > 0 {
buf.push(xor_pseudo_rng_u8_bytes(rand_fn));
num -= 4;
num -= 1;
}
buf.into_iter().flatten().take(len).collect()
}
Expand Down Expand Up @@ -156,7 +156,8 @@ mod tests {

#[test]
fn test_rand_bytes() {
let s = rand_bytes(8);
assert_eq!(8, s.len());
for i in 0..8 {
assert_eq!(i, rand_bytes(i).len());
}
}
}

0 comments on commit 6a1579f

Please sign in to comment.