Skip to content

Commit

Permalink
use_file: Use <File as Read>::read_buf_exact().
Browse files Browse the repository at this point in the history
Replace some unsafe code with safe code.

Eliminate the libc dependency on non-Android/Linux targets that use
use_file.
  • Loading branch information
briansmith committed Jun 18, 2024
1 parent 230e895 commit 9db3541
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ once_cell = { version = "1.19.0", default-features = false, features = ["race"]
compiler_builtins = { version = "0.1", optional = true }
core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" }

[target.'cfg(unix)'.dependencies]
[target.'cfg(any(target_os = "android", target_os = "dragonfly", target_os = "emscripten", target_os = "freebsd", target_os = "hurd", target_os = "illumos", target_os = "linux", target_os = "macos", target_os = "netbsd", target_os = "openbsd", target_os = "solaris", target_os = "vita", target_os = "vxworks", all(target_os = "horizon", target_arch = "arm")))'.dependencies]
libc = { version = "0.2.154", default-features = false }

[target.'cfg(target_os = "wasi")'.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
html_root_url = "https://docs.rs/getrandom/0.2.15"
)]
#![feature(core_io_borrowed_buf, read_buf)]
#![feature(once_cell_try)]
#![no_std]
#![warn(rust_2018_idioms, unused_lifetimes, missing_docs)]
Expand Down Expand Up @@ -236,7 +237,6 @@ pub use crate::error::Error;
// regardless of what value it returns.
cfg_if! {
if #[cfg(any(target_os = "haiku", target_os = "redox", target_os = "nto", target_os = "aix"))] {
mod util_libc;
#[path = "use_file.rs"] mod imp;
} else if #[cfg(any(
target_os = "macos",
Expand Down
25 changes: 12 additions & 13 deletions src/use_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
extern crate std;

use crate::{util_libc::sys_fill_exact, Error};
use core::{ffi::c_void, mem::MaybeUninit};
use std::{fs::File, io, os::unix::io::AsRawFd as _, sync::OnceLock};
use crate::Error;
use core::{io::BorrowedBuf, mem::MaybeUninit};
use std::{
fs::File,
io::{self, Read as _},
sync::OnceLock,
};

/// For all platforms, we use `/dev/urandom` rather than `/dev/random`.
/// For more information see the linked man pages in lib.rs.
Expand Down Expand Up @@ -34,16 +38,9 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
// after `init()` returns an `Ok` result (Ordering::Release). See
// https://github.com/rust-lang/rust/issues/126239.
static FILE: OnceLock<File> = OnceLock::new();
let file = FILE.get_or_try_init(init)?;

// TODO(MSRV feature(read_buf)): Use `std::io::Read::read_buf`
sys_fill_exact(dest, |buf| unsafe {
libc::read(
file.as_raw_fd(),
buf.as_mut_ptr().cast::<c_void>(),
buf.len(),
)
})
let mut file: &File = FILE.get_or_try_init(init)?;
file.read_buf_exact(BorrowedBuf::from(dest).unfilled())
.map_err(map_io_error)
}

#[cold]
Expand Down Expand Up @@ -85,6 +82,8 @@ fn init() -> Result<File, Error> {
// libsodium uses `libc::poll` similarly to this.
#[cfg(any(target_os = "android", target_os = "linux"))]
fn wait_until_rng_ready() -> Result<(), Error> {
use std::os::fd::AsRawFd as _;

let file = File::open("/dev/random").map_err(map_io_error)?;
let mut pfd = libc::pollfd {
fd: file.as_raw_fd(),
Expand Down

0 comments on commit 9db3541

Please sign in to comment.