File tree 4 files changed +31
-5
lines changed
src/ImageSharp/Formats/Jpeg/Components/Decoder
4 files changed +31
-5
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,9 @@ internal struct HuffmanScanBuffer
22
22
// Whether there is no more good data to pull from the stream for the current mcu.
23
23
private bool badData ;
24
24
25
+ // How many times have we hit the eof.
26
+ private int eofHitCount ;
27
+
25
28
public HuffmanScanBuffer ( BufferedReadStream stream )
26
29
{
27
30
this . stream = stream ;
@@ -31,6 +34,7 @@ public HuffmanScanBuffer(BufferedReadStream stream)
31
34
this . MarkerPosition = 0 ;
32
35
this . badData = false ;
33
36
this . NoData = false ;
37
+ this . eofHitCount = 0 ;
34
38
}
35
39
36
40
/// <summary>
@@ -218,11 +222,16 @@ private int ReadStream()
218
222
// we know we have hit the EOI and completed decoding the scan buffer.
219
223
if ( value == - 1 || ( this . badData && this . data == 0 && this . stream . Position >= this . stream . Length ) )
220
224
{
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
222
226
// 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 ++ ;
226
235
}
227
236
228
237
return value ;
Original file line number Diff line number Diff line change @@ -229,6 +229,19 @@ public void Issue2133_DeduceColorSpace<TPixel>(TestImageProvider<TPixel> provide
229
229
}
230
230
}
231
231
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
+
232
245
// DEBUG ONLY!
233
246
// The PDF.js output should be saved by "tests\ImageSharp.Tests\Formats\Jpg\pdfjs\jpeg-converter.htm"
234
247
// into "\tests\Images\ActualOutput\JpegDecoderTests\"
Original file line number Diff line number Diff line change @@ -269,7 +269,8 @@ public static class Issues
269
269
public const string ValidExifArgumentNullExceptionOnEncode = "Jpg/issues/Issue2087-exif-null-reference-on-encode.jpg" ;
270
270
public const string Issue2133DeduceColorSpace = "Jpg/issues/Issue2133.jpg" ;
271
271
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" ;
273
274
274
275
public static class Fuzz
275
276
{
You can’t perform that action at this time.
0 commit comments