]> git.lizzy.rs Git - rust.git/commitdiff
Fix reading/writing 4 GiB or larger files on Windows 64-bit
authorOliver Middleton <olliemail27@gmail.com>
Tue, 23 Feb 2016 00:04:24 +0000 (00:04 +0000)
committerOliver Middleton <olliemail27@gmail.com>
Tue, 23 Feb 2016 00:51:18 +0000 (00:51 +0000)
`ReadFile` and `WriteFile` take a DWORD (u32) for the length argument
which was erroneously cast from a usize causing truncation. This meant
methods like `write_all` and `read_exact` would unexpectedly fail if
given a buffer 4 GiB or larger.

We can instead just ask for `u32::MAX` bytes if the given buffer is too
big.

src/libstd/sys/windows/handle.rs

index cb41b05daaea26c48afe08bea8f5bb343db0f9d4..47676a927f65877ab02e386ace0fc98e974b46ea 100644 (file)
@@ -8,6 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+use cmp;
 use io::ErrorKind;
 use io;
 use mem;
@@ -15,6 +16,7 @@
 use ptr;
 use sys::c;
 use sys::cvt;
+use u32;
 
 /// An owned container for `HANDLE` object, closing them on Drop.
 ///
@@ -64,10 +66,12 @@ pub fn raw(&self) -> c::HANDLE { self.0 }
 
     pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
         let mut read = 0;
+        // ReadFile takes a DWORD (u32) for the length so it only supports
+        // reading u32::MAX bytes at a time.
+        let len = cmp::min(buf.len(), u32::MAX as usize) as c::DWORD;
         let res = cvt(unsafe {
-            c::ReadFile(self.0, buf.as_ptr() as c::LPVOID,
-                           buf.len() as c::DWORD, &mut read,
-                           ptr::null_mut())
+            c::ReadFile(self.0, buf.as_mut_ptr() as c::LPVOID,
+                        len, &mut read, ptr::null_mut())
         });
 
         match res {
@@ -85,10 +89,12 @@ pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
 
     pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
         let mut amt = 0;
+        // WriteFile takes a DWORD (u32) for the length so it only supports
+        // writing u32::MAX bytes at a time.
+        let len = cmp::min(buf.len(), u32::MAX as usize) as c::DWORD;
         try!(cvt(unsafe {
             c::WriteFile(self.0, buf.as_ptr() as c::LPVOID,
-                            buf.len() as c::DWORD, &mut amt,
-                            ptr::null_mut())
+                         len, &mut amt, ptr::null_mut())
         }));
         Ok(amt as usize)
     }