Skip to content

Commit

Permalink
Fix a crash when receiving a zero-length packet
Browse files Browse the repository at this point in the history
  • Loading branch information
lcdr committed Mar 10, 2021
1 parent baf00f9 commit e04fc74
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[build]
target = "i686-pc-windows-msvc"
rustflags = ["-C", "link-args=/debug:none"]
8 changes: 6 additions & 2 deletions src/tcpudp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,13 @@ impl Connection {
}
self.packet.offset += n;
}
self.packet.reading_length = false;
self.packet.offset = 0;
self.packet.buffer = vec![0; u32::from_le_bytes(self.packet.length) as usize].into_boxed_slice();
let packet_length = u32::from_le_bytes(self.packet.length) as usize;
if packet_length == 0 {
return Err(io::Error::new(io::ErrorKind::WouldBlock, "packet length zero"));
}
self.packet.reading_length = false;
self.packet.buffer = vec![0; packet_length].into_boxed_slice();
}
while self.packet.offset < self.packet.buffer.len() {
let n = self.tcp.read(&mut self.packet.buffer[self.packet.offset..])?;
Expand Down

0 comments on commit e04fc74

Please sign in to comment.