diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2016-02-23 23:22:58 -0800 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2016-02-24 09:17:07 -0800 |
| commit | f3be73c84b546d975da8ec45671fa9e0e0ead8da (patch) | |
| tree | 7697572e8381019253b882892ccae22d421097de /src/libstd/sys/common | |
| parent | 281f9d868fee8e4f9750fc12289dc10522f587ea (diff) | |
| download | rust-f3be73c84b546d975da8ec45671fa9e0e0ead8da.tar.gz rust-f3be73c84b546d975da8ec45671fa9e0e0ead8da.zip | |
std: Cap read/write limits on Windows networking
Similar to #31825 where the read/write limits were capped for files, this implements similar limits when reading/writing networking types. On Unix this shouldn't affect anything because the write size is already a `usize`, but on Windows this will cap the read/write amounts to `i32::max_value`. cc #31841
Diffstat (limited to 'src/libstd/sys/common')
| -rw-r--r-- | src/libstd/sys/common/net.rs | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/src/libstd/sys/common/net.rs b/src/libstd/sys/common/net.rs index 7e05895b2cc..1cb9303a9fc 100644 --- a/src/libstd/sys/common/net.rs +++ b/src/libstd/sys/common/net.rs @@ -10,6 +10,7 @@ use prelude::v1::*; +use cmp; use ffi::{CStr, CString}; use fmt; use io::{self, Error, ErrorKind}; @@ -198,10 +199,11 @@ impl TcpStream { } pub fn write(&self, buf: &[u8]) -> io::Result<usize> { + let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t; let ret = try!(cvt(unsafe { c::send(*self.inner.as_inner(), buf.as_ptr() as *const c_void, - buf.len() as wrlen_t, + len, 0) })); Ok(ret as usize) @@ -358,21 +360,23 @@ impl UdpSocket { pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> { let mut storage: c::sockaddr_storage = unsafe { mem::zeroed() }; let mut addrlen = mem::size_of_val(&storage) as c::socklen_t; + let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t; let n = try!(cvt(unsafe { c::recvfrom(*self.inner.as_inner(), buf.as_mut_ptr() as *mut c_void, - buf.len() as wrlen_t, 0, + len, 0, &mut storage as *mut _ as *mut _, &mut addrlen) })); Ok((n as usize, try!(sockaddr_to_addr(&storage, addrlen as usize)))) } pub fn send_to(&self, buf: &[u8], dst: &SocketAddr) -> io::Result<usize> { + let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t; let (dstp, dstlen) = dst.into_inner(); let ret = try!(cvt(unsafe { c::sendto(*self.inner.as_inner(), - buf.as_ptr() as *const c_void, buf.len() as wrlen_t, + buf.as_ptr() as *const c_void, len, 0, dstp, dstlen) })); Ok(ret as usize) |
