Skip to content

Commit

Permalink
Fix an infinite loop in reading PNG files.
Browse files Browse the repository at this point in the history
  • Loading branch information
kamadak committed Jan 4, 2021
1 parent 7c11330 commit f21df24
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ impl<T> BufReadExt for T where T: io::BufRead {
fn discard_exact(&mut self, mut len: usize) -> io::Result<()> {
while len > 0 {
let consume_len = match self.fill_buf() {
Ok(buf) if buf.is_empty() =>
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof, "unexpected EOF")),
Ok(buf) => buf.len().min(len),
Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
Err(e) => return Err(e),
Expand Down Expand Up @@ -99,6 +102,16 @@ mod tests {
use std::io::Read;
use super::*;

#[test]
fn discard_exact() {
let mut buf = b"abc".as_ref();
buf.discard_exact(1).unwrap();
assert_eq!(buf, b"bc");
buf.discard_exact(2).unwrap();
assert_eq!(buf, b"");
buf.discard_exact(1).unwrap_err();
}

#[test]
fn read8_len() {
let mut reader = Cursor::new([]);
Expand Down

0 comments on commit f21df24

Please # to comment.