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
This commit is contained in:
parent
822330ef87
commit
39b1a84c55
1 changed files with 8 additions and 4 deletions
|
|
@ -74,15 +74,19 @@ impl Packet {
|
||||||
return Err(ParseError::WeirdID.into());
|
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
|
// TODO: investigate this, becuase it is just a hunch
|
||||||
// but if it is too big, the vec![] macro panics
|
// 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() {
|
if data_size > u16::MAX.into() {
|
||||||
return Err(ParseError::LengthIsTooBig(length.get_int()).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
|
// 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
|
// next line does not run into "capacity overflow", but it doesn't work
|
||||||
let mut data: Vec<u8> = match std::panic::catch_unwind(|| vec![0; data_size]) {
|
let mut data: Vec<u8> = match std::panic::catch_unwind(|| vec![0; data_size]) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue