Skip to content

Commit

Permalink
Fix: oer enumerated de handled zero padding when it should not (libra…
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicceboy authored May 13, 2024
1 parent 9476bae commit 9e889ba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 18 additions & 3 deletions src/oer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,26 @@ mod tests {
A = 1,
B = 2,
}
#[derive(AsnType, Decode, Encode, Debug, Clone, Copy, PartialEq)]
#[rasn(enumerated)]
enum TestDefaults {
A,
B,
}
// Leading zeroes
decode_error!(coer, Test, &[0x00, 0x00, 0x00, 0x01, 0x01]);
decode_ok!(oer, Test, &[0x00, 0x00, 0x00, 0x01, 0x01], Test::A);
// Leading zeros not allowed for enumerated values in any case
round_trip!(oer, Test, Test::A, &[0x01]);
round_trip!(oer, TestDefaults, TestDefaults::A, &[0x00]);
// Unfortunately, below is correct since we just parse the first byte and do not chek the
// remainder in reality
decode_ok!(oer, TestDefaults, &[0x00, 0x00], TestDefaults::A);
decode_error!(oer, Test, &[0x00, 0x01]);
decode_error!(oer, Test, &[0x00, 0x81, 0x01]);
decode_ok!(oer, Test, &[0x81, 0x01], Test::A);
decode_ok!(oer, Test, &[0x01], Test::A);
// Long form when not needed
decode_error!(coer, Test, &[0b1000_0001, 0x01, 0x01]);
decode_ok!(oer, Test, &[0b1000_0001, 0x01, 0x01], Test::A);
decode_error!(coer, Test, &[0b1000_0001, 0x01]);
decode_ok!(oer, Test, &[0b1000_0001, 0x01], Test::A);
}
}
2 changes: 1 addition & 1 deletion src/oer/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ impl<'input> crate::Decoder for Decoder<'input> {
}

fn decode_enumerated<E: Enumerated>(&mut self, _: Tag) -> Result<E, Self::Error> {
let byte = self.parse_byte_and_leading_zeros()?;
let byte = self.parse_one_byte()?;
if byte < 128 {
// Short form, use value directly as unsigned integer
E::from_discriminant(isize::from(byte))
Expand Down

0 comments on commit 9e889ba

Please sign in to comment.