From 8caa48a1e68fb2cac9522673b21ee682443467b8 Mon Sep 17 00:00:00 2001 From: Jake Probst Date: Sun, 23 Jun 2019 15:51:11 -0700 Subject: [PATCH] check that the entire packet was read --- psopacket/src/lib.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/psopacket/src/lib.rs b/psopacket/src/lib.rs index 2b4b70f..6b6734f 100644 --- a/psopacket/src/lib.rs +++ b/psopacket/src/lib.rs @@ -206,18 +206,29 @@ pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream { impl PSOPacket for #this_struct { fn from_bytes(data: &Vec) -> Result<#this_struct, PacketParseError> { let mut cur = std::io::Cursor::new(data); - cur.seek(SeekFrom::Start(2)).unwrap(); let mut b: [u8; 2] = [0; 2]; cur.read(&mut b).unwrap(); + let len = u16::from_le_bytes(b); + cur.read(&mut b).unwrap(); let cmd = u16::from_le_bytes(b); if cmd != #pkt_cmd { return Err(PacketParseError::WrongPacketCommand); } + + if len as usize != data.len() { + return Err(PacketParseError::WrongPacketSize(len, data.len())); + } - Ok(#this_struct { + let result = Ok(#this_struct { #(#from_bytes)* - }) + }); + + if cur.position() as usize != data.len() { + return Err(PacketParseError::DataStructNotLargeEnough(cur.position(), data.len())); + } + + result } fn as_bytes(&self) -> Vec { let mut buf: Vec = Vec::new();