Imagesharp: JpgExport -> System.ArgumentOutOfRangeException: Index and count must refer to a location within the buffer. Parameter name: bytes

Created on 8 May 2018  路  4Comments  路  Source: SixLabors/ImageSharp

Prerequisites

  • [x] I have written a descriptive issue title
  • [x] 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
  • [x] I have searched open and closed issues to ensure it has not already been reported

Description


Similar to this issue I raised yesterday but with a different stack trace (thanks for looking at it so quickly!). I'm using ImageSharp to open all these images from mscoco - there are thousands of images and it's doing well on most of them.

Loading and export this image as a Jpg ByteArray causes the following stack trace (again, PNG works okay):

Steps to Reproduce

coco_val2014_000000260238


Unhandled Exception: System.ArgumentOutOfRangeException: Index and count must refer to a location within the buffer. Parameter name: bytes at System.Text.UnicodeEncoding.GetString(Byte[] bytes, Int32 index, Int32 count) at SixLabors.ImageSharp.MetaData.Profiles.Icc.IccDataReader.ReadTextDescriptionTagDataEntry() at SixLabors.ImageSharp.MetaData.Profiles.Icc.IccDataReader.ReadTagDataEntry(IccTagTableEntry info) at SixLabors.ImageSharp.MetaData.Profiles.Icc.IccReader.ReadTagData(IccDataReader reader) at SixLabors.ImageSharp.MetaData.Profiles.Icc.IccProfile.InitializeEntries() at SixLabors.ImageSharp.MetaData.Profiles.Icc.IccWriter.Write(IccProfile profile) at SixLabors.ImageSharp.Formats.Jpeg.GolangPort.JpegEncoderCore.WriteIccProfile(IccProfile iccProfile) at SixLabors.ImageSharp.Formats.Jpeg.GolangPort.JpegEncoderCore.Encode[TPixel](Image1 image, Stream stream)
at WebApplicationBasic.Program.Main(String[] args) in Program.cs:line 17
`

I loaded the image and exported to JPG like so:
var img = SixLabors.ImageSharp.Image.Load<Rgba32>(args[0]); img.ToByteArray(new JpegEncoder());

System Configuration

  • ImageSharp version:
    SixLabors.ImageSharp 1.0.0-beta0003
  • Other ImageSharp packages and versions:
  • Environment (Operating system, version and so on):
    MacOS High Sierra
  • .NET Framework version:
    Dotnet 2.1.3
  • Additional information:
jpeg icc

Most helpful comment

@JimBobSquarePants, sure, I'll try to implement both fixes this week.

All 4 comments

It's another invalid ICC profile. This time it's only one tag tough. The text description tag consists of three differently encoded texts. ASCII, Unicode and ScriptCode. In this case the ASCII part reports a string length of 33 while it's actually 43. Then the it tries to read the Unicode length which should be zero, but actually reads part of the previous text and returns a huge number.

I think I can add a safeguard around invalid tags like this by checking lengths against the tag size. If it's too big it'll stop trying to read the tag and report it as invalid or simply discard it.

@JBildstein If you were able to PR something that would be really useful. I still don't know the inner workings of ICC profiles yet to do a good job.

@JimBobSquarePants, sure, I'll try to implement both fixes this week.

You rock! 馃

Was this page helpful?
0 / 5 - 0 ratings