Skip to content

Commit

Permalink
Auto merge of #238 - epilys:master, r=mbrubeck
Browse files Browse the repository at this point in the history
Return allocation error in deserialize instead of panicking

There's no way to catch allocation errors since out of memory errors
cause an abort. Fail gracefully by returning the error instead of
panicking.

I happened upon this error when deserializing untrusted data with bincode. Bincode provides a byte limit bound but for sequences it's not possible to enforce this through serde since collection types like smallvec handle their own allocation.
  • Loading branch information
bors-servo committed Nov 8, 2020
2 parents 28fb0f4 + d1394a0 commit c7af9e2
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/lib.rs
Expand Up @@ -233,6 +233,12 @@ pub enum CollectionAllocErr {
},
}

impl fmt::Display for CollectionAllocErr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Allocation error: {:?}", self)
}
}

impl From<LayoutErr> for CollectionAllocErr {
fn from(_: LayoutErr) -> Self {
CollectionAllocErr::CapacityOverflow
Expand Down Expand Up @@ -1543,8 +1549,10 @@ where
where
B: SeqAccess<'de>,
{
use serde::de::Error;
let len = seq.size_hint().unwrap_or(0);
let mut values = SmallVec::with_capacity(len);
let mut values = SmallVec::new();
values.try_reserve(len).map_err(B::Error::custom)?;

while let Some(value) = seq.next_element()? {
values.push(value);
Expand Down

0 comments on commit c7af9e2

Please sign in to comment.