Skip to content

Fix building issue #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.12] - 2019-08-18
### Changed
- Update wasi dependency from v0.5 to v0.7. [#100]

[#100]: https://github.com/rust-random/getrandom/pull/100

## [0.1.11] - 2019-08-25
### Fixed
- Implement `std`-dependent traits for selected targets even if `std`
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "getrandom"
version = "0.1.11"
version = "0.1.12"
edition = "2018"
authors = ["The Rand Project Developers"]
license = "MIT OR Apache-2.0"
Expand Down Expand Up @@ -29,15 +29,15 @@ core = { version = "1.0", optional = true, package = "rustc-std-workspace-core"
libc = { version = "0.2.62", default-features = false }

[target.'cfg(target_os = "wasi")'.dependencies]
wasi = "0.5"
wasi = "0.7"

[target.wasm32-unknown-unknown.dependencies]
wasm-bindgen = { version = "0.2.29", optional = true }
stdweb = { version = "0.4.18", optional = true }

[features]
std = []
# enables dummy implementation for unsupported targets
# Enables dummy implementation for unsupported targets
dummy = []
# Unstable feature to support being a libstd dependency
rustc-dep-of-std = ["compiler_builtins", "core"]
37 changes: 22 additions & 15 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,29 @@ impl Error {
}
}

#[cfg(any(unix, target_os = "redox"))]
fn os_err_desc(errno: i32, buf: &mut [u8]) -> Option<&str> {
let buf_ptr = buf.as_mut_ptr() as *mut libc::c_char;
if unsafe { libc::strerror_r(errno, buf_ptr, buf.len()) } != 0 {
return None;
}

// Take up to trailing null byte
let n = buf.len();
let idx = buf.iter().position(|&b| b == 0).unwrap_or(n);
core::str::from_utf8(&buf[..idx]).ok()
}
cfg_if! {
if #[cfg(unix)] {
fn os_err_desc(errno: i32, buf: &mut [u8]) -> Option<&str> {
let buf_ptr = buf.as_mut_ptr() as *mut libc::c_char;
if unsafe { libc::strerror_r(errno, buf_ptr, buf.len()) } != 0 {
return None;
}

#[cfg(not(any(unix, target_os = "redox")))]
fn os_err_desc(_errno: i32, _buf: &mut [u8]) -> Option<&str> {
None
// Take up to trailing null byte
let n = buf.len();
let idx = buf.iter().position(|&b| b == 0).unwrap_or(n);
core::str::from_utf8(&buf[..idx]).ok()
}
} else if #[cfg(target_os = "wasi")] {
fn os_err_desc(errno: i32, _buf: &mut [u8]) -> Option<&str> {
core::num::NonZeroU16::new(errno as u16)
.and_then(wasi::wasi_unstable::error_str)
}
} else {
fn os_err_desc(_errno: i32, _buf: &mut [u8]) -> Option<&str> {
None
}
}
}

impl fmt::Debug for Error {
Expand Down
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,12 @@ cfg_if! {
if #[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "emscripten",
target_os = "freebsd", target_os = "haiku", target_os = "illumos",
target_os = "linux", target_os = "macos", target_os = "netbsd",
target_os = "openbsd", target_os = "redox", target_os = "solaris"))] {
target_os = "openbsd", target_os = "redox", target_os = "solaris",
target_os = "vxworks"))] {
#[allow(dead_code)]
mod util_libc;
// Keep std-only trait definitions for backwards compatiblity
mod error_impls;
} else if #[cfg(target_os = "vxworks")] {
#[allow(dead_code)]
mod util_libc;
} else if #[cfg(feature = "std")] {
mod error_impls;
}
Expand Down
13 changes: 5 additions & 8 deletions src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@

//! Implementation for WASI
use crate::Error;
use core::num::NonZeroU32;
use core::num;
use wasi::wasi_unstable::random_get;

pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
let ret = random_get(dest);
if let Some(code) = NonZeroU32::new(ret as u32) {
error!("WASI: random_get failed with return value {}", code);
Err(Error::from(code))
} else {
Ok(()) // Zero means success for WASI
}
random_get(dest).map_err(|e: num::NonZeroU16| {
// convert wasi's NonZeroU16 error into getrandom's NonZeroU32 error
num::NonZeroU32::new(e.get() as u32).unwrap().into()
})
}