Skip to content

Commit a9e2d1c

Browse files
committed
Import TryInto
1 parent 5945455 commit a9e2d1c

2 files changed

Lines changed: 7 additions & 8 deletions

File tree

src/hermit.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::Error;
2-
use core::{cmp::min, mem::MaybeUninit, num::NonZeroU32};
2+
use core::{convert::TryInto, mem::MaybeUninit, num::NonZeroU32};
33

44
extern "C" {
55
fn sys_read_entropy(buffer: *mut u8, length: usize, flags: u32) -> isize;
@@ -17,8 +17,7 @@ pub fn getrandom_inner(mut dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
1717
.unwrap_or(Error::UNEXPECTED);
1818
return Err(err);
1919
}
20-
let len = min(res as usize, dest.len());
21-
dest = dest.get_mut(len..).ok_or(Error::UNEXPECTED)?;
20+
dest = dest.get_mut(res as usize..).ok_or(Error::UNEXPECTED)?;
2221
}
2322
Ok(())
2423
}

src/util_libc.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#![allow(dead_code)]
99
use crate::Error;
1010
use core::{
11-
cmp::min,
1211
mem::MaybeUninit,
1312
num::NonZeroU32,
1413
ptr::NonNull,
@@ -70,17 +69,18 @@ pub fn sys_fill_exact(
7069
) -> Result<(), Error> {
7170
while !buf.is_empty() {
7271
let res = sys_fill(buf);
73-
if res < 0 {
72+
if res > 0 {
73+
buf = buf.get_mut(res as usize..).ok_or(Error::UNEXPECTED)?;
74+
} else if res < 0 {
7475
let err = last_os_error();
7576
// We should try again if the call was interrupted.
7677
if err.raw_os_error() != Some(libc::EINTR) {
7778
return Err(err);
7879
}
7980
} else {
80-
// We don't check for EOF (ret = 0) as the data we are reading
81+
// EOF (ret = 0) should be impossible, as the data we are reading
8182
// should be an infinite stream of random bytes.
82-
let len = min(res as usize, buf.len());
83-
buf = buf.get_mut(len..).ok_or(Error::UNEXPECTED)?;
83+
return Err(Error::UNEXPECTED);
8484
}
8585
}
8686
Ok(())

0 commit comments

Comments
 (0)