From 82f1b423904ae631cb6eb8d2013287b69b62160c Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Sat, 20 Jan 2018 10:08:44 +0100 Subject: [PATCH 1/3] Throw exception when the image does not contain a data chunk (#441). --- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 5cdf80289c..41f3030930 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -269,6 +269,11 @@ namespace SixLabors.ImageSharp.Formats.Png } } + if (image == null) + { + throw new ImageFormatException("PNG Image does not contain a data chunk"); + } + return image; } finally From 61f7a218114e6483642abdc604fc78f09e96fca7 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 4 Mar 2018 23:43:36 +1100 Subject: [PATCH 2/3] Fix broken test --- .../Formats/Png/PngDecoderTests.cs | 63 ++++++++++++++----- 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs index 248e0a5eea..09c268c141 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs @@ -19,6 +19,39 @@ namespace SixLabors.ImageSharp.Tests { private const PixelTypes PixelTypes = Tests.PixelTypes.Rgba32 | Tests.PixelTypes.RgbaVector | Tests.PixelTypes.Argb32; + // Contains the png marker, IHDR and pHYs chunks of a 1x1 pixel 32bit png 1 a single black pixel. + private static byte[] raw1x1PngIHDRAndpHYs = + { + // PNG Identifier + 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, + + // IHDR + 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, + // IHDR CRC + 0x90, 0x77, 0x53, 0xDE, + + // pHYS + 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0E, 0xC3, 0x00, 0x00, 0x0E, 0xC3, 0x01, + // pHYS CRC + 0xC7, 0x6F, 0xA8, 0x64 + }; + + // Contains the png marker, IDAT and IEND chunks of a 1x1 pixel 32bit png 1 a single black pixel. + private static byte[] raw1x1PngIDATAndIEND = + { + // IDAT + 0x00, 0x00, 0x00, 0x0C, 0x49, 0x44, 0x41, 0x54, 0x18, 0x57, 0x63, 0x60, 0x60, 0x60, 0x00, 0x00, + 0x00, 0x04, 0x00, 0x01, + // IDAT CRC + 0x5C, 0xCD, 0xFF, 0x69, + + // IEND + 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, + 0x4E, 0x44, + // IEND CRC + 0xAE, 0x42, 0x60, 0x82 + }; + public static readonly string[] CommonTestImages = { TestImages.Png.Splash, TestImages.Png.Indexed, @@ -210,11 +243,9 @@ namespace SixLabors.ImageSharp.Tests { using (var memStream = new MemoryStream()) { - memStream.Skip(8); - + WriteHeaderChunk(memStream); WriteChunk(memStream, chunkName); - - CompressStream(memStream); + WriteDataChunk(memStream); var decoder = new PngDecoder(); @@ -230,23 +261,27 @@ namespace SixLabors.ImageSharp.Tests [Theory] [InlineData(PngChunkTypes.Gamma)] [InlineData(PngChunkTypes.PaletteAlpha)] - [InlineData(PngChunkTypes.Physical)] + [InlineData(PngChunkTypes.Physical)] // It's ok to test physical as we don't throw for duplicate chunks. //[InlineData(PngChunkTypes.Text)] //TODO: Figure out how to test this public void Decode_IncorrectCRCForNonCriticalChunk_ExceptionIsThrown(string chunkName) { using (var memStream = new MemoryStream()) { - memStream.Skip(8); - + WriteHeaderChunk(memStream); WriteChunk(memStream, chunkName); - - CompressStream(memStream); + WriteDataChunk(memStream); var decoder = new PngDecoder(); decoder.Decode(null, memStream); } } + private static void WriteHeaderChunk(MemoryStream memStream) + { + // Writes a 1x1 32bit png header chunk containing a single black pixel + memStream.Write(raw1x1PngIHDRAndpHYs, 0, raw1x1PngIHDRAndpHYs.Length); + } + private static void WriteChunk(MemoryStream memStream, string chunkName) { memStream.Write(new byte[] { 0, 0, 0, 1 }, 0, 4); @@ -254,13 +289,11 @@ namespace SixLabors.ImageSharp.Tests memStream.Write(new byte[] { 0, 0, 0, 0, 0 }, 0, 5); } - private static void CompressStream(Stream stream) + private static void WriteDataChunk(MemoryStream memStream) { - stream.Position = 0; - using (var deflateStream = new DeflateStream(stream, CompressionLevel.NoCompression, true)) - { - } - stream.Position = 0; + // Writes a 1x1 32bit png data chunk containing a single black pixel + memStream.Write(raw1x1PngIDATAndIEND, 0, raw1x1PngIDATAndIEND.Length); + memStream.Position = 0; } } } \ No newline at end of file From aca4560118686cd2353695ba1c80ee85192a7930 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 4 Mar 2018 23:56:58 +1100 Subject: [PATCH 3/3] Fix test --- tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs index 09c268c141..f70e4e3300 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs @@ -26,6 +26,7 @@ namespace SixLabors.ImageSharp.Tests 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, // IHDR + 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, // IHDR CRC 0x90, 0x77, 0x53, 0xDE,