Skip to content

Commit 22de22f

Browse files
committed
Ensure that blobs match the length of arrays they're being deserialized into and otherwise fail.
1 parent 21ae561 commit 22de22f

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

libsql/src/value.rs

+36-2
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ mod serde_ {
631631

632632
serde::forward_to_deserialize_any! {
633633
i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
634-
bytes byte_buf unit_struct newtype_struct seq tuple tuple_struct
634+
bytes byte_buf unit_struct newtype_struct seq tuple_struct
635635
map struct identifier ignored_any
636636
}
637637

@@ -669,6 +669,31 @@ mod serde_ {
669669
}
670670
}
671671

672+
fn deserialize_tuple<V>(
673+
self,
674+
len: usize,
675+
visitor: V,
676+
) -> std::result::Result<V::Value, Self::Error>
677+
where
678+
V: Visitor<'de>,
679+
{
680+
match self.value {
681+
Value::Blob(b) => {
682+
if len != b.len() {
683+
return Err(de::Error::invalid_value(
684+
de::Unexpected::Other(&format!("{:?}", Value::Blob(b))),
685+
&"expected and provided buffers don't match in length",
686+
));
687+
}
688+
visitor.visit_seq(SeqDeserializer::new(b.into_iter()))
689+
}
690+
_ => Err(de::Error::invalid_value(
691+
de::Unexpected::Other(&format!("{:?}", self.value)),
692+
&"a blob of a certain length",
693+
)),
694+
}
695+
}
696+
672697
fn deserialize_any<V>(self, visitor: V) -> std::result::Result<V::Value, Self::Error>
673698
where
674699
V: Visitor<'de>,
@@ -815,7 +840,16 @@ mod serde_ {
815840
assert!(de::<f64>(Value::Blob(b"abc".to_vec())).is_err());
816841
assert!(de::<MyEnum>(Value::Text("C".to_string())).is_err());
817842

818-
assert_eq!(de::<[u8; 2]>(Value::Blob(b"aa".to_vec())), Ok([97, 97]));
843+
assert_eq!(
844+
de::<[u8; 0]>(Value::Blob(b"".to_vec())).unwrap(),
845+
[] as [u8; 0]
846+
);
847+
assert_eq!(
848+
de::<[u8; 2]>(Value::Blob(b"aa".to_vec())).unwrap(),
849+
[97, 97]
850+
);
851+
assert!(de::<[u8; 2]>(Value::Blob(b"aaa".to_vec())).is_err());
852+
assert!(de::<[u8; 3]>(Value::Blob(b"aa".to_vec())).is_err());
819853
}
820854
}
821855
}

0 commit comments

Comments
 (0)