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

PNG: Throw exception, if palette chunk is missing #2020

Merged
merged 4 commits into from
Feb 21, 2022

Conversation

brianpopow
Copy link
Collaborator

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

This PR changes the PNG decoder to throw an appropriate exception, if the palette chunk is missing.

@brianpopow brianpopow added this to the 2.*.* milestone Feb 19, 2022
@brianpopow brianpopow requested a review from a team February 19, 2022 18:00
@@ -21,6 +21,9 @@ public static void ThrowInvalidImageContentException(string errorMessage, Except
[MethodImpl(InliningOptions.ColdPath)]
public static void ThrowNoData() => throw new InvalidImageContentException("PNG Image does not contain a data chunk");

[MethodImpl(InliningOptions.ColdPath)]
public static void ThrowMissingPalette() => throw new InvalidImageContentException("PNG Image does not contain palette chunk");
Copy link
Member

Choose a reason for hiding this comment

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

Maybe a palette chunk?

@JimBobSquarePants
Copy link
Member

What's the current behavior? Out of range exception?

https://media.githubusercontent.com/media/SixLabors/ImageSharp/29ddc6053e1a27ab095e87d8baa015f7428c53ef/tests/Images/Input/Png/missing_plte.png

The first referenced example is unopenable using conventional editors Edge and Chrome can open it displaying it as a 16x16 transparent png.

I haven't tried the second but I wonder whether we should do the same? #2014 updates our Gif decoder to be more robust in this manner when decoding images whose color palette cannot be assigned. If we did the same for Png then the behavior for both decoders would then match browser behavior.

@brianpopow
Copy link
Collaborator Author

What's the current behavior? Out of range exception?

System.NullReferenceException: 'Object reference not set to an instance of an object.' when accessing the empty palette span.

https://media.githubusercontent.com/media/SixLabors/ImageSharp/29ddc6053e1a27ab095e87d8baa015f7428c53ef/tests/Images/Input/Png/missing_plte.png

The first referenced example is unopenable using conventional editors Edge and Chrome can open it displaying it as a 16x16 transparent png.

I haven't tried the second but I wonder whether we should do the same? #2014 updates our Gif decoder to be more robust in this manner when decoding images whose color palette cannot be assigned. If we did the same for Png then the behavior for both decoders would then match browser behavior.

The png is corrupt and in my opinion throwing an exception and stating what's wrong with image is the right thing to do in such case. I dont think silently ignoring this would be good.

@JimBobSquarePants
Copy link
Member

The png is corrupt and in my opinion throwing an exception and stating what's wrong with image is the right thing to do in such case. I dont think silently ignoring this would be good.

Yeah, I've been flip-flopping around that. I'm gonna update my Gif PR to do what you do. 👍

Copy link
Member

@JimBobSquarePants JimBobSquarePants left a comment

Choose a reason for hiding this comment

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

LGTM Thanks! 👍

@JimBobSquarePants JimBobSquarePants merged commit 968c5ff into main Feb 21, 2022
@JimBobSquarePants JimBobSquarePants deleted the bp/missingpalette branch February 21, 2022 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants