Skip to content

Commit 9c25d6c

Browse files
Merge pull request #1178 from SixLabors/js/fix-1177
Always read CRC.
2 parents 8d404d6 + b86ad76 commit 9c25d6c

File tree

5 files changed

+40
-15
lines changed

5 files changed

+40
-15
lines changed

src/ImageSharp/Formats/Png/PngDecoderCore.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,24 +1141,22 @@ private bool TryReadChunk(out PngChunk chunk)
11411141
/// <param name="chunk">The <see cref="PngChunk"/>.</param>
11421142
private void ValidateChunk(in PngChunk chunk)
11431143
{
1144-
if (!chunk.IsCritical)
1145-
{
1146-
return;
1147-
}
1148-
1149-
Span<byte> chunkType = stackalloc byte[4];
1144+
uint crc = this.ReadChunkCrc();
11501145

1151-
BinaryPrimitives.WriteUInt32BigEndian(chunkType, (uint)chunk.Type);
1146+
if (chunk.IsCritical)
1147+
{
1148+
Span<byte> chunkType = stackalloc byte[4];
1149+
BinaryPrimitives.WriteUInt32BigEndian(chunkType, (uint)chunk.Type);
11521150

1153-
this.crc.Reset();
1154-
this.crc.Update(chunkType);
1155-
this.crc.Update(chunk.Data.GetSpan());
1151+
this.crc.Reset();
1152+
this.crc.Update(chunkType);
1153+
this.crc.Update(chunk.Data.GetSpan());
11561154

1157-
uint crc = this.ReadChunkCrc();
1158-
if (this.crc.Value != crc)
1159-
{
1160-
string chunkTypeName = Encoding.ASCII.GetString(chunkType);
1161-
PngThrowHelper.ThrowInvalidChunkCrc(chunkTypeName);
1155+
if (this.crc.Value != crc)
1156+
{
1157+
string chunkTypeName = Encoding.ASCII.GetString(chunkType);
1158+
PngThrowHelper.ThrowInvalidChunkCrc(chunkTypeName);
1159+
}
11621160
}
11631161
}
11641162

tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ public partial class PngDecoderTests
8989
TestImages.Png.Issue1014_5, TestImages.Png.Issue1014_6
9090
};
9191

92+
public static readonly string[] TestImagesIssue1177 =
93+
{
94+
TestImages.Png.Issue1177_1,
95+
TestImages.Png.Issue1177_2
96+
};
97+
9298
[Theory]
9399
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32)]
94100
public void Decode<TPixel>(TestImageProvider<TPixel> provider)
@@ -243,6 +249,23 @@ public void Issue1014<TPixel>(TestImageProvider<TPixel> provider)
243249
Assert.Null(ex);
244250
}
245251

252+
[Theory]
253+
[WithFileCollection(nameof(TestImagesIssue1177), PixelTypes.Rgba32)]
254+
public void Issue1177<TPixel>(TestImageProvider<TPixel> provider)
255+
where TPixel : unmanaged, IPixel<TPixel>
256+
{
257+
System.Exception ex = Record.Exception(
258+
() =>
259+
{
260+
using (Image<TPixel> image = provider.GetImage(PngDecoder))
261+
{
262+
image.DebugSave(provider);
263+
image.CompareToOriginal(provider, ImageComparer.Exact);
264+
}
265+
});
266+
Assert.Null(ex);
267+
}
268+
246269
[Theory]
247270
[WithFile(TestImages.Png.Issue1127, PixelTypes.Rgba32)]
248271
public void Issue1127<TPixel>(TestImageProvider<TPixel> provider)

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ public static class Png
9595
public const string Issue1014_6 = "Png/issues/Issue_1014_6.png";
9696
public const string Issue1127 = "Png/issues/Issue_1127.png";
9797

98+
// Issue 1177: https://github.com/SixLabors/ImageSharp/issues/1177
99+
public const string Issue1177_1 = "Png/issues/Issue_1177_1.png";
100+
public const string Issue1177_2 = "Png/issues/Issue_1177_2.png";
101+
98102
public static class Bad
99103
{
100104
// Odd chunk lengths
6.86 KB
Loading
55.8 KB
Loading

0 commit comments

Comments
 (0)