Skip to content

Don't count filter bytes for empty Adam7 passes - #737

Open
Mrmaxmeier wants to merge 1 commit into
image-rs:masterfrom
Mrmaxmeier:fix/interlaced-frame-size-empty-passes
Open

Mrmaxmeier wants to merge 1 commit into
image-rs:masterfrom
Mrmaxmeier:fix/interlaced-frame-size-empty-passes

Conversation

@Mrmaxmeier

Copy link
Copy Markdown
Contributor

Hi,

I ran into something with the buf_independent fuzzer recently. The minimal reproducer is a 1x1 interlaced
image whose zlib stream ends right after the pixel data, without a final block. Depending on how the
input is split into reads, it either decodes or fails:

whole slice       -> Err(CorruptFlateStream { err: InsufficientInput })
byte by byte      -> Err(CorruptFlateStream { err: InsufficientInput })
intermittent EOFs -> Ok

The same image without interlacing decodes fine with all three readers.

The Reader computes the number of image data bytes in a frame up front. For interlaced frames
that's a sum over the 7 Adam7 passes:

let frame_bytes = if self.info().interlaced {
let mut bytes = 0u64;
for pass in crate::adam7::PassConstants::PASSES {
bytes += self
.info()
.raw_row_length_from_width(pass.count_samples(self.subframe.width))
as u64
* pass.count_lines(self.subframe.height) as u64;
}
bytes
} else {
(self.subframe.rowlen as u64) * self.subframe.height as u64
};

Some passes can be empty, e.g. passes 2, 4 and 6 of a 1-pixel-wide image. For those,
raw_row_length_from_width(0) is 1, so the sum counts a filter byte for each of their lines. But
an empty pass has no data at all
(PNG 1.2, 2.6):

filter-type bytes are associated only with nonempty scanlines; no filter-type bytes are present in an empty pass.

and Adam7Iterator already skips them.

So for the 1x1 image the decoder expects 5 bytes instead of 2 and keeps reading the zlib stream after
the last row. If the stream ends properly, that's harmless. Here it doesn't, so the decoder runs out
of input and fails, unless the reader reports an UnexpectedEof first: then
next_raw_interlaced_row finishes with the row it already has, and the frame completes.

This PR skips empty passes when computing the frame size. Valid images decode as before, and the
reproducer decodes with all three readers.

Thanks!

The byte count of an interlaced frame (`UnfilteringBuffer::start_frame`)
adds `raw_row_length_from_width(samples) * lines` for all 7 passes. For
a pass without samples (passes 2, 4 and 6 of an image 1 pixel wide, and
so on) `raw_row_length_from_width(0)` is 1, so one filter byte is
counted per line, although `Adam7Iterator` skips such passes and the
encoder writes no rows for them. The decoder then waits for image data
that does not exist:

* a zlib stream that ends right after the pixel data without a final block
  (accepted for the non-interlaced image) is rejected with
  `CorruptFlateStream { InsufficientInput }`, and
* corrupt trailing zlib data after the pixel data is decompressed and
  reported, except when the reader hits an `UnexpectedEof` first and the
  frame is completed from the readable rows (the `buf_independent` fuzzer:
  results differ between a whole-slice reader and an intermittent-EOF
  reader).

Skip passes without samples. Valid images decode unchanged.

Assisted-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant