Description
Upfront disclaimer: I think that this issue is a relatively low priority for Chromium - I am mainly reporting it to openly share and discuss some of the more exotic requirements/scenarios that may be imposed on image decoders.
Consider the following scenario (based on what a Skia test does here):
- Main input (e.g.
Read
in Rust orSkStream
in Skia) contains a PNG image concatenated with some other data - The input is wrapped in another, secondary
Read
/SkStream
(which just forwards all calls to the main input; it is present only to accomodate the fact that thepng::Decoder::new
consumes/takes-ownership of the input). - The secondary input is passed to
png
crate for decoding the whole image (including theIEND
chunk viaReader.finish
call).
Expected behavior:
- The client code should be able to figure out where the PNG /
IEND
data ends. In the case of theCodec_end
test in Skia, this is achieved through the expectations that the main input stream is positioned right after theIEND
chunk.
Actual behavior:
std::io::BufReader
can aggressively read arbitrarily many bytes from the wrappedRead
, potentially overshooting beyond theIEND
chunk.
I realize that the Read
trait doesn't have any APIs to 1) "peek" forward, but 2) only "consume" up to the end of IEND
. And therefore I don't quite see a nice way to address this. Very silly, brainstorming-quality, probably no-go ideas:
- Maybe
Reader
can track and report how many bytes have actually been consumed viaBufRead.consume
(this should be always less then or equal to the number of bytes that have been read viaRead.read
) - Or maybe if the input is
Read + Seek
thenBufReader.seek
can be called byReader.finish
to position the wrapper reader at the end of the image.
This issue is tracked in https://crbug.com/378936780 from Chromium side. This issue seems like a relatively low priority from Chromium perspective, but (based on a test comment) it may potentially cause issues with adoption of SkPngRustCodec
in Android.