diff --git a/src/util_libc.rs b/src/util_libc.rs index aae4637e..f64cc312 100644 --- a/src/util_libc.rs +++ b/src/util_libc.rs @@ -67,20 +67,21 @@ pub fn sys_fill_exact( mut buf: &mut [MaybeUninit], sys_fill: impl Fn(&mut [MaybeUninit]) -> 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(())