From bc3702ec428bf4a170b9a00da20d24c58405b2b0 Mon Sep 17 00:00:00 2001 From: Dan Cline <6798349+Rjected@users.noreply.github.com> Date: Thu, 1 Dec 2022 21:27:33 -0500 Subject: [PATCH] chore(rlp): add comment on `0x80` decoding (#312) * add comment on 0x80 decoding * decode 0x80 into zero explicitly * add explicit 0x80 decoding for ethereum types --- crates/common/rlp/src/decode.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/common/rlp/src/decode.rs b/crates/common/rlp/src/decode.rs index 68e030228f..ca96be2f02 100644 --- a/crates/common/rlp/src/decode.rs +++ b/crates/common/rlp/src/decode.rs @@ -185,6 +185,12 @@ macro_rules! decode_integer { if buf.remaining() < h.payload_length { return Err(DecodeError::InputTooShort) } + // In the case of 0x80, the Header will be decoded, leaving h.payload_length to be + // zero. + // 0x80 is the canonical encoding of 0, so we return 0 here. + if h.payload_length == 0 { + return Ok(<$t>::from(0u8)) + } let v = <$t>::from_be_bytes( static_left_pad(&buf[..h.payload_length]).ok_or(DecodeError::LeadingZero)?, ); @@ -252,6 +258,12 @@ mod ethereum_types_support { if buf.remaining() < h.payload_length { return Err(DecodeError::InputTooShort) } + // In the case of 0x80, the Header will be decoded, leaving h.payload_length to + // be zero. + // 0x80 is the canonical encoding of 0, so we return 0 here. + if h.payload_length == 0 { + return Ok(<$t>::from(0u8)) + } let n = <$t>::from_big_endian( &static_left_pad::<$n_bytes>(&buf[..h.payload_length]) .ok_or(DecodeError::LeadingZero)?,