]> git.lizzy.rs Git - rust.git/commitdiff
rand: bubble up IO errors when creating an OSRng.
authorHuon Wilson <dbau.pp+github@gmail.com>
Mon, 24 Mar 2014 13:41:43 +0000 (00:41 +1100)
committerHuon Wilson <dbau.pp+github@gmail.com>
Tue, 1 Apr 2014 09:46:09 +0000 (20:46 +1100)
src/librand/isaac.rs
src/librand/lib.rs
src/librand/os.rs

index d0dc5b90867ff42198accd6813df2c372a656619..5021675c2d81a1e3c18f538958883d1ef12496b2 100644 (file)
@@ -46,12 +46,15 @@ impl IsaacRng {
     /// Create an ISAAC random number generator with a random seed.
     pub fn new() -> IsaacRng {
         let mut rng = EMPTY;
-
+        let mut os_rng = match OSRng::new() {
+            Ok(r) => r,
+            Err(e) => fail!("IsaacRng::new: creating OSRng failed: {}", e)
+        };
         unsafe {
             let ptr = rng.rsl.as_mut_ptr();
 
             raw::mut_buf_as_slice(ptr as *mut u8, mem::size_of_val(&rng.rsl), |slice| {
-                OSRng::new().fill_bytes(slice);
+                os_rng.fill_bytes(slice);
             })
         }
 
@@ -251,12 +254,15 @@ impl Isaac64Rng {
     /// seed.
     pub fn new() -> Isaac64Rng {
         let mut rng = EMPTY_64;
-
+        let mut os_rng = match OSRng::new() {
+            Ok(r) => r,
+            Err(e) => fail!("Isaac64Rng::new: creating OSRng failed: {}", e)
+        };
         unsafe {
             let ptr = rng.rsl.as_mut_ptr();
 
             raw::mut_buf_as_slice(ptr as *mut u8, mem::size_of_val(&rng.rsl), |slice| {
-                OSRng::new().fill_bytes(slice);
+                os_rng.fill_bytes(slice);
             })
         }
 
index e405ace38670bb6a4417d0f6c75d2afd4aabe6f4..438075998f7ead010cea87f29b078038703653ca 100644 (file)
@@ -540,7 +540,10 @@ impl XorShiftRng {
     pub fn new() -> XorShiftRng {
         let mut s = [0u8, ..16];
         loop {
-            let mut r = OSRng::new();
+            let mut r = match OSRng::new() {
+                Ok(r) => r,
+                Err(e) => fail!("XorShiftRng::new: creating OSRng failed: {}", e)
+            };
             r.fill_bytes(s);
 
             if !s.iter().all(|x| *x == 0) {
index 0f4169bfe28cdfced418e35f8b01810134270700..396346b672f5398d9658f61e5299fe89ef48df6f 100644 (file)
@@ -17,7 +17,7 @@
 mod imp {
     use Rng;
     use reader::ReaderRng;
-    use std::io::File;
+    use std::io::{IoResult, File};
 
     /// A random number generator that retrieves randomness straight from
     /// the operating system. Platform sources:
@@ -35,12 +35,11 @@ pub struct OSRng {
 
     impl OSRng {
         /// Create a new `OSRng`.
-        pub fn new() -> OSRng {
-            let reader = File::open(&Path::new("/dev/urandom"));
-            let reader = reader.ok().expect("Error opening /dev/urandom");
+        pub fn new() -> IoResult<OSRng> {
+            let reader = try!(File::open(&Path::new("/dev/urandom")));
             let reader_rng = ReaderRng::new(reader);
 
-            OSRng { inner: reader_rng }
+            Ok(OSRng { inner: reader_rng })
         }
     }
 
@@ -61,6 +60,7 @@ fn fill_bytes(&mut self, v: &mut [u8]) {
 mod imp {
     use Rng;
     use std::cast;
+    use std::io::{IoResult, IoError};
     use std::libc::{c_ulong, DWORD, BYTE, LPCSTR, BOOL};
     use std::os;
     use std::rt::stack;
@@ -99,7 +99,7 @@ fn CryptGenRandom(hProv: HCRYPTPROV,
 
     impl OSRng {
         /// Create a new `OSRng`.
-        pub fn new() -> OSRng {
+        pub fn new() -> IoResult<OSRng> {
             let mut hcp = 0;
             let mut ret = unsafe {
                 CryptAcquireContextA(&mut hcp, 0 as LPCSTR, 0 as LPCSTR,
@@ -143,9 +143,10 @@ pub fn new() -> OSRng {
             }
 
             if ret == 0 {
-                fail!("couldn't create context: {}", os::last_os_error());
+                Err(IoError::last_error())
+            } else {
+                Ok(OSRng { hcryptprov: hcp })
             }
-            OSRng { hcryptprov: hcp }
         }
     }