From e695897fe8d4ee1a08511a1839a647fa0be1743b Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Fri, 7 Apr 2017 18:36:09 -0700 Subject: [PATCH] tcpd: Fix behavior when reading to small buffer --- schemes/tcpd/src/main.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/schemes/tcpd/src/main.rs b/schemes/tcpd/src/main.rs index af18237..6bd53bc 100644 --- a/schemes/tcpd/src/main.rs +++ b/schemes/tcpd/src/main.rs @@ -574,12 +574,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() { + } else if let Some((ip, mut tcp)) = handle.data.pop_front() { let mut i = 0; - while i < buf.len() && i < tcp.data.len() { - buf[i] = tcp.data[i]; + let mut len = std::cmp::min(buf.len(), tcp.data.len()); + for c in tcp.data.drain(0..len) { + buf[i] = c; i += 1; } + if !tcp.data.is_empty() { + handle.data.push_front((ip, tcp)); + } return Ok(i); } else if handle.flags & O_NONBLOCK == O_NONBLOCK || handle.read_closed() {