about summary refs log tree commit diff
path: root/src/libstd/sys
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2016-02-26 22:21:59 +0000
committerbors <bors@rust-lang.org>2016-02-26 22:21:59 +0000
commit98a8a71236453a16f0048a65506f4d0bea042ca5 (patch)
tree8330302b253e4f26f4d5ec8f942bd5157e3cdaaa /src/libstd/sys
parent64b4b6d6c6ab98261ebf4349bf7064d69b117964 (diff)
parentac3cc33fee5e0f576cc9f04f7df40cd5d968be43 (diff)
downloadrust-98a8a71236453a16f0048a65506f4d0bea042ca5.tar.gz
rust-98a8a71236453a16f0048a65506f4d0bea042ca5.zip
Auto merge of #31876 - ollie27:win_fill_bytes, r=brson
CryptGenRandom takes a DWORD (u32) for the length so it only supports
writing u32::MAX bytes at a time.

Casting the length from a usize caused truncation meaning the whole
buffer was not always filled.

cc #31841

This is the same as rust-lang-nursery/rand#99. I think it's a good idea to keep the implementations in sync.

r? @alexcrichton
Diffstat (limited to 'src/libstd/sys')
-rw-r--r--src/libstd/sys/windows/rand.rs18
1 files changed, 11 insertions, 7 deletions
diff --git a/src/libstd/sys/windows/rand.rs b/src/libstd/sys/windows/rand.rs
index fdd260b6e28..10e3d45f9d5 100644
--- a/src/libstd/sys/windows/rand.rs
+++ b/src/libstd/sys/windows/rand.rs
@@ -48,13 +48,17 @@ impl Rng for OsRng {
         unsafe { mem::transmute(v) }
     }
     fn fill_bytes(&mut self, v: &mut [u8]) {
-        let ret = unsafe {
-            c::CryptGenRandom(self.hcryptprov, v.len() as c::DWORD,
-                              v.as_mut_ptr())
-        };
-        if ret == 0 {
-            panic!("couldn't generate random bytes: {}",
-                   io::Error::last_os_error());
+        // CryptGenRandom takes a DWORD (u32) for the length so we need to
+        // split up the buffer.
+        for slice in v.chunks_mut(<c::DWORD>::max_value() as usize) {
+            let ret = unsafe {
+                c::CryptGenRandom(self.hcryptprov, slice.len() as c::DWORD,
+                                  slice.as_mut_ptr())
+            };
+            if ret == 0 {
+                panic!("couldn't generate random bytes: {}",
+                       io::Error::last_os_error());
+            }
         }
     }
 }