Skip to content

Commit

Permalink
Ensure that blobs match the length of arrays they're being deserializ…
Browse files Browse the repository at this point in the history
…ed into and otherwise fail.
  • Loading branch information
ignatz committed Oct 8, 2024
1 parent 21ae561 commit 22de22f
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions libsql/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ mod serde_ {

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

Expand Down Expand Up @@ -669,6 +669,31 @@ mod serde_ {
}
}

fn deserialize_tuple<V>(
self,
len: usize,
visitor: V,
) -> std::result::Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
match self.value {
Value::Blob(b) => {
if len != b.len() {
return Err(de::Error::invalid_value(
de::Unexpected::Other(&format!("{:?}", Value::Blob(b))),
&"expected and provided buffers don't match in length",
));
}
visitor.visit_seq(SeqDeserializer::new(b.into_iter()))
}
_ => Err(de::Error::invalid_value(
de::Unexpected::Other(&format!("{:?}", self.value)),
&"a blob of a certain length",
)),
}
}

fn deserialize_any<V>(self, visitor: V) -> std::result::Result<V::Value, Self::Error>
where
V: Visitor<'de>,
Expand Down Expand Up @@ -815,7 +840,16 @@ mod serde_ {
assert!(de::<f64>(Value::Blob(b"abc".to_vec())).is_err());
assert!(de::<MyEnum>(Value::Text("C".to_string())).is_err());

assert_eq!(de::<[u8; 2]>(Value::Blob(b"aa".to_vec())), Ok([97, 97]));
assert_eq!(
de::<[u8; 0]>(Value::Blob(b"".to_vec())).unwrap(),
[] as [u8; 0]
);
assert_eq!(
de::<[u8; 2]>(Value::Blob(b"aa".to_vec())).unwrap(),
[97, 97]
);
assert!(de::<[u8; 2]>(Value::Blob(b"aaa".to_vec())).is_err());
assert!(de::<[u8; 3]>(Value::Blob(b"aa".to_vec())).is_err());
}
}
}

0 comments on commit 22de22f

Please sign in to comment.