From a9e2d1cbf751b197bed3b0c9b64c1b28e157a2b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Wed, 8 Nov 2023 09:40:47 +0300 Subject: [PATCH] Import TryInto --- src/hermit.rs | 5 ++--- src/util_libc.rs | 10 +++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/hermit.rs b/src/hermit.rs index 804cc2697..5c609ac82 100644 --- a/src/hermit.rs +++ b/src/hermit.rs @@ -1,5 +1,5 @@ use crate::Error; -use core::{cmp::min, mem::MaybeUninit, num::NonZeroU32}; +use core::{convert::TryInto, mem::MaybeUninit, num::NonZeroU32}; extern "C" { fn sys_read_entropy(buffer: *mut u8, length: usize, flags: u32) -> isize; @@ -17,8 +17,7 @@ pub fn getrandom_inner(mut dest: &mut [MaybeUninit]) -> Result<(), Error> { .unwrap_or(Error::UNEXPECTED); return Err(err); } - let len = min(res as usize, dest.len()); - dest = dest.get_mut(len..).ok_or(Error::UNEXPECTED)?; + dest = dest.get_mut(res as usize..).ok_or(Error::UNEXPECTED)?; } Ok(()) } diff --git a/src/util_libc.rs b/src/util_libc.rs index 67151686c..3d5a9c2a7 100644 --- a/src/util_libc.rs +++ b/src/util_libc.rs @@ -8,7 +8,6 @@ #![allow(dead_code)] use crate::Error; use core::{ - cmp::min, mem::MaybeUninit, num::NonZeroU32, ptr::NonNull, @@ -70,17 +69,18 @@ pub fn sys_fill_exact( ) -> Result<(), Error> { while !buf.is_empty() { let res = sys_fill(buf); - if res < 0 { + if res > 0 { + buf = buf.get_mut(res as usize..).ok_or(Error::UNEXPECTED)?; + } else if res < 0 { 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); } } else { - // We don't check for EOF (ret = 0) as the data we are reading + // EOF (ret = 0) should be impossible, as the data we are reading // should be an infinite stream of random bytes. - let len = min(res as usize, buf.len()); - buf = buf.get_mut(len..).ok_or(Error::UNEXPECTED)?; + return Err(Error::UNEXPECTED); } } Ok(())