From 9d0debe6f3e999142e1746a61db38d454dc69438 Mon Sep 17 00:00:00 2001 From: Mayuri Kini Date: Sat, 7 Dec 2024 00:45:26 -0800 Subject: [PATCH] Add graceful error handling for less than 2 bytes packet (#34) --- src/packet.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/packet.rs b/src/packet.rs index 7b083f0..549a301 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -58,6 +58,9 @@ pub enum Packet { impl Packet { /// Deserializes a [`u8`] slice into a [`Packet`]. pub fn deserialize(buf: &[u8]) -> Result> { + if buf.len() < 2 { + return Err("Buffer too short to serialize".into()); + } let opcode = Opcode::from_u16(Convert::to_u16(&buf[0..=1])?)?; match opcode {