File tree Expand file tree Collapse file tree 4 files changed +28
-4
lines changed
src/ImageSharp/Formats/Jpeg/Components/Decoder Expand file tree Collapse file tree 4 files changed +28
-4
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,9 @@ internal struct JpegBitReader
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 JpegBitReader ( BufferedReadStream stream )
26
29
{
27
30
this . stream = stream ;
@@ -31,6 +34,7 @@ public JpegBitReader(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>
@@ -219,11 +223,16 @@ private int ReadStream()
219
223
// we know we have hit the EOI and completed decoding the scan buffer.
220
224
if ( value == - 1 || ( this . badData && this . data == 0 && this . stream . Position >= this . stream . Length ) )
221
225
{
222
- // We've encountered the end of the file stream which means there's no EOI marker
226
+ // We've hit the end of the file stream more times than allowed which means there's no EOI marker
223
227
// in the image or the SOS marker has the wrong dimensions set.
224
- this . badData = true ;
225
- this . NoData = true ;
226
- value = 0 ;
228
+ if ( this . eofHitCount > JpegConstants . Huffman . FetchLoop )
229
+ {
230
+ this . badData = true ;
231
+ this . NoData = true ;
232
+ value = 0 ;
233
+ }
234
+
235
+ this . eofHitCount ++ ;
227
236
}
228
237
229
238
return value ;
Original file line number Diff line number Diff line change @@ -364,4 +364,15 @@ public void Issue2517_DecodeWorks<TPixel>(TestImageProvider<TPixel> provider)
364
364
image . DebugSave ( provider ) ;
365
365
image . CompareToOriginal ( provider ) ;
366
366
}
367
+
368
+ // https://github.com/SixLabors/ImageSharp/issues/2638
369
+ [ Theory ]
370
+ [ WithFile ( TestImages . Jpeg . Issues . Issue2638 , PixelTypes . Rgba32 ) ]
371
+ public void Issue2638_DecodeWorks < TPixel > ( TestImageProvider < TPixel > provider )
372
+ where TPixel : unmanaged, IPixel < TPixel >
373
+ {
374
+ using Image < TPixel > image = provider . GetImage ( JpegDecoder . Instance ) ;
375
+ image . DebugSave ( provider ) ;
376
+ image . CompareToOriginal ( provider ) ;
377
+ }
367
378
}
Original file line number Diff line number Diff line change @@ -315,6 +315,7 @@ public static class Issues
315
315
public const string Issue2564 = "Jpg/issues/issue-2564.jpg" ;
316
316
public const string HangBadScan = "Jpg/issues/Hang_C438A851.jpg" ;
317
317
public const string Issue2517 = "Jpg/issues/issue2517-bad-d7.jpg" ;
318
+ public const string Issue2638 = "Jpg/issues/Issue2638.jpg" ;
318
319
319
320
public static class Fuzz
320
321
{
You can’t perform that action at this time.
0 commit comments