Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relaxed handle of corrupt png files #2589

Merged

Conversation

svenclaesson
Copy link
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

CLAassistant commented Nov 24, 2023

CLA assistant check
All committers have signed the CLA.

@JimBobSquarePants
Copy link
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
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

@@ -43,7 +43,7 @@ public void IfAutoLoadWellKnownFormatsIsTrueAllFormatsAreLoaded()
Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<PngDecoder>().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<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
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
Contributor Author

Thanks for working on this

return false;
}

while (length < 0 || length > (this.currentStream.Length - this.currentStream.Position))
while (length < 0)
Copy link
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
8 checks passed
@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