From acc8cf6e4c7f75043449ec5090035fe370ab7abe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= <bkchr@users.noreply.github.com> Date: Wed, 22 Jun 2022 11:02:59 +0200 Subject: [PATCH] WrapperOpaque: Use `decode_all` to decode from the `Vec<u8>` (#11726) This ensures that there isn't any extra data attached that isn't used. --- substrate/frame/support/src/traits/misc.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/substrate/frame/support/src/traits/misc.rs b/substrate/frame/support/src/traits/misc.rs index 1f0ba1e769c..ccbb47909d5 100644 --- a/substrate/frame/support/src/traits/misc.rs +++ b/substrate/frame/support/src/traits/misc.rs @@ -745,7 +745,7 @@ impl<T: Encode> Encode for WrapperOpaque<T> { impl<T: Decode> Decode for WrapperOpaque<T> { fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> { - Ok(Self(T::decode(&mut &<Vec<u8>>::decode(input)?[..])?)) + Ok(Self(T::decode_all(&mut &<Vec<u8>>::decode(input)?[..])?)) } fn skip<I: Input>(input: &mut I) -> Result<(), codec::Error> { @@ -967,6 +967,10 @@ mod test { 2usize.pow(14) - 1 + 2 ); assert_eq!(<WrapperOpaque<[u8; 2usize.pow(14)]>>::max_encoded_len(), 2usize.pow(14) + 4); + + let data = 4u64; + // Ensure that we check that the `Vec<u8>` is consumed completly on decode. + assert!(WrapperOpaque::<u32>::decode(&mut &data.encode().encode()[..]).is_err()); } #[test] -- GitLab