Skip to content

Commit bc7c600

Browse files
Backported #2701 (fixes #2638) to release 2.1.x
1 parent 71d5316 commit bc7c600

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanBuffer.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ internal struct HuffmanScanBuffer
2222
// Whether there is no more good data to pull from the stream for the current mcu.
2323
private bool badData;
2424

25+
// How many times have we hit the eof.
26+
private int eofHitCount;
27+
2528
public HuffmanScanBuffer(BufferedReadStream stream)
2629
{
2730
this.stream = stream;
@@ -31,6 +34,7 @@ public HuffmanScanBuffer(BufferedReadStream stream)
3134
this.MarkerPosition = 0;
3235
this.badData = false;
3336
this.NoData = false;
37+
this.eofHitCount = 0;
3438
}
3539

3640
/// <summary>
@@ -211,13 +215,19 @@ public bool FindNextMarker()
211215
private int ReadStream()
212216
{
213217
int value = this.badData ? 0 : this.stream.ReadByte();
218+
214219
if (value == -1)
215220
{
216-
// We've encountered the end of the file stream which means there's no EOI marker
221+
// We've hit the end of the file stream more times than allowed which means there's no EOI marker
217222
// in the image or the SOS marker has the wrong dimensions set.
218-
this.badData = true;
219-
this.NoData = true;
220-
value = 0;
223+
if (this.eofHitCount > JpegConstants.Huffman.FetchLoop)
224+
{
225+
this.badData = true;
226+
this.NoData = true;
227+
value = 0;
228+
}
229+
230+
this.eofHitCount++;
221231
}
222232

223233
return value;

tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,19 @@ public void Issue2133_DeduceColorSpace<TPixel>(TestImageProvider<TPixel> provide
229229
}
230230
}
231231

232+
// https://github.com/SixLabors/ImageSharp/issues/2638
233+
[Theory]
234+
[WithFile(TestImages.Jpeg.Issues.Issue2638, PixelTypes.Rgba32)]
235+
public void Issue2638_DecodeWorks<TPixel>(TestImageProvider<TPixel> provider)
236+
where TPixel : unmanaged, IPixel<TPixel>
237+
{
238+
using (Image<TPixel> image = provider.GetImage(JpegDecoder))
239+
{
240+
image.DebugSave(provider);
241+
image.CompareToOriginal(provider);
242+
}
243+
}
244+
232245
// DEBUG ONLY!
233246
// The PDF.js output should be saved by "tests\ImageSharp.Tests\Formats\Jpg\pdfjs\jpeg-converter.htm"
234247
// into "\tests\Images\ActualOutput\JpegDecoderTests\"

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ public static class Issues
266266
public const string ExifNullArrayTag = "Jpg/issues/issue-2056-exif-null-array.jpg";
267267
public const string ValidExifArgumentNullExceptionOnEncode = "Jpg/issues/Issue2087-exif-null-reference-on-encode.jpg";
268268
public const string Issue2133DeduceColorSpace = "Jpg/issues/Issue2133.jpg";
269+
public const string Issue2638 = "Jpg/issues/Issue2638.jpg";
269270

270271
public static class Fuzz
271272
{
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)