Skip to content

Commit 7959229

Browse files
committed
[#64] Do not panic if length of UTF-8 string is invalid
1 parent c72166d commit 7959229

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/decoder/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ use serde::de::Deserialize;
4242
fn read_string<R: Read + ?Sized>(reader: &mut R, utf8_lossy: bool) -> DecoderResult<String> {
4343
let len = reader.read_i32::<LittleEndian>()?;
4444

45+
// UTF-8 String must have at least 1 byte (the last 0x00).
46+
if len < 1 {
47+
return Err(DecoderError::InvalidLength(len as usize, format!("invalid length {} for UTF-8 string", len)));
48+
}
49+
4550
let s = if utf8_lossy {
4651
let mut buf = Vec::with_capacity(len as usize - 1);
4752
reader.take(len as u64 - 1).read_to_end(&mut buf)?;

0 commit comments

Comments
 (0)