From 457e215def7900f9875a876df59ed38857d2e043 Mon Sep 17 00:00:00 2001 From: Jan Bujak Date: Mon, 6 Jun 2022 11:34:47 +0000 Subject: [PATCH] Fix one-by-off in `BoundedSlice::try_from` --- frame/support/src/storage/bounded_vec.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/frame/support/src/storage/bounded_vec.rs b/frame/support/src/storage/bounded_vec.rs index f1f4330ab2960..232f9f97fc6f8 100644 --- a/frame/support/src/storage/bounded_vec.rs +++ b/frame/support/src/storage/bounded_vec.rs @@ -135,7 +135,7 @@ impl> Ord for BoundedVec { impl<'a, T, S: Get> TryFrom<&'a [T]> for BoundedSlice<'a, T, S> { type Error = (); fn try_from(t: &'a [T]) -> Result { - if t.len() < S::get() as usize { + if t.len() <= S::get() as usize { Ok(BoundedSlice(t, PhantomData)) } else { Err(()) @@ -1007,4 +1007,18 @@ pub mod test { _ => unreachable!("deserializer must raise error"), } } + + #[test] + fn bounded_vec_try_from_works() { + assert!(BoundedVec::>::try_from(vec![0]).is_ok()); + assert!(BoundedVec::>::try_from(vec![0, 1]).is_ok()); + assert!(BoundedVec::>::try_from(vec![0, 1, 2]).is_err()); + } + + #[test] + fn bounded_slice_try_from_works() { + assert!(BoundedSlice::>::try_from(&[0][..]).is_ok()); + assert!(BoundedSlice::>::try_from(&[0, 1][..]).is_ok()); + assert!(BoundedSlice::>::try_from(&[0, 1, 2][..]).is_err()); + } }