From 38f26bb5221a034e691e426121766cccc3523401 Mon Sep 17 00:00:00 2001 From: Wolfgang Welz Date: Wed, 17 Jul 2024 10:49:50 +0200 Subject: [PATCH 1/9] implement decode_full --- crates/rlp/src/decode.rs | 18 ++++++++++++++++++ crates/rlp/src/error.rs | 3 +++ crates/rlp/src/lib.rs | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/rlp/src/decode.rs b/crates/rlp/src/decode.rs index 6988ec7..7b33ef9 100644 --- a/crates/rlp/src/decode.rs +++ b/crates/rlp/src/decode.rs @@ -176,6 +176,24 @@ mod std_impl { } } +/// Decodes the entire input, ensuring no trailing bytes remain. +/// +/// # Errors +/// +/// Returns an error if the encoding is invalid or if data remains after decoding the RLP item. +#[inline] +pub fn decode_full(bytes: impl AsRef<[u8]>) -> Result { + let mut buf = bytes.as_ref(); + let out = T::decode(&mut buf)?; + + // check if there are any remaining bytes after decoding + if !buf.is_empty() { + return Err(Error::TrailingBytes); + } + + Ok(out) +} + /// Left-pads a slice to a statically known size array. /// /// # Errors diff --git a/crates/rlp/src/error.rs b/crates/rlp/src/error.rs index 5eed183..763ffe1 100644 --- a/crates/rlp/src/error.rs +++ b/crates/rlp/src/error.rs @@ -12,6 +12,8 @@ pub enum Error { LeadingZero, /// Overran input while decoding. InputTooShort, + /// Additional trailing bytes found after decoding. + TrailingBytes, /// Expected single byte, but got invalid value. NonCanonicalSingleByte, /// Expected size, but got invalid value. @@ -42,6 +44,7 @@ impl fmt::Display for Error { Self::Overflow => f.write_str("overflow"), Self::LeadingZero => f.write_str("leading zero"), Self::InputTooShort => f.write_str("input too short"), + Self::TrailingBytes => f.write_str("trailing bytes"), Self::NonCanonicalSingleByte => f.write_str("non-canonical single byte"), Self::NonCanonicalSize => f.write_str("non-canonical size"), Self::UnexpectedLength => f.write_str("unexpected length"), diff --git a/crates/rlp/src/lib.rs b/crates/rlp/src/lib.rs index fd90349..6364123 100644 --- a/crates/rlp/src/lib.rs +++ b/crates/rlp/src/lib.rs @@ -19,7 +19,7 @@ extern crate alloc; mod decode; -pub use decode::{Decodable, Rlp}; +pub use decode::{decode_full, Decodable, Rlp}; mod error; pub use error::{Error, Result}; From 75501ced95b94410613d886899b3444ea2176c06 Mon Sep 17 00:00:00 2001 From: Wolfgang Welz Date: Wed, 17 Jul 2024 11:13:44 +0200 Subject: [PATCH 2/9] add tests --- crates/rlp/src/decode.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/rlp/src/decode.rs b/crates/rlp/src/decode.rs index 7b33ef9..4c04001 100644 --- a/crates/rlp/src/decode.rs +++ b/crates/rlp/src/decode.rs @@ -229,7 +229,7 @@ fn slice_to_array(slice: &[u8]) -> Result<[u8; N]> { #[cfg(test)] mod tests { use super::*; - use crate::Encodable; + use crate::{encode, Encodable}; use core::fmt::Debug; use hex_literal::hex; @@ -362,4 +362,18 @@ mod tests { check_decode::([(Err(Error::InputTooShort), &hex!("82")[..])]); check_decode::([(Err(Error::InputTooShort), &hex!("82")[..])]); } + + #[test] + fn rlp_full() { + fn check_decode_full(input: T) { + let encoded = encode(&input); + assert_eq!(decode_full::(&encoded), Ok(input)); + assert_eq!(decode_full::([encoded, vec![0x00]].concat()), Err(Error::TrailingBytes)); + } + + check_decode_full::("".into()); + check_decode_full::("test1234".into()); + check_decode_full::>(vec![]); + check_decode_full::>(vec![0; 4]); + } } From 22fc808c4708876f8e552b292fbe2d37c040f683 Mon Sep 17 00:00:00 2001 From: Wolfgang Welz Date: Wed, 17 Jul 2024 11:39:24 +0200 Subject: [PATCH 3/9] add CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d8c2ca..db84c6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- `decode_full` function + ## [0.3.7] - 2024-06-29 ### Fixed From b86364e0b36959a41824341ca1236eb256e58737 Mon Sep 17 00:00:00 2001 From: Wolfgang Welz Date: Wed, 17 Jul 2024 11:43:22 +0200 Subject: [PATCH 4/9] add PR number --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db84c6c..5a84956 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- `decode_full` function +- `decode_full` function ([#23]) + +[#23]: https://github.com/alloy-rs/rlp/pull/23 ## [0.3.7] - 2024-06-29 From 25d265026a661faec2150d58faf215ddd3a960e5 Mon Sep 17 00:00:00 2001 From: Wolfgang Welz Date: Mon, 5 Aug 2024 16:11:15 +0200 Subject: [PATCH 5/9] Update crates/rlp/src/decode.rs Co-authored-by: Matthias Seitz --- crates/rlp/src/decode.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/rlp/src/decode.rs b/crates/rlp/src/decode.rs index 4c04001..a9bac57 100644 --- a/crates/rlp/src/decode.rs +++ b/crates/rlp/src/decode.rs @@ -182,7 +182,7 @@ mod std_impl { /// /// Returns an error if the encoding is invalid or if data remains after decoding the RLP item. #[inline] -pub fn decode_full(bytes: impl AsRef<[u8]>) -> Result { +pub fn decode_exact(bytes: impl AsRef<[u8]>) -> Result { let mut buf = bytes.as_ref(); let out = T::decode(&mut buf)?; From d1987bfb1ea97145620a03697c70295b43c3c93b Mon Sep 17 00:00:00 2001 From: Wolfgang Welz Date: Mon, 5 Aug 2024 16:12:50 +0200 Subject: [PATCH 6/9] revert CHANGELOG changes --- CHANGELOG.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a84956..2d8c2ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,12 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -### Added - -- `decode_full` function ([#23]) - -[#23]: https://github.com/alloy-rs/rlp/pull/23 - ## [0.3.7] - 2024-06-29 ### Fixed From 3f77b9831496af677c451d268b59e5ceb23b5804 Mon Sep 17 00:00:00 2001 From: Wolfgang Welz Date: Mon, 5 Aug 2024 17:26:18 +0200 Subject: [PATCH 7/9] fix imports --- crates/rlp/src/decode.rs | 17 ++++++++++------- crates/rlp/src/lib.rs | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/crates/rlp/src/decode.rs b/crates/rlp/src/decode.rs index a9bac57..3a5abe8 100644 --- a/crates/rlp/src/decode.rs +++ b/crates/rlp/src/decode.rs @@ -365,15 +365,18 @@ mod tests { #[test] fn rlp_full() { - fn check_decode_full(input: T) { + fn check_decode_exact(input: T) { let encoded = encode(&input); - assert_eq!(decode_full::(&encoded), Ok(input)); - assert_eq!(decode_full::([encoded, vec![0x00]].concat()), Err(Error::TrailingBytes)); + assert_eq!(decode_exact::(&encoded), Ok(input)); + assert_eq!( + decode_exact::([encoded, vec![0x00]].concat()), + Err(Error::TrailingBytes) + ); } - check_decode_full::("".into()); - check_decode_full::("test1234".into()); - check_decode_full::>(vec![]); - check_decode_full::>(vec![0; 4]); + check_decode_exact::("".into()); + check_decode_exact::("test1234".into()); + check_decode_exact::>(vec![]); + check_decode_exact::>(vec![0; 4]); } } diff --git a/crates/rlp/src/lib.rs b/crates/rlp/src/lib.rs index 6364123..bcf586c 100644 --- a/crates/rlp/src/lib.rs +++ b/crates/rlp/src/lib.rs @@ -19,7 +19,7 @@ extern crate alloc; mod decode; -pub use decode::{decode_full, Decodable, Rlp}; +pub use decode::{decode_exact, Decodable, Rlp}; mod error; pub use error::{Error, Result}; From 9a21052c63c63acd0f8cb99dda14466664ff3ff2 Mon Sep 17 00:00:00 2001 From: Wolfgang Welz Date: Mon, 5 Aug 2024 22:46:03 +0200 Subject: [PATCH 8/9] add CHANGELOG --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d13bee..6f43e60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- `decode_exact` function ([#23]) + +### Fixed + - Fix `RlpEncodableWrapper` doc ([#22]) [#22]: https://github.com/alloy-rs/rlp/pull/22 +[#23]: https://github.com/alloy-rs/rlp/pull/23 ## [0.3.7] - 2024-06-29 From 051ebf73e1e9157a6f30f913ea9eb1104384794c Mon Sep 17 00:00:00 2001 From: Wolfgang Welz Date: Mon, 5 Aug 2024 22:53:38 +0200 Subject: [PATCH 9/9] remove TrailingBytes to avoid breaking change --- crates/rlp/src/decode.rs | 5 +++-- crates/rlp/src/error.rs | 3 --- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/crates/rlp/src/decode.rs b/crates/rlp/src/decode.rs index 129a015..63714a8 100644 --- a/crates/rlp/src/decode.rs +++ b/crates/rlp/src/decode.rs @@ -188,7 +188,8 @@ pub fn decode_exact(bytes: impl AsRef<[u8]>) -> Result { // check if there are any remaining bytes after decoding if !buf.is_empty() { - return Err(Error::TrailingBytes); + // TODO: introduce a new variant TrailingBytes to better distinguish this error + return Err(Error::UnexpectedLength); } Ok(out) @@ -382,7 +383,7 @@ mod tests { assert_eq!(decode_exact::(&encoded), Ok(input)); assert_eq!( decode_exact::([encoded, vec![0x00]].concat()), - Err(Error::TrailingBytes) + Err(Error::UnexpectedLength) ); } diff --git a/crates/rlp/src/error.rs b/crates/rlp/src/error.rs index 763ffe1..5eed183 100644 --- a/crates/rlp/src/error.rs +++ b/crates/rlp/src/error.rs @@ -12,8 +12,6 @@ pub enum Error { LeadingZero, /// Overran input while decoding. InputTooShort, - /// Additional trailing bytes found after decoding. - TrailingBytes, /// Expected single byte, but got invalid value. NonCanonicalSingleByte, /// Expected size, but got invalid value. @@ -44,7 +42,6 @@ impl fmt::Display for Error { Self::Overflow => f.write_str("overflow"), Self::LeadingZero => f.write_str("leading zero"), Self::InputTooShort => f.write_str("input too short"), - Self::TrailingBytes => f.write_str("trailing bytes"), Self::NonCanonicalSingleByte => f.write_str("non-canonical single byte"), Self::NonCanonicalSize => f.write_str("non-canonical size"), Self::UnexpectedLength => f.write_str("unexpected length"),