Skip to content

Commit 1aa6ac3

Browse files
committed
Auto merge of #31858 - alexcrichton:fix-networking-cast, r=brson
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
2 parents f59fd46 + f3be73c commit 1aa6ac3

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/libstd/sys/common/net.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use prelude::v1::*;
1212

13+
use cmp;
1314
use ffi::{CStr, CString};
1415
use fmt;
1516
use io::{self, Error, ErrorKind};
@@ -198,10 +199,11 @@ impl TcpStream {
198199
}
199200

200201
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
202+
let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t;
201203
let ret = try!(cvt(unsafe {
202204
c::send(*self.inner.as_inner(),
203205
buf.as_ptr() as *const c_void,
204-
buf.len() as wrlen_t,
206+
len,
205207
0)
206208
}));
207209
Ok(ret as usize)
@@ -358,21 +360,23 @@ impl UdpSocket {
358360
pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
359361
let mut storage: c::sockaddr_storage = unsafe { mem::zeroed() };
360362
let mut addrlen = mem::size_of_val(&storage) as c::socklen_t;
363+
let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t;
361364

362365
let n = try!(cvt(unsafe {
363366
c::recvfrom(*self.inner.as_inner(),
364367
buf.as_mut_ptr() as *mut c_void,
365-
buf.len() as wrlen_t, 0,
368+
len, 0,
366369
&mut storage as *mut _ as *mut _, &mut addrlen)
367370
}));
368371
Ok((n as usize, try!(sockaddr_to_addr(&storage, addrlen as usize))))
369372
}
370373

371374
pub fn send_to(&self, buf: &[u8], dst: &SocketAddr) -> io::Result<usize> {
375+
let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t;
372376
let (dstp, dstlen) = dst.into_inner();
373377
let ret = try!(cvt(unsafe {
374378
c::sendto(*self.inner.as_inner(),
375-
buf.as_ptr() as *const c_void, buf.len() as wrlen_t,
379+
buf.as_ptr() as *const c_void, len,
376380
0, dstp, dstlen)
377381
}));
378382
Ok(ret as usize)

src/libstd/sys/windows/net.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use cmp;
1112
use io;
1213
use libc::{c_int, c_void};
1314
use mem;
@@ -131,9 +132,9 @@ impl Socket {
131132
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
132133
// On unix when a socket is shut down all further reads return 0, so we
133134
// do the same on windows to map a shut down socket to returning EOF.
135+
let len = cmp::min(buf.len(), i32::max_value() as usize) as i32;
134136
unsafe {
135-
match c::recv(self.0, buf.as_mut_ptr() as *mut c_void,
136-
buf.len() as i32, 0) {
137+
match c::recv(self.0, buf.as_mut_ptr() as *mut c_void, len, 0) {
137138
-1 if c::WSAGetLastError() == c::WSAESHUTDOWN => Ok(0),
138139
-1 => Err(last_error()),
139140
n => Ok(n as usize)

0 commit comments

Comments
 (0)