File tree Expand file tree Collapse file tree 4 files changed +31
-4
lines changed
src/ImageSharp/Formats/Jpeg/Components/Decoder Expand file tree Collapse file tree 4 files changed +31
-4
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,9 @@ internal struct JpegBitReader
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 JpegBitReader ( BufferedReadStream stream )
2629 {
2730 this . stream = stream ;
@@ -31,6 +34,7 @@ public JpegBitReader(BufferedReadStream stream)
3134 this . MarkerPosition = 0 ;
3235 this . badData = false ;
3336 this . NoData = false ;
37+ this . eofHitCount = 0 ;
3438 }
3539
3640 /// <summary>
@@ -80,6 +84,9 @@ public void Reset()
8084 [ MethodImpl ( InliningOptions . ShortMethod ) ]
8185 public bool HasBadMarker ( ) => this . Marker != JpegConstants . Markers . XFF && ! this . HasRestartMarker ( ) ;
8286
87+ [ MethodImpl ( InliningOptions . ShortMethod ) ]
88+ public bool HasEndMarker ( ) => this . Marker == JpegConstants . Markers . EOI ;
89+
8390 [ MethodImpl ( InliningOptions . AlwaysInline ) ]
8491 public void FillBuffer ( )
8592 {
@@ -219,11 +226,16 @@ private int ReadStream()
219226 // we know we have hit the EOI and completed decoding the scan buffer.
220227 if ( value == - 1 || ( this . badData && this . data == 0 && this . stream . Position >= this . stream . Length ) )
221228 {
222- // We've encountered the end of the file stream which means there's no EOI marker
229+ // We've passed the end of the file stream which means there's no EOI marker
223230 // in the image or the SOS marker has the wrong dimensions set.
224- this . badData = true ;
225- this . NoData = true ;
226- value = 0 ;
231+ if ( this . eofHitCount > JpegConstants . Huffman . FetchLoop )
232+ {
233+ this . badData = true ;
234+ this . NoData = true ;
235+ value = 0 ;
236+ }
237+
238+ this . eofHitCount ++ ;
227239 }
228240
229241 return value ;
Original file line number Diff line number Diff line change @@ -364,4 +364,15 @@ public void Issue2517_DecodeWorks<TPixel>(TestImageProvider<TPixel> provider)
364364 image . DebugSave ( provider ) ;
365365 image . CompareToOriginal ( provider ) ;
366366 }
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+ }
367378}
Original file line number Diff line number Diff line change @@ -315,6 +315,7 @@ public static class Issues
315315 public const string Issue2564 = "Jpg/issues/issue-2564.jpg" ;
316316 public const string HangBadScan = "Jpg/issues/Hang_C438A851.jpg" ;
317317 public const string Issue2517 = "Jpg/issues/issue2517-bad-d7.jpg" ;
318+ public const string Issue2638 = "Jpg/issues/Issue2638.jpg" ;
318319
319320 public static class Fuzz
320321 {
You can’t perform that action at this time.
0 commit comments