Skip to content

Commit c8d2ea0

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

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
@@ -630,7 +630,7 @@ mod serde_ {
630630

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

@@ -668,6 +668,31 @@ mod serde_ {
668668
}
669669
}
670670

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

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

0 commit comments

Comments
 (0)