Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Bump `r-efi` dependency to v6 [#814]

### Fixed
- Read `errno` only when it is set [#810]
- Check the return value of `ProcessPrng` on Windows [#811]

[Unreleased]: https://github.com/rust-random/getrandom/compare/v0.4.1...master
[#810]: https://github.com/rust-random/getrandom/pull/810
[#811]: https://github.com/rust-random/getrandom/pull/811
[#814]: https://github.com/rust-random/getrandom/pull/814

Expand Down
17 changes: 13 additions & 4 deletions src/backends/getentropy.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//! Implementation using getentropy(2)
//!
//! When porting to a new target, ensure that its implementation follows the
//! POSIX conventions from
//! <https://pubs.opengroup.org/onlinepubs/9799919799/functions/getentropy.html>.
//!
//! Available since:
//! - macOS 10.12
//! - OpenBSD 5.6
Expand All @@ -17,11 +21,16 @@ mod utils;

#[inline]
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
for chunk in dest.chunks_mut(256) {
// https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/limits.h.html
// says `GETENTROPY_MAX` is at least 256.
const GETENTROPY_MAX: usize = 256;

for chunk in dest.chunks_mut(GETENTROPY_MAX) {
let ret = unsafe { libc::getentropy(chunk.as_mut_ptr().cast::<c_void>(), chunk.len()) };
if ret != 0 {
let errno = utils::get_errno();
return Err(Error::from_errno(errno));
match ret {
0 => continue,
-1 => return Err(Error::from_errno(utils::get_errno())),
_ => return Err(Error::UNEXPECTED),
}
}
Ok(())
Expand Down
10 changes: 7 additions & 3 deletions src/backends/vxworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
.expect("chunk size is bounded by i32::MAX");
let p: *mut libc::c_uchar = chunk.as_mut_ptr().cast();
let ret = unsafe { libc::randABytes(p, chunk_len) };
if ret != 0 {
let errno = unsafe { libc::errnoGet() };
return Err(Error::from_errno(errno));
match ret {
0 => continue,
-1 => {
let errno = unsafe { libc::errnoGet() };
return Err(Error::from_errno(errno));
}
_ => return Err(Error::UNEXPECTED),
}
}
Ok(())
Expand Down