diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 186e99437..ce2d28ff0 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -539,13 +539,8 @@ namespace ImageSharp.Formats private PngChunk ReadChunk() { PngChunk chunk = new PngChunk(); - - if (this.ReadChunkLength(chunk) == 0) - { - return null; - } - - if (chunk.Length <= 0) + this.ReadChunkLength(chunk); + if (chunk.Length < 0) { return null; } @@ -630,7 +625,7 @@ namespace ImageSharp.Formats /// /// Thrown if the input stream is not valid. /// - private int ReadChunkLength(PngChunk chunk) + private void ReadChunkLength(PngChunk chunk) { int numBytes = this.currentStream.Read(this.chunkLengthBuffer, 0, 4); if (numBytes >= 1 && numBytes <= 3) @@ -638,11 +633,15 @@ namespace ImageSharp.Formats throw new ImageFormatException("Image stream is not valid!"); } + if (numBytes <= 0) + { + chunk.Length = -1; + return; + } + this.chunkLengthBuffer.ReverseBytes(); chunk.Length = BitConverter.ToInt32(this.chunkLengthBuffer, 0); - - return numBytes; } } }