Skip to content

Commit 0e8c949

Browse files
committed
rand: bubble up IO errors when creating an OSRng.
1 parent 119289b commit 0e8c949

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

src/librand/isaac.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,15 @@ impl IsaacRng {
4646
/// Create an ISAAC random number generator with a random seed.
4747
pub fn new() -> IsaacRng {
4848
let mut rng = EMPTY;
49-
49+
let mut os_rng = match OSRng::new() {
50+
Ok(r) => r,
51+
Err(e) => fail!("IsaacRng::new: creating OSRng failed: {}", e)
52+
};
5053
unsafe {
5154
let ptr = rng.rsl.as_mut_ptr();
5255

5356
raw::mut_buf_as_slice(ptr as *mut u8, mem::size_of_val(&rng.rsl), |slice| {
54-
OSRng::new().fill_bytes(slice);
57+
os_rng.fill_bytes(slice);
5558
})
5659
}
5760

@@ -251,12 +254,15 @@ impl Isaac64Rng {
251254
/// seed.
252255
pub fn new() -> Isaac64Rng {
253256
let mut rng = EMPTY_64;
254-
257+
let mut os_rng = match OSRng::new() {
258+
Ok(r) => r,
259+
Err(e) => fail!("Isaac64Rng::new: creating OSRng failed: {}", e)
260+
};
255261
unsafe {
256262
let ptr = rng.rsl.as_mut_ptr();
257263

258264
raw::mut_buf_as_slice(ptr as *mut u8, mem::size_of_val(&rng.rsl), |slice| {
259-
OSRng::new().fill_bytes(slice);
265+
os_rng.fill_bytes(slice);
260266
})
261267
}
262268

src/librand/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,10 @@ impl XorShiftRng {
540540
pub fn new() -> XorShiftRng {
541541
let mut s = [0u8, ..16];
542542
loop {
543-
let mut r = OSRng::new();
543+
let mut r = match OSRng::new() {
544+
Ok(r) => r,
545+
Err(e) => fail!("XorShiftRng::new: creating OSRng failed: {}", e)
546+
};
544547
r.fill_bytes(s);
545548

546549
if !s.iter().all(|x| *x == 0) {

src/librand/os.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub use self::imp::OSRng;
1717
mod imp {
1818
use Rng;
1919
use reader::ReaderRng;
20-
use std::io::File;
20+
use std::io::{IoResult, File};
2121

2222
/// A random number generator that retrieves randomness straight from
2323
/// the operating system. Platform sources:
@@ -35,12 +35,11 @@ mod imp {
3535

3636
impl OSRng {
3737
/// Create a new `OSRng`.
38-
pub fn new() -> OSRng {
39-
let reader = File::open(&Path::new("/dev/urandom"));
40-
let reader = reader.ok().expect("Error opening /dev/urandom");
38+
pub fn new() -> IoResult<OSRng> {
39+
let reader = try!(File::open(&Path::new("/dev/urandom")));
4140
let reader_rng = ReaderRng::new(reader);
4241

43-
OSRng { inner: reader_rng }
42+
Ok(OSRng { inner: reader_rng })
4443
}
4544
}
4645

@@ -61,6 +60,7 @@ mod imp {
6160
mod imp {
6261
use Rng;
6362
use std::cast;
63+
use std::io::{IoResult, IoError};
6464
use std::libc::{c_ulong, DWORD, BYTE, LPCSTR, BOOL};
6565
use std::os;
6666
use std::rt::stack;
@@ -99,7 +99,7 @@ mod imp {
9999

100100
impl OSRng {
101101
/// Create a new `OSRng`.
102-
pub fn new() -> OSRng {
102+
pub fn new() -> IoResult<OSRng> {
103103
let mut hcp = 0;
104104
let mut ret = unsafe {
105105
CryptAcquireContextA(&mut hcp, 0 as LPCSTR, 0 as LPCSTR,
@@ -143,9 +143,10 @@ mod imp {
143143
}
144144

145145
if ret == 0 {
146-
fail!("couldn't create context: {}", os::last_os_error());
146+
Err(IoError::last_error())
147+
} else {
148+
Ok(OSRng { hcryptprov: hcp })
147149
}
148-
OSRng { hcryptprov: hcp }
149150
}
150151
}
151152

0 commit comments

Comments
 (0)