Skip to content

Jpeg unhandled System.ArgumentException with malformed image #1900

@br3aker

Description

@br3aker

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

Description

I've modified valid image from repo image base with actually itu spec complient numbers. I've changed component count from SOF segment from 3 to 255 and modified marker 'length' accordingly. Current master throws System.ArgumentException:

Unhandled exception. System.ArgumentException: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
   at System.Buffer.BlockCopy(Array src, Int32 srcOffset, Array dst, Int32 dstOffset, Int32 count)
   at SixLabors.ImageSharp.Formats.Jpeg.JpegDecoderCore.ProcessStartOfFrameMarker(BufferedReadStream stream, Int32 remaining, JpegFileMarker& frameMarker, Boolean metadataOnly) in D:\Programming\C#\ImageSharp\src\ImageSharp\Formats\Jpeg\JpegDecoderCore.cs:line 1013
   at SixLabors.ImageSharp.Formats.Jpeg.JpegDecoderCore.ParseStream(BufferedReadStream stream, HuffmanScanDecoder scanDecoder, CancellationToken cancellationToken) in D:\Programming\C#\ImageSharp\src\ImageSharp\Formats\Jpeg\JpegDecoderCore.cs:line 312
   at SixLabors.ImageSharp.Formats.Jpeg.JpegDecoderCore.Decode[TPixel](BufferedReadStream stream, CancellationToken cancellationToken) in D:\Programming\C#\ImageSharp\src\ImageSharp\Formats\Jpeg\JpegDecoderCore.cs:line 183
   at SixLabors.ImageSharp.Formats.ImageDecoderUtilities.Decode[TPixel](IImageDecoderInternals decoder, Configuration configuration, Stream stream, Func`3 largeImageExceptionFactory) in D:\Programming\C#\ImageSharp\src\ImageSharp\Formats\ImageDecoderUtilities.cs:line 162
   at SixLabors.ImageSharp.Formats.ImageDecoderUtilities.Decode[TPixel](IImageDecoderInternals decoder, Configuration configuration, Stream stream) in D:\Programming\C#\ImageSharp\src\ImageSharp\Formats\ImageDecoderUtilities.cs:line 149
   at SixLabors.ImageSharp.Formats.Jpeg.JpegDecoder.Decode[TPixel](Configuration configuration, Stream stream) in D:\Programming\C#\ImageSharp\src\ImageSharp\Formats\Jpeg\JpegDecoder.cs:line 26

// Image.Load call site

libjpeg-turbo output:

Corrupt JPEG data: 3016 extraneous bytes before marker 0xd9
Invalid JPEG file structure: missing SOS marker

Steps to Reproduce

Image.Load(...) on this image:

jpeg_components255.zip

How to fix

Source of the problem is that 255 component SOF marker would occupy 8 + 255 * 3 == 8 + 765 bytes, 2 of them are 0xFF SOFn marker bytes, 6 of them are 'constant sized' and are read as 6 bytes package. Remaining 765 bytes are variable and depend on component count which is 255 in this case, they are read into intermediate buffer:

private readonly byte[] temp = new byte[2 * 16 * 4];

in one go:

stream.Read(this.temp, 0, remaining);

So decoder writes 765 bytes into 128 byte-sized buffer. We can enlarge buffer to handle up to 255 components but do we need to? I'm yet to see a JPEG color space with 255 dimensions. And even if such color space exists - right now decoder can handle up to 4 components color spaces (CMYK) so should we bother?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions