@@ -396,6 +396,15 @@ func (d *decoder) decode(r io.Reader, configOnly bool) (image.Image, error) {
396
396
if marker == eoiMarker {// End Of Image.
397
397
break
398
398
}
399
+ if rst0Marker <= marker && marker <= rst7Marker {
400
+ // Figures B.2 and B.16 of the specification suggest that restart markers should
401
+ // only occur between Entropy Coded Segments and not after the final ECS.
402
+ // However, some encoders may generate incorrect JPEGs with a final restart
403
+ // marker. That restart marker will be seen here instead of inside the processSOS
404
+ // method, and is ignored as a harmless error. Restart markers have no extra data,
405
+ // so we check for this before we read the 16-bit length of the segment.
406
+ continue
407
+ }
399
408
400
409
// Read the 16-bit length of the segment. The value includes the 2 bytes for the
401
410
// length itself, so we subtract 2 to get the number of remaining bytes.
@@ -424,7 +433,7 @@ func (d *decoder) decode(r io.Reader, configOnly bool) (image.Image, error) {
424
433
err = d .processSOS (n )
425
434
case marker == driMarker :// Define Restart Interval.
426
435
err = d .processDRI (n )
427
- case marker >= app0Marker && marker <= app15Marker || marker == comMarker :// APPlication specific, or COMment.
436
+ case app0Marker <= marker && marker <= app15Marker || marker == comMarker :// APPlication specific, or COMment.
428
437
err = d .ignore (n )
429
438
default :
430
439
err = UnsupportedError ("unknown marker" )