Skip to content

Commit 9baba86

Browse files
Merge pull request #1707 from stephentoub/fixdsread
Fix a few uses of DeflateStream.Read
2 parents ce2a28b + defec9c commit 9baba86

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

src/ImageSharp/Formats/Png/PngDecoderCore.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -506,11 +506,15 @@ private void DecodePixelData<TPixel>(DeflateStream compressedStream, ImageFrame<
506506
while (this.currentRow < this.header.Height)
507507
{
508508
Span<byte> scanlineSpan = this.scanline.GetSpan();
509-
int bytesRead = compressedStream.Read(scanlineSpan, this.currentRowBytesRead, this.bytesPerScanline - this.currentRowBytesRead);
510-
this.currentRowBytesRead += bytesRead;
511-
if (this.currentRowBytesRead < this.bytesPerScanline)
509+
while (this.currentRowBytesRead < this.bytesPerScanline)
512510
{
513-
return;
511+
int bytesRead = compressedStream.Read(scanlineSpan, this.currentRowBytesRead, this.bytesPerScanline - this.currentRowBytesRead);
512+
if (bytesRead <= 0)
513+
{
514+
return;
515+
}
516+
517+
this.currentRowBytesRead += bytesRead;
514518
}
515519

516520
this.currentRowBytesRead = 0;
@@ -577,11 +581,15 @@ private void DecodeInterlacedPixelData<TPixel>(DeflateStream compressedStream, I
577581

578582
while (this.currentRow < this.header.Height)
579583
{
580-
int bytesRead = compressedStream.Read(this.scanline.GetSpan(), this.currentRowBytesRead, bytesPerInterlaceScanline - this.currentRowBytesRead);
581-
this.currentRowBytesRead += bytesRead;
582-
if (this.currentRowBytesRead < bytesPerInterlaceScanline)
584+
while (this.currentRowBytesRead < bytesPerInterlaceScanline)
583585
{
584-
return;
586+
int bytesRead = compressedStream.Read(this.scanline.GetSpan(), this.currentRowBytesRead, bytesPerInterlaceScanline - this.currentRowBytesRead);
587+
if (bytesRead <= 0)
588+
{
589+
return;
590+
}
591+
592+
this.currentRowBytesRead += bytesRead;
585593
}
586594

587595
this.currentRowBytesRead = 0;

src/ImageSharp/Formats/Tiff/Compression/Decompressors/DeflateTiffCompression.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,18 @@ protected override void Decompress(BufferedReadStream stream, int byteCount, Spa
4646
{
4747
deframeStream.AllocateNewBytes(byteCount, true);
4848
DeflateStream dataStream = deframeStream.CompressedStream;
49-
dataStream.Read(buffer, 0, buffer.Length);
49+
50+
int totalRead = 0;
51+
while (totalRead < buffer.Length)
52+
{
53+
int bytesRead = dataStream.Read(buffer, totalRead, buffer.Length - totalRead);
54+
if (bytesRead <= 0)
55+
{
56+
break;
57+
}
58+
59+
totalRead += bytesRead;
60+
}
5061
}
5162

5263
if (this.Predictor == TiffPredictor.Horizontal)

0 commit comments

Comments
 (0)