Skip to content
Snippets Groups Projects
Commit 6bfb4921 authored by Éloïs's avatar Éloïs
Browse files

fix(kv_typed): zerocopy slice deser not handle correctly empty slice

parent de0a303a
Branches
Tags
No related merge requests found
......@@ -193,7 +193,7 @@ impl<V: ValueSliceZc, BC: BackendCol, E: EventTrait<V = V>> DbCollectionRoGetRef
) -> KvResult<Option<D>> {
let r = self.inner.read();
r.backend_col.get_ref_slice::<E::K, V, D, _>(k, |bytes| {
if bytes.is_empty() {
if bytes.len() <= V::prefix_len() {
f(&[])
} else if let Some(layout_verified) =
zerocopy::LayoutVerified::<_, [V::Elem]>::new_slice(&bytes[V::prefix_len()..])
......
......@@ -63,11 +63,15 @@ where
type Err = LayoutVerifiedErr;
fn from_bytes(bytes: &[u8]) -> Result<Self, Self::Err> {
if bytes.is_empty() {
Ok(SmallVec::new())
} else {
let layout_verified = zerocopy::LayoutVerified::<_, [T]>::new_slice(bytes)
.ok_or(LayoutVerifiedErr(stringify!(T)))?;
Ok(SmallVec::from_slice(layout_verified.into_slice()))
}
}
}
impl<T> FromBytes for Vec<T>
where
......@@ -76,6 +80,9 @@ where
type Err = LayoutVerifiedErr;
fn from_bytes(bytes: &[u8]) -> Result<Self, Self::Err> {
if bytes.is_empty() {
Ok(Vec::with_capacity(0))
} else {
let layout_verified = zerocopy::LayoutVerified::<_, [T]>::new_slice(bytes)
.ok_or(LayoutVerifiedErr(stringify!(Vec<T>)))?;
let slice = layout_verified.into_slice();
......@@ -85,6 +92,7 @@ where
Ok(vec)
}
}
}
impl<T> FromBytes for BTreeSet<T>
where
......
......@@ -175,9 +175,13 @@ where
Some(Ok((k_bytes, v_bytes))) => {
if let Some(k_layout) = zerocopy::LayoutVerified::<_, K::Ref>::new(k_bytes.as_ref())
{
if let Some(v_layout) = zerocopy::LayoutVerified::<_, [V::Elem]>::new_slice(
if v_bytes.as_ref().len() <= V::prefix_len() {
Some((self.f)(&k_layout, &[]))
} else if let Some(v_layout) =
zerocopy::LayoutVerified::<_, [V::Elem]>::new_slice(
&v_bytes.as_ref()[V::prefix_len()..],
) {
)
{
Some((self.f)(&k_layout, &v_layout))
} else {
Some(Err(KvError::DeserError(
......
......@@ -138,7 +138,7 @@ impl<'tx, V: ValueSliceZc, BC: BackendCol, E: EventTrait<V = V>> TxColRo<'tx, BC
self.col_reader
.backend_col
.get_ref_slice::<E::K, V, D, _>(k, |bytes| {
if bytes.is_empty() {
if bytes.len() <= V::prefix_len() {
f(&[])
} else if let Some(layout_verified) =
zerocopy::LayoutVerified::<_, [V::Elem]>::new_slice(&bytes[V::prefix_len()..])
......
......@@ -62,7 +62,7 @@ impl<'tx, V: ValueSliceZc, BC: BackendCol, E: EventTrait<V = V>> TxColRw<'tx, BC
self.col_reader
.backend_col
.get_ref_slice::<E::K, V, D, _>(k, |bytes| {
if bytes.is_empty() {
if bytes.len() <= V::prefix_len() {
f(&[])
} else if let Some(layout_verified) =
zerocopy::LayoutVerified::<_, [V::Elem]>::new_slice(&bytes[V::prefix_len()..])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment