From e3e2785b6614ac6b6e7798fbe32a989249a17a82 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Sun, 18 Jul 2021 20:43:24 -0400 Subject: [PATCH] Fix a few uses of DeflateStream.Read The code is assuming it'll always return the requested amount unless it hits EOF, but that's not guaranteed. --- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 24 ++++++++++++------- .../Decompressors/DeflateTiffCompression.cs | 13 +++++++++- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index de70a9dff6..987dc150c2 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -506,11 +506,15 @@ namespace SixLabors.ImageSharp.Formats.Png while (this.currentRow < this.header.Height) { Span scanlineSpan = this.scanline.GetSpan(); - int bytesRead = compressedStream.Read(scanlineSpan, this.currentRowBytesRead, this.bytesPerScanline - this.currentRowBytesRead); - this.currentRowBytesRead += bytesRead; - if (this.currentRowBytesRead < this.bytesPerScanline) + while (this.currentRowBytesRead < this.bytesPerScanline) { - return; + int bytesRead = compressedStream.Read(scanlineSpan, this.currentRowBytesRead, this.bytesPerScanline - this.currentRowBytesRead); + if (bytesRead <= 0) + { + return; + } + + this.currentRowBytesRead += bytesRead; } this.currentRowBytesRead = 0; @@ -577,11 +581,15 @@ namespace SixLabors.ImageSharp.Formats.Png while (this.currentRow < this.header.Height) { - int bytesRead = compressedStream.Read(this.scanline.GetSpan(), this.currentRowBytesRead, bytesPerInterlaceScanline - this.currentRowBytesRead); - this.currentRowBytesRead += bytesRead; - if (this.currentRowBytesRead < bytesPerInterlaceScanline) + while (this.currentRowBytesRead < bytesPerInterlaceScanline) { - return; + int bytesRead = compressedStream.Read(this.scanline.GetSpan(), this.currentRowBytesRead, bytesPerInterlaceScanline - this.currentRowBytesRead); + if (bytesRead <= 0) + { + return; + } + + this.currentRowBytesRead += bytesRead; } this.currentRowBytesRead = 0; diff --git a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/DeflateTiffCompression.cs b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/DeflateTiffCompression.cs index 67af4ff6cb..2188913bc3 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/DeflateTiffCompression.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/DeflateTiffCompression.cs @@ -46,7 +46,18 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors { deframeStream.AllocateNewBytes(byteCount, true); DeflateStream dataStream = deframeStream.CompressedStream; - dataStream.Read(buffer, 0, buffer.Length); + + int totalRead = 0; + while (totalRead < buffer.Length) + { + int bytesRead = dataStream.Read(buffer, totalRead, buffer.Length - totalRead); + if (bytesRead <= 0) + { + break; + } + + totalRead += bytesRead; + } } if (this.Predictor == TiffPredictor.Horizontal)