Skip to content

Commit bb82f79

Browse files
committed
#2701 to 2.1.x [copy]
1 parent 627b5f7 commit bb82f79

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

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

+13-4
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>
@@ -218,11 +222,16 @@ private int ReadStream()
218222
// we know we have hit the EOI and completed decoding the scan buffer.
219223
if (value == -1 || (this.badData && this.data == 0 && this.stream.Position >= this.stream.Length))
220224
{
221-
// We've encountered the end of the file stream which means there's no EOI marker
225+
// We've hit the end of the file stream more times than allowed which means there's no EOI marker
222226
// in the image or the SOS marker has the wrong dimensions set.
223-
this.badData = true;
224-
this.NoData = true;
225-
value = 0;
227+
if (this.eofHitCount > JpegConstants.Huffman.FetchLoop)
228+
{
229+
this.badData = true;
230+
this.NoData = true;
231+
value = 0;
232+
}
233+
234+
this.eofHitCount++;
226235
}
227236

228237
return value;

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

+13
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ public static class Issues
269269
public const string ValidExifArgumentNullExceptionOnEncode = "Jpg/issues/Issue2087-exif-null-reference-on-encode.jpg";
270270
public const string Issue2133DeduceColorSpace = "Jpg/issues/Issue2133.jpg";
271271
public const string HangBadScan = "Jpg/issues/Hang_C438A851.jpg";
272-
public const string Issue2758 = "Jpg/issues/issue-2758.jpg";
272+
public const string Issue2758 = "Jpg/issues/issue-2758.jpg";
273+
public const string Issue2638 = "Jpg/issues/Issue2638.jpg";
273274

274275
public static class Fuzz
275276
{
Loading

0 commit comments

Comments
 (0)