Skip to content

Commit

Permalink
Restore using NoMoreImageData errors.
Browse files Browse the repository at this point in the history
#496 has incorrectly started
to return `UnexpectedEof` in a situation when when there is no
`Read`-level EOF.  In particular, this may happen when an `IDAT` of
`fdAT` chunk has been fully decoded, decompressed, and flushed, but
still doesn't contain the expected number of image pixels.  In such a
situation the old `NoMoreImageData` error was appropriate.  In a sense,
this commit is a partial revert and/or refactoring of the earlier commit
b5b0674

This commit fixes a timeout found locally by the `buf_independent`
fuzzer.  The repro case has been saved under
`fuzz/corpus/buf_independent/regressions`.
  • Loading branch information
anforowicz authored and kornelski committed Sep 20, 2024
1 parent 589a45e commit ab0e86c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
Binary file not shown.
9 changes: 4 additions & 5 deletions src/decoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,9 @@ impl<R: Read> Reader<R> {
// Read image data until we have at least one full row (but possibly more than one).
while self.data_stream.len() - self.current_start < rowlen {
if self.subframe.consumed_and_flushed {
return Err(DecodingError::IoError(ErrorKind::UnexpectedEof.into()));
return Err(DecodingError::Format(
FormatErrorInner::NoMoreImageData.into(),
));
}

// Clear the current buffer before appending more data.
Expand All @@ -716,12 +718,9 @@ impl<R: Read> Reader<R> {

match self.decoder.decode_next(&mut self.data_stream)? {
Some(Decoded::ImageData) => {}
Some(Decoded::ImageDataFlushed) => {
Some(Decoded::ImageDataFlushed) | None /* after IEND chunk */ => {
self.subframe.consumed_and_flushed = true;
}
None => {
return Err(DecodingError::IoError(ErrorKind::UnexpectedEof.into()));
}
_ => (),
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/decoder/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ pub(crate) enum FormatErrorInner {
CorruptFlateStream {
err: fdeflate::DecompressionError,
},
/// The image data chunk was too short for the expected pixel count.
NoMoreImageData,
/// Bad text encoding
BadTextEncoding(TextDecodingError),
/// fdAT shorter than 4 bytes
Expand Down Expand Up @@ -333,6 +335,10 @@ impl fmt::Display for FormatError {
UnknownInterlaceMethod(nr) => write!(fmt, "Unknown interlace method {}.", nr),
BadSubFrameBounds {} => write!(fmt, "Sub frame is out-of-bounds."),
InvalidSignature => write!(fmt, "Invalid PNG signature."),
NoMoreImageData => write!(
fmt,
"IDAT or fDAT chunk does not have enough data for image."
),
CorruptFlateStream { err } => {
write!(fmt, "Corrupt deflate stream. ")?;
write!(fmt, "{:?}", err)
Expand Down

0 comments on commit ab0e86c

Please sign in to comment.