Skip to content

Avoid reading beyond the IEND chunk #531

Open
@anforowicz

Description

@anforowicz

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 or SkStream 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 the png::Decoder::new consumes/takes-ownership of the input).
  • The secondary input is passed to png crate for decoding the whole image (including the IEND chunk via Reader.finish call).

Expected behavior:

  • The client code should be able to figure out where the PNG / IEND data ends. In the case of the Codec_end test in Skia, this is achieved through the expectations that the main input stream is positioned right after the IEND chunk.

Actual behavior:

  • std::io::BufReader can aggressively read arbitrarily many bytes from the wrapped Read, potentially overshooting beyond the IEND 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 via BufRead.consume (this should be always less then or equal to the number of bytes that have been read via Read.read)
  • Or maybe if the input is Read + Seek then BufReader.seek can be called by Reader.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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions