Skip to content

Commit

Permalink
der(clippy): fix uint <-> int conversion safety
Browse files Browse the repository at this point in the history
  • Loading branch information
baloo committed Sep 24, 2024
1 parent 535984e commit 38302f9
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions der/src/asn1/integer/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ macro_rules! impl_encoding_traits {
let bytes = reader.read_into(&mut buf[..max_length])?;

let result = if is_highest_bit_set(bytes) {
<$uint>::from_be_bytes(decode_to_array(bytes)?) as $int
<$int>::try_from(<$uint>::from_be_bytes(decode_to_array(bytes)?))?
} else {
Self::from_be_bytes(uint::decode_to_array(bytes)?)
};
Expand All @@ -48,15 +48,15 @@ macro_rules! impl_encoding_traits {
impl EncodeValue for $int {
fn value_len(&self) -> Result<Length> {
if *self < 0 {
negative_encoded_len(&(*self as $uint).to_be_bytes())
negative_encoded_len(&(<$uint>::try_from(*self)?).to_be_bytes())
} else {
uint::encoded_len(&self.to_be_bytes())
}
}

fn encode_value(&self, writer: &mut impl Writer) -> Result<()> {
if *self < 0 {
encode_bytes(writer, &(*self as $uint).to_be_bytes())
encode_bytes(writer, &(<$uint>::try_from(*self)?).to_be_bytes())
} else {
uint::encode_bytes(writer, &self.to_be_bytes())
}
Expand Down

0 comments on commit 38302f9

Please sign in to comment.