diff --git a/src/hermit.rs b/src/hermit.rs index 5791ec08..21649d1f 100644 --- a/src/hermit.rs +++ b/src/hermit.rs @@ -1,5 +1,10 @@ use crate::Error; -use core::{convert::TryInto, mem::MaybeUninit, num::NonZeroU32}; +use core::{mem::MaybeUninit, num::NonZeroU32}; + +/// Minimum return value which we should get from syscalls in practice, +/// because Hermit uses positive `i32`s for error codes: +/// https://github.com/hermitcore/libhermit-rs/blob/main/src/errno.rs +const MIN_RET_CODE: isize = -(i32::MAX as isize); extern "C" { fn sys_read_entropy(buffer: *mut u8, length: usize, flags: u32) -> isize; @@ -8,17 +13,14 @@ extern "C" { pub fn getrandom_inner(mut dest: &mut [MaybeUninit]) -> Result<(), Error> { while !dest.is_empty() { let res = unsafe { sys_read_entropy(dest.as_mut_ptr() as *mut u8, dest.len(), 0) }; - if res > 0 { - dest = dest.get_mut(res as usize..).ok_or(Error::UNEXPECTED)?; + // Positive `isize`s can be safely casted to `usize` + if res > 0 && (res as usize) <= dest.len() { + dest = &mut dest[res as usize..]; } else { - // We should not get `res` equal to zero or smaller than `-i32::MAX`. - // If we get such unexpected value after all, we will return `Error::UNEXPECTED`. - let err = res - .checked_neg() - .and_then(|val| val.try_into().ok()) - .and_then(NonZeroU32::new) - .map(Into::into) - .unwrap_or(Error::UNEXPECTED); + let err = match res { + MIN_RET_CODE..=-1 => NonZeroU32::new(-res as u32).unwrap().into(), + _ => Error::UNEXPECTED, + }; return Err(err); } }