Skip to content

Commit

Permalink
return BufferTooShort when Connection::recv(...) is called with an em…
Browse files Browse the repository at this point in the history
…pty buffer

Motivation:

We should return BufferTooShort if recv is called with an empty buffer to guard against missuse and possible endless loops in users code.

Modifications:

- Add extra if statement that checks for an empty buffer and return the correct error in this case
- Add unit test

Result:

Less risk for users

Fixes ##817.
  • Loading branch information
normanmaurer authored Jan 31, 2021
1 parent f236657 commit d1e704b
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,10 @@ impl Connection {
pub fn recv(&mut self, buf: &mut [u8]) -> Result<usize> {
let len = buf.len();

if len == 0 {
return Err(Error::BufferTooShort);
}

// Keep track of how many bytes we received from the client, so we
// can limit bytes sent back before address validation, to a multiple
// of this. The limit needs to be increased early on, so that if there
Expand Down Expand Up @@ -6804,6 +6808,15 @@ mod tests {
assert_eq!(pipe.server.recv(&mut buf[..written]), Ok(written));
}

#[test]
fn recv_empty_buffer() {
let mut buf = [0; 65535];
let mut pipe = testing::Pipe::default().unwrap();

assert_eq!(pipe.handshake(&mut buf), Ok(()));
assert_eq!(pipe.server.recv(&mut buf[..0]), Err(Error::BufferTooShort));
}

#[test]
/// Tests that the MAX_STREAMS frame is sent for bidirectional streams.
fn stream_limit_update_bidi() {
Expand Down

0 comments on commit d1e704b

Please sign in to comment.