Skip to content

Commit

Permalink
Tweak sys_fill_exact
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Nov 8, 2023
1 parent dd9dc6a commit 9e922d8
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/util_libc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,21 @@ pub fn sys_fill_exact(
mut buf: &mut [MaybeUninit<u8>],
sys_fill: impl Fn(&mut [MaybeUninit<u8>]) -> libc::ssize_t,
) -> Result<(), Error> {
use core::cmp::Ordering::*;

while !buf.is_empty() {
let res = sys_fill(buf);
match res.cmp(&0) {
Greater => buf = buf.get_mut(res as usize..).ok_or(Error::UNEXPECTED)?,
Less => {
match res {
res if res > 0 => buf = buf.get_mut(res as usize..).ok_or(Error::UNEXPECTED)?,
-1 => {
let err = last_os_error();
// We should try again if the call was interrupted.
if err.raw_os_error() != Some(libc::EINTR) {
return Err(err);
}
}
Equal => return Err(Error::UNEXPECTED),
// Negative return codes not equal to -1 should be impossible.
// EOF (ret = 0) should be impossible, as the data we are reading
// should be an infinite stream of random bytes.
_ => return Err(Error::UNEXPECTED),
}
}
Ok(())
Expand Down

0 comments on commit 9e922d8

Please sign in to comment.