Skip to content

Commit 767e7dd

Browse files
committed
util_libc: Avoid as conversion in ssize_t-to-usize conversions.
1 parent 148255c commit 767e7dd

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/util_libc.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,22 @@ pub fn sys_fill_exact(
5555
) -> Result<(), Error> {
5656
while !buf.is_empty() {
5757
let res = sys_fill(buf);
58-
match res {
59-
res if res > 0 => buf = buf.get_mut(res as usize..).ok_or(Error::UNEXPECTED)?,
60-
-1 => {
61-
let err = last_os_error();
62-
// We should try again if the call was interrupted.
63-
if err.raw_os_error() != Some(libc::EINTR) {
64-
return Err(err);
65-
}
58+
if let Ok(res) = usize::try_from(res) {
59+
if res > 0 {
60+
buf = buf.get_mut(res..).ok_or(Error::UNEXPECTED)?;
61+
continue;
62+
}
63+
} else if res == -1 {
64+
let err = last_os_error();
65+
// We should try again if the call was interrupted.
66+
if err.raw_os_error() != Some(libc::EINTR) {
67+
return Err(err);
6668
}
67-
// Negative return codes not equal to -1 should be impossible.
68-
// EOF (ret = 0) should be impossible, as the data we are reading
69-
// should be an infinite stream of random bytes.
70-
_ => return Err(Error::UNEXPECTED),
7169
}
70+
// Negative return codes not equal to -1 should be impossible.
71+
// EOF (ret = 0) should be impossible, as the data we are reading
72+
// should be an infinite stream of random bytes.
73+
return Err(Error::UNEXPECTED);
7274
}
7375
Ok(())
7476
}

0 commit comments

Comments
 (0)