image-rs / image-png

PNG decoding and encoding library in pure Rust
https://docs.rs/png
Apache License 2.0
359 stars 142 forks source link

Corrupt auxillary chunks should be ignored, not treated as fatal errors #525

Open fintelia opened 5 days ago

fintelia commented 5 days ago

The PNG spec says:

Unexpected values in fields of known chunks (for example, an unexpected compression method in the IHDR chunk) shall be checked for and treated as errors. However, it is recommended that unexpected field values be treated as fatal errors only in critical chunks. An unexpected value in an ancillary chunk can be handled by ignoring the whole chunk as though it were an unknown chunk type.

Right now, parse_chunk instead treats any error in any known chunk as fatal and sets the decoding state to None.

anforowicz commented 3 days ago

Good catch - thanks for filing the bug.

A few thoughts that I hope are helpful:

kornelski commented 3 days ago

APNG chunks started out as a non-standard hack, and it was long too late to fix their names when they got into the spec.

APNG chunks will need special case handling. They could simply be treated as mandatory, or mandatory after decoding past the first frame (iEND).

fintelia commented 3 days ago

For reference, this is how the spec recommends handling APNG errors:

APNG is designed to allow incremental display of frames before the entire datastream has been read. This implies that some errors may not be detected until partway through the animation. It is strongly recommended that when any error is encountered decoders should discard all subsequent frames, stop the animation, and revert to displaying the static image. A decoder which detects an error before the animation has started should display the static image. An error message may be displayed to the user if appropriate.

But really, the top priority should be making sure that adding support for new metadata chunks doesn't cause breaking changes for users due to previously readable PNGs now raising errors about the new chunk being corrupt.