Skip to content

Commit

Permalink
[#64] Do not panic if length of UTF-8 string is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Jun 27, 2018
1 parent c72166d commit 288e067
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/decoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ use serde::de::Deserialize;
fn read_string<R: Read + ?Sized>(reader: &mut R, utf8_lossy: bool) -> DecoderResult<String> {
let len = reader.read_i32::<LittleEndian>()?;

// UTF-8 String must have at least 1 byte (the last 0x00).
if len < 1 {
return Err(DecoderError::InvalidLength(0, "invalid length for UTF-8 string".to_owned()));
}

let s = if utf8_lossy {
let mut buf = Vec::with_capacity(len as usize - 1);
reader.take(len as u64 - 1).read_to_end(&mut buf)?;
Expand Down

0 comments on commit 288e067

Please sign in to comment.