Skip to content

Commit f21df24

Browse files
committed
Fix an infinite loop in reading PNG files.
1 parent 7c11330 commit f21df24

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/util.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ impl<T> BufReadExt for T where T: io::BufRead {
5656
fn discard_exact(&mut self, mut len: usize) -> io::Result<()> {
5757
while len > 0 {
5858
let consume_len = match self.fill_buf() {
59+
Ok(buf) if buf.is_empty() =>
60+
return Err(io::Error::new(
61+
io::ErrorKind::UnexpectedEof, "unexpected EOF")),
5962
Ok(buf) => buf.len().min(len),
6063
Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
6164
Err(e) => return Err(e),
@@ -99,6 +102,16 @@ mod tests {
99102
use std::io::Read;
100103
use super::*;
101104

105+
#[test]
106+
fn discard_exact() {
107+
let mut buf = b"abc".as_ref();
108+
buf.discard_exact(1).unwrap();
109+
assert_eq!(buf, b"bc");
110+
buf.discard_exact(2).unwrap();
111+
assert_eq!(buf, b"");
112+
buf.discard_exact(1).unwrap_err();
113+
}
114+
102115
#[test]
103116
fn read8_len() {
104117
let mut reader = Cursor::new([]);

0 commit comments

Comments
 (0)