Skip to content

Commit a40d3de

Browse files
committed
Fixes for CI, CloudABI, 32-bit Linux, MacOS, WASM
Also new: impl From<NonZeroU32> for Error
1 parent 51303b5 commit a40d3de

File tree

7 files changed

+26
-13
lines changed

7 files changed

+26
-13
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ matrix:
7777
- cargo build --target wasm32-unknown-unknown --features=wasm-bindgen
7878
- cargo web test --target wasm32-unknown-unknown --features=stdweb
7979
- cargo build --manifest-path tests/wasm_bindgen/Cargo.toml --target wasm32-unknown-unknown
80-
- wasm-bindgen --nodejs target/wasm32-unknown-unknown/debug/rand_wasm_bindgen_test.wasm --out-dir tests/wasm_bindgen/js
80+
- wasm-bindgen --nodejs target/wasm32-unknown-unknown/debug/getrandom_wasm_bindgen_test.wasm --out-dir tests/wasm_bindgen/js
8181
- node tests/wasm_bindgen/js/index.js
8282
- wasm-pack test --node tests/wasm_bindgen
8383

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ libc = "0.2"
2020
[target.'cfg(windows)'.dependencies]
2121
winapi = { version = "0.3", features = ["minwindef", "ntsecapi", "winnt"] }
2222

23+
[target.'cfg(target_os = "cloudabi")'.dependencies]
24+
cloudabi = "0.0.3"
25+
2326
[target.'cfg(fuchsia)'.dependencies]
2427
fuchsia-cprng = "0.1"
2528

src/cloudabi.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
8-
use error::Error;
98

10-
extern "C" {
11-
fn cloudabi_sys_random_get(buf: *mut u8, len: usize) -> u16;
12-
}
9+
//! Implementation for CloudABI
10+
11+
extern crate cloudabi;
12+
13+
use core::num::NonZeroU32;
14+
use error::Error;
1315

1416
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
15-
let errno = unsafe { cloudabi_sys_random_get(dest.as_ptr(), dest.len()) };
16-
if errno == 0 {
17+
let errno = unsafe { cloudabi::random_get(dest) };
18+
if errno == cloudabi::errno::SUCCESS {
1719
Ok(())
1820
} else {
19-
Err(Error(unsafe {
20-
NonZeroU32::new_unchecked(errno as u32)
21-
}))
21+
let code = NonZeroU32::new(errno as u32).unwrap();
22+
Err(Error::from(code))
2223
}
2324
}

src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ impl fmt::Display for Error {
5050
}
5151
}
5252

53+
impl From<NonZeroU32> for Error {
54+
fn from(code: NonZeroU32) -> Self {
55+
Error(code)
56+
}
57+
}
58+
5359
#[cfg(not(target_env = "sgx"))]
5460
impl From<io::Error> for Error {
5561
fn from(err: io::Error) -> Self {

src/linux_android.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn syscall_getrandom(dest: &mut [u8]) -> Result<(), io::Error> {
3232
let ret = unsafe {
3333
libc::syscall(libc::SYS_getrandom, dest.as_mut_ptr(), dest.len(), 0)
3434
};
35-
if ret == -1 || ret != dest.len() as i64 {
35+
if ret < 0 || (ret as usize) != dest.len() {
3636
return Err(io::Error::last_os_error());
3737
}
3838
Ok(())

src/macos.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
// except according to those terms.
88

99
//! Implementation for MacOS / iOS
10+
extern crate libc;
11+
1012
use super::Error;
1113
use std::io;
14+
use self::libc::{c_int, size_t};
1215

1316
enum SecRandom {}
1417

src/wasm32_stdweb.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ thread_local!(
2727
static RNG_SOURCE: RefCell<Option<RngSource>> = RefCell::new(None);
2828
);
2929

30-
pub fn getrandom_inner(mut dest: &mut [u8]) -> Result<(), Error> {
30+
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
3131
assert_eq!(mem::size_of::<usize>(), 4);
3232

3333
RNG_SOURCE.with(|f| {
@@ -69,7 +69,7 @@ fn getrandom_init() -> Result<RngSource, Error> {
6969
}
7070
}
7171

72-
fn getrandom_fill(source: &mut RngSource, mut dest: &mut [u8]) -> Result<(), Error> {
72+
fn getrandom_fill(source: &mut RngSource, dest: &mut [u8]) -> Result<(), Error> {
7373
for chunk in dest.chunks_mut(65536) {
7474
let len = chunk.len() as u32;
7575
let ptr = chunk.as_mut_ptr() as i32;

0 commit comments

Comments
 (0)