Skip to content

Commit

Permalink
Auto merge of #31825 - ollie27:win_lfs, r=alexcrichton
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
bors committed Feb 23, 2016
2 parents 6ffd7cd + b340f25 commit 0de3cac
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/libstd/sys/windows/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use cmp;
use io::ErrorKind;
use io;
use mem;
use ops::Deref;
use ptr;
use sys::c;
use sys::cvt;
use u32;

/// An owned container for `HANDLE` object, closing them on Drop.
///
Expand Down Expand Up @@ -64,10 +66,12 @@ impl RawHandle {

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 {
Expand All @@ -85,10 +89,12 @@ impl RawHandle {

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)
}
Expand Down

0 comments on commit 0de3cac

Please sign in to comment.