Skip to content

Commit 0de3cac

Browse files
committed
Auto merge of #31825 - ollie27:win_lfs, r=alexcrichton
`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.
2 parents 6ffd7cd + b340f25 commit 0de3cac

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/libstd/sys/windows/handle.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use cmp;
1112
use io::ErrorKind;
1213
use io;
1314
use mem;
1415
use ops::Deref;
1516
use ptr;
1617
use sys::c;
1718
use sys::cvt;
19+
use u32;
1820

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

6567
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
6668
let mut read = 0;
69+
// ReadFile takes a DWORD (u32) for the length so it only supports
70+
// reading u32::MAX bytes at a time.
71+
let len = cmp::min(buf.len(), u32::MAX as usize) as c::DWORD;
6772
let res = cvt(unsafe {
68-
c::ReadFile(self.0, buf.as_ptr() as c::LPVOID,
69-
buf.len() as c::DWORD, &mut read,
70-
ptr::null_mut())
73+
c::ReadFile(self.0, buf.as_mut_ptr() as c::LPVOID,
74+
len, &mut read, ptr::null_mut())
7175
});
7276

7377
match res {
@@ -85,10 +89,12 @@ impl RawHandle {
8589

8690
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
8791
let mut amt = 0;
92+
// WriteFile takes a DWORD (u32) for the length so it only supports
93+
// writing u32::MAX bytes at a time.
94+
let len = cmp::min(buf.len(), u32::MAX as usize) as c::DWORD;
8895
try!(cvt(unsafe {
8996
c::WriteFile(self.0, buf.as_ptr() as c::LPVOID,
90-
buf.len() as c::DWORD, &mut amt,
91-
ptr::null_mut())
97+
len, &mut amt, ptr::null_mut())
9298
}));
9399
Ok(amt as usize)
94100
}

0 commit comments

Comments
 (0)