Skip to content

Relaxed handle of corrupt png files#2589

Merged
JimBobSquarePants merged 8 commits into
SixLabors:mainfrom
svenclaesson:better_handle_corrupt_png
Nov 29, 2023
Merged

Relaxed handle of corrupt png files#2589
JimBobSquarePants merged 8 commits into
SixLabors:mainfrom
svenclaesson:better_handle_corrupt_png

Conversation

@svenclaesson
Copy link
Copy Markdown
Contributor

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

Description

WIP support for handing truncated png files

Implementing an option for PngDecoder, IgnoreCrcErrors
Tried it a bit but got stuck by the fact that IDAT and IEND is considered critical chunks and found no way of ignoring that fact if the file is truncated, IDAT is cut off and IEND is missing.

This file is truncated and i'm trying to get a partial result converted as libpng (chrome) and WIC can do.
280752616-57c5e277-a472-4f13-98f0-a2a238c0fe28

Maybe you have some pointers for me

See #2570

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Nov 24, 2023

CLA assistant check
All committers have signed the CLA.

@JimBobSquarePants
Copy link
Copy Markdown
Member

JimBobSquarePants commented Nov 29, 2023

@svenclaesson could you please explicitly invite me to contribute to your fork. I need to add the test image from above and due to a quirk in Git LFS permissions with GitHub I cannot push any images to your fork without being invited.

@dlemstra is there a way to get the Magick decoder to break on adaptive filter errors? When I try to use your decoder as a reference for this file the following exception is thrown.

ImageMagick.MagickCoderErrorException : bad adaptive filter value `' @ error/png.c/MagickPNGErrorHandler/1716

@JimBobSquarePants JimBobSquarePants added this to the v3.1.0 milestone Nov 29, 2023
@JimBobSquarePants JimBobSquarePants changed the title WIP relaxed handle of corrupt png files Relaxed handle of corrupt png files Nov 29, 2023
@JimBobSquarePants
Copy link
Copy Markdown
Member

Thanks for the invite. @svenclaesson

I've now reworked the decoder to allow granular control of CRC checking and also allow us to decode the provided image.

Decode_InvalidDataChunkCrc_IgnoreCrcErrors_Rgba32_Issue_2589

Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<BmpDecoder>().Count());
Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<JpegDecoder>().Count());
Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<BmpDecoder>().Count());
Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<GifDecoder>().Count());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really good spot!!

@svenclaesson
Copy link
Copy Markdown
Contributor Author

Thanks for working on this

}

while (length < 0 || length > (this.currentStream.Length - this.currentStream.Position))
while (length < 0)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@antonfirsov I did find a way to improve this!

@JimBobSquarePants JimBobSquarePants merged commit e69e622 into SixLabors:main Nov 29, 2023
@JimBobSquarePants JimBobSquarePants deleted the better_handle_corrupt_png branch November 29, 2023 12:01
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.

Image.Load() throws InvalidImageContentException: CRC Error. PNG IDAT chunk is corrupt!

3 participants