Merge pull request from ids1024/tcp-buf

tcpd: Fix behavior when reading to small buffer
This commit is contained in:
Jeremy Soller 2017-04-09 13:01:24 -06:00 committed by GitHub
commit 022467dcd9

View file

@ -660,14 +660,16 @@ impl SchemeMut for Tcpd {
Handle::Tcp(ref mut handle) => {
if ! handle.is_connected() {
return Err(Error::new(ENOTCONN));
} else if let Some((_ip, tcp)) = handle.data.pop_front() {
let mut i = 0;
while i < buf.len() && i < tcp.data.len() {
buf[i] = tcp.data[i];
i += 1;
} else if let Some((ip, mut tcp)) = handle.data.pop_front() {
let len = std::cmp::min(buf.len(), tcp.data.len());
for (i, c) in tcp.data.drain(0..len).enumerate() {
buf[i] = c;
}
if !tcp.data.is_empty() {
handle.data.push_front((ip, tcp));
}
return Ok(i);
return Ok(len);
} else if handle.flags & O_NONBLOCK == O_NONBLOCK || handle.read_closed() {
return Ok(0);
} else {