forked from rust-random/getrandom
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce OS errors are in the allowed range (rust-random#441)
Avoid the `From<NonZeroU32>` implementation in favor of a constructor that centralizes all the range checking in one place. Besides being more consistent in the range checking, this also reduces the boilerplate in callers, which makes it easier to maintain the ports to less-common operating systems.
- Loading branch information
1 parent
8933c05
commit 05cdf6f
Showing
5 changed files
with
29 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,9 @@ | ||
//! Implementation for WASI | ||
use crate::Error; | ||
use core::{ | ||
mem::MaybeUninit, | ||
num::{NonZeroU16, NonZeroU32}, | ||
}; | ||
use core::mem::MaybeUninit; | ||
use wasi::random_get; | ||
|
||
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | ||
unsafe { random_get(dest.as_mut_ptr().cast::<u8>(), dest.len()) }.map_err(|e| { | ||
// The WASI errno will always be non-zero, but we check just in case. | ||
match NonZeroU16::new(e.raw()) { | ||
Some(r) => Error::from(NonZeroU32::from(r)), | ||
None => Error::ERRNO_NOT_POSITIVE, | ||
} | ||
}) | ||
unsafe { random_get(dest.as_mut_ptr().cast::<u8>(), dest.len()) } | ||
.map_err(|e| Error::from_os_error(e.raw().into())) | ||
} |