From 2e10c1128632eb92ff9ee2797bcd5537ec62e0a8 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Sat, 29 Sep 2018 15:56:00 -0700 Subject: [PATCH] Ensure 4 bytes are read when reading png chunk type --- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index a728bb296..3b67146b9 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -1148,14 +1148,9 @@ namespace SixLabors.ImageSharp.Formats.Png /// private PngChunkType ReadChunkType() { - int numBytes = this.currentStream.Read(this.chunkTypeBuffer, 0, 4); - - if (numBytes >= 1 && numBytes <= 3) - { - throw new ImageFormatException("Image stream is not valid!"); - } - - return (PngChunkType)BinaryPrimitives.ReadUInt32BigEndian(this.chunkTypeBuffer.AsSpan()); + return this.currentStream.Read(this.chunkTypeBuffer, 0, 4) == 4 + ? (PngChunkType)BinaryPrimitives.ReadUInt32BigEndian(this.chunkTypeBuffer.AsSpan()) + : throw new ImageFormatException("Invalid PNG data."); } ///