From 9341ebc222fe55684c1d4d4ba9c82f3a272a6de9 Mon Sep 17 00:00:00 2001 From: 0xalpharush <0xalpharush@protonmail.com> Date: Tue, 12 Nov 2024 16:42:40 -0600 Subject: [PATCH 1/2] add regression tests for decoding panics --- pade-macro/tests/complex.rs | 69 ++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/pade-macro/tests/complex.rs b/pade-macro/tests/complex.rs index a6359d8..de116ce 100644 --- a/pade-macro/tests/complex.rs +++ b/pade-macro/tests/complex.rs @@ -55,33 +55,33 @@ fn enums_have_correct_variant_bit_width() { ); } -#[test] -fn supports_struct_with_enum() { - #[derive(PadeEncode, PadeDecode, PartialEq, Eq, Debug)] - struct OuterStruct { - #[pade_width(3)] - x: i32, - enum1: Cases, - list: Vec, - inside: Inside, - enum2: Cases - } +#[derive(PadeEncode, PadeDecode, PartialEq, Eq, Debug)] +struct OuterStruct { + #[pade_width(3)] + x: i32, + enum1: Cases, + list: Vec, + inside: Inside, + enum2: Cases +} - #[derive(PadeEncode, PadeDecode, PartialEq, Eq, Debug)] - struct Inside { - number: u128, - another: u128, - enum1: Cases - } +#[derive(PadeEncode, PadeDecode, PartialEq, Eq, Debug)] +struct Inside { + number: u128, + another: u128, + enum1: Cases +} - #[derive(PadeEncode, PadeDecode, PartialEq, Eq, Debug)] - pub enum Cases { - Once { x: u128, y: u128 }, - Twice { a: u128, b: u128 }, - // memes - Thrice { a: u128, b: u128 } - } +#[derive(PadeEncode, PadeDecode, PartialEq, Eq, Debug)] +pub enum Cases { + Once { x: u128, y: u128 }, + Twice { a: u128, b: u128 }, + // memes + Thrice { a: u128, b: u128 } +} +#[test] +fn supports_struct_with_enum() { let outer = OuterStruct { x: 34342, enum1: Cases::Twice { a: 10, b: 2000000 }, @@ -101,6 +101,27 @@ fn supports_struct_with_enum() { assert_eq!(outer, decoded); } +#[test] +fn regression_panic_1() { + let mut bytes: &[u8] = &[ + 9, 0, 134, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 38, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 30, 132, 128, 0, 0, + 80, 0, 0, 0, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 15, 183, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 239, 96, 2, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 13, 243, 251, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 146, 250, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 123, 0, 0, 0, 0, 0, 0, 0, 255, 245, 0, 0, 0, 0, + 0, 1, 167, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + ]; + let _ = OuterStruct::pade_decode(&mut bytes, None); +} + +#[test] +fn regression_panic_2() { + let mut bytes: &[u8] = &[0]; + let _ = OuterStruct::pade_decode(&mut bytes, None); +} + #[test] fn bool_ordering_more_than_1byte() { #[derive(PadeEncode, PadeDecode, PartialEq, Eq, Debug)] From 82f79b42698f9d1de97d0b81a1f8ccb2cc199790 Mon Sep 17 00:00:00 2001 From: 0xalpharush <0xalpharush@protonmail.com> Date: Wed, 13 Nov 2024 10:09:28 -0600 Subject: [PATCH 2/2] add another regression test --- pade-macro/tests/complex.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pade-macro/tests/complex.rs b/pade-macro/tests/complex.rs index de116ce..43c6125 100644 --- a/pade-macro/tests/complex.rs +++ b/pade-macro/tests/complex.rs @@ -122,6 +122,15 @@ fn regression_panic_2() { let _ = OuterStruct::pade_decode(&mut bytes, None); } +#[test] +fn regression_panic_3() { + let mut bytes: &[u8] = &[ + 246, 0, 134, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 30, 132, 128, 0, 0, 80 + ]; + let _ = OuterStruct::pade_decode(&mut bytes, None); +} + #[test] fn bool_ordering_more_than_1byte() { #[derive(PadeEncode, PadeDecode, PartialEq, Eq, Debug)]