Skip to content

Commit

Permalink
Add graceful error handling for less than 2 bytes packet (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
mayurikini authored Dec 7, 2024
1 parent bd92df2 commit 9d0debe
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ pub enum Packet {
impl Packet {
/// Deserializes a [`u8`] slice into a [`Packet`].
pub fn deserialize(buf: &[u8]) -> Result<Packet, Box<dyn Error>> {
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 {
Expand Down

0 comments on commit 9d0debe

Please sign in to comment.