Skip to content

Commit

Permalink
Fix one-by-off in BoundedSlice::try_from (paritytech#11600)
Browse files Browse the repository at this point in the history
  • Loading branch information
koute authored and ark0f committed Feb 27, 2023
1 parent 0305fe6 commit e2953f8
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion frame/support/src/storage/bounded_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl<T: Ord, Bound: Get<u32>> Ord for BoundedVec<T, Bound> {
impl<'a, T, S: Get<u32>> TryFrom<&'a [T]> for BoundedSlice<'a, T, S> {
type Error = ();
fn try_from(t: &'a [T]) -> Result<Self, Self::Error> {
if t.len() < S::get() as usize {
if t.len() <= S::get() as usize {
Ok(BoundedSlice(t, PhantomData))
} else {
Err(())
Expand Down Expand Up @@ -1007,4 +1007,18 @@ pub mod test {
_ => unreachable!("deserializer must raise error"),
}
}

#[test]
fn bounded_vec_try_from_works() {
assert!(BoundedVec::<u32, ConstU32<2>>::try_from(vec![0]).is_ok());
assert!(BoundedVec::<u32, ConstU32<2>>::try_from(vec![0, 1]).is_ok());
assert!(BoundedVec::<u32, ConstU32<2>>::try_from(vec![0, 1, 2]).is_err());
}

#[test]
fn bounded_slice_try_from_works() {
assert!(BoundedSlice::<u32, ConstU32<2>>::try_from(&[0][..]).is_ok());
assert!(BoundedSlice::<u32, ConstU32<2>>::try_from(&[0, 1][..]).is_ok());
assert!(BoundedSlice::<u32, ConstU32<2>>::try_from(&[0, 1, 2][..]).is_err());
}
}

0 comments on commit e2953f8

Please sign in to comment.