From 39b1a84c55e3135ae60d4ba815c06aa868e08d9e Mon Sep 17 00:00:00 2001 From: Tamipes Date: Wed, 17 Dec 2025 18:44:20 +0100 Subject: [PATCH] fix: PacketLengthInvalid did not correctly return at any time this was due to the value being coerced to usize and then panicing in debug with integer subtract underflow --- src/packets/mod.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/packets/mod.rs b/src/packets/mod.rs index e8dd26b..28c6a04 100644 --- a/src/packets/mod.rs +++ b/src/packets/mod.rs @@ -74,15 +74,19 @@ impl Packet { return Err(ParseError::WeirdID.into()); } + let lenght_value = length.get_int() as usize; + let length_value_length = id.get_data().len(); + if lenght_value < length_value_length { + return Err(ParseError::PacketLengthInvalid(length.get_int()).into()); + } + let data_size = lenght_value - length_value_length; + // TODO: investigate this, becuase it is just a hunch // but if it is too big, the vec![] macro panics - let data_size = length.get_int() as usize - id.get_data().len(); if data_size > u16::MAX.into() { return Err(ParseError::LengthIsTooBig(length.get_int()).into()); } - if data_size < 0 { - return Err(ParseError::PacketLengthInvalid(length.get_int()).into()); - } + // TODO: this is a bandaid fix; the above checks *should* make sure the // next line does not run into "capacity overflow", but it doesn't work let mut data: Vec = match std::panic::catch_unwind(|| vec![0; data_size]) {