Skip to content

Commit

Permalink
misc/multiaddr: Limit initial memory allocation in visit_seq (#1833)
Browse files Browse the repository at this point in the history
Without a limit, one can send malformed input such that seq.size_hint() returns
a very large value and crashes the program even if the following data is pretty
small.
  • Loading branch information
pawanjay176 authored Nov 14, 2020
1 parent 9c74b40 commit f7ab4f7
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion misc/multiaddr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl<'de> Deserialize<'de> for Multiaddr {
formatter.write_str("multiaddress")
}
fn visit_seq<A: de::SeqAccess<'de>>(self, mut seq: A) -> StdResult<Self::Value, A::Error> {
let mut buf: Vec<u8> = Vec::with_capacity(seq.size_hint().unwrap_or(0));
let mut buf: Vec<u8> = Vec::with_capacity(std::cmp::min(seq.size_hint().unwrap_or(0), 4096));
while let Some(e) = seq.next_element()? { buf.push(e); }
if self.is_human_readable {
let s = String::from_utf8(buf).map_err(DeserializerError::custom)?;
Expand Down

0 comments on commit f7ab4f7

Please sign in to comment.