diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs
index e2cd806314..272f93d108 100644
--- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs
+++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs
@@ -1141,19 +1141,22 @@ namespace SixLabors.ImageSharp.Formats.Png
/// The .
private void ValidateChunk(in PngChunk chunk)
{
- Span chunkType = stackalloc byte[4];
+ uint crc = this.ReadChunkCrc();
- BinaryPrimitives.WriteUInt32BigEndian(chunkType, (uint)chunk.Type);
+ if (chunk.IsCritical)
+ {
+ Span chunkType = stackalloc byte[4];
+ BinaryPrimitives.WriteUInt32BigEndian(chunkType, (uint)chunk.Type);
- this.crc.Reset();
- this.crc.Update(chunkType);
- this.crc.Update(chunk.Data.GetSpan());
+ this.crc.Reset();
+ this.crc.Update(chunkType);
+ this.crc.Update(chunk.Data.GetSpan());
- uint crc = this.ReadChunkCrc();
- if (this.crc.Value != crc && chunk.IsCritical)
- {
- string chunkTypeName = Encoding.ASCII.GetString(chunkType);
- PngThrowHelper.ThrowInvalidChunkCrc(chunkTypeName);
+ if (this.crc.Value != crc)
+ {
+ string chunkTypeName = Encoding.ASCII.GetString(chunkType);
+ PngThrowHelper.ThrowInvalidChunkCrc(chunkTypeName);
+ }
}
}