From 1dfa17a4d12e156114626dccc243e50363d7d14d Mon Sep 17 00:00:00 2001 From: Mayuri Kini Date: Fri, 6 Dec 2024 15:53:44 -0800 Subject: [PATCH 1/3] Add graceful error handling for less than 2 bytes packet --- src/packet.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/packet.rs b/src/packet.rs index 7b083f0..f82cf6e 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 buff.len < 2 { + return Err("Buffer too short to serialize".into()); + } let opcode = Opcode::from_u16(Convert::to_u16(&buf[0..=1])?)?; match opcode { From 379f0411c43ee1e9b37abd8708271fb543c93ca2 Mon Sep 17 00:00:00 2001 From: Mayuri Kini Date: Fri, 6 Dec 2024 16:01:20 -0800 Subject: [PATCH 2/3] Fix typo --- src/packet.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/packet.rs b/src/packet.rs index f82cf6e..3e77dcd 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -58,7 +58,7 @@ pub enum Packet { impl Packet { /// Deserializes a [`u8`] slice into a [`Packet`]. pub fn deserialize(buf: &[u8]) -> Result> { - if buff.len < 2 { + if buf.len < 2 { return Err("Buffer too short to serialize".into()); } let opcode = Opcode::from_u16(Convert::to_u16(&buf[0..=1])?)?; From 49ad4c9dace72971df4e88c225011da5188303b9 Mon Sep 17 00:00:00 2001 From: Mayuri Kini Date: Fri, 6 Dec 2024 16:03:48 -0800 Subject: [PATCH 3/3] Fix typo --- src/packet.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/packet.rs b/src/packet.rs index 3e77dcd..549a301 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -58,7 +58,7 @@ pub enum Packet { impl Packet { /// Deserializes a [`u8`] slice into a [`Packet`]. pub fn deserialize(buf: &[u8]) -> Result> { - if buf.len < 2 { + if buf.len() < 2 { return Err("Buffer too short to serialize".into()); } let opcode = Opcode::from_u16(Convert::to_u16(&buf[0..=1])?)?;