Skip to content

BaseExifReader.ReadSubIfd throws InvalidOperationException with "Collection was modified" #2857

Open
@klinkby

Description

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

ImageSharp version

3.1.6

Other ImageSharp packages and versions

2.1.9

Environment (Operating system, version and so on)

Windows 11 (10.0.26100.2605)

.NET Framework version

net9.0

Description

The issue can be reproduced with the two LoC in a console app in "Steps to Reproduce" and the attached jpeg. Crashes with:

An exception of type 'System.InvalidOperationException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'Collection was modified; enumeration operation may not execute.'
   at System.ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion()
   at System.Collections.Generic.List`1.Enumerator.MoveNext()
   at SixLabors.ImageSharp.Metadata.Profiles.Exif.BaseExifReader.ReadSubIfd(List`1 values)
   at SixLabors.ImageSharp.Metadata.Profiles.Exif.ExifReader.ReadValues()
   at SixLabors.ImageSharp.Metadata.Profiles.Exif.ExifProfile.InitializeValues()
   at SixLabors.ImageSharp.Metadata.Profiles.Exif.ExifProfile.TryCreateThumbnail[TPixel](Image`1& image)
   at SixLabors.ImageSharp.Metadata.Profiles.Exif.ExifProfile.TryCreateThumbnail(Image& image)
   at Program.<<Main>$>d__0.MoveNext() in Program.cs:line 3

The image is a commercially licensed image from Colourbox, seemlingly generated with Adobe Photoshop CS 3. Windows Explorer and various online exif parsers has no problem showing the metadata.


First discovered in Umbraco CMS that use ImageSharp for rendering thumbnails: When adding the specific jpeg images with extensive exif metadata it fails to produce thumbnails from it. Here The log shows:

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
    at SixLabors.ImageSharp.Metadata.Profiles.Exif.BaseExifReader.ReadSubIfd(List`1 values)
    at SixLabors.ImageSharp.Metadata.Profiles.Exif.ExifReader.ReadValues()
    at SixLabors.ImageSharp.Metadata.Profiles.Exif.ExifProfile.InitializeValues()
    at SixLabors.ImageSharp.Metadata.Profiles.Exif.ExifProfile.GetValueInternal(ExifTag tag)
    at SixLabors.ImageSharp.Web.FormattedImage.TryGetExifOrientation(UInt16& value)
    at SixLabors.ImageSharp.Web.Processors.ResizeWebProcessor.GetExifOrientation(FormattedImage image, CommandCollection commands, CommandParser parser, CultureInfo culture)
    at SixLabors.ImageSharp.Web.Processors.ResizeWebProcessor.GetResizeOptions(FormattedImage image, CommandCollection commands, CommandParser parser, CultureInfo culture)
    at SixLabors.ImageSharp.Web.Processors.ResizeWebProcessor.Process(FormattedImage image, ILogger logger, CommandCollection commands, CommandParser parser, CultureInfo culture)
    at SixLabors.ImageSharp.Web.Processors.WebProcessingExtensions.Process(FormattedImage source, ILogger logger, IReadOnlyList`1 processors, CommandCollection commands, CommandParser parser, CultureInfo culture)
    at SixLabors.ImageSharp.Web.Middleware.ImageSharpMiddleware.ProcessRequestAsync(HttpContext httpContext, IImageResolver sourceImageResolver, ImageContext imageContext, CommandCollection commands, Boolean retry)
    at SixLabors.ImageSharp.Web.Middleware.ImageSharpMiddleware.ProcessRequestAsync(HttpContext httpContext, IImageResolver sourceImageResolver, ImageContext imageContext, CommandCollection commands, Boolean retry)
    at SixLabors.ImageSharp.Web.Middleware.ImageSharpMiddleware.Invoke(HttpContext httpContext, Boolean retry)
    at StackExchange.Profiling.MiniProfilerMiddleware.Invoke(HttpContext context) in C:\\projects\\dotnet\\src\\MiniProfiler.AspNetCore\\MiniProfilerMiddleware.cs:line 121
    at Umbraco.Cms.Web.Common.Middleware.UmbracoRequestMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)
    at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass6_1.<<UseMiddlewareInterface>b__1>d.MoveNext()

Steps to Reproduce

using SixLabors.ImageSharp;
var image = await Image.LoadAsync("colourbox1227365.jpg");
image.Metadata.ExifProfile.TryCreateThumbnail(out var _); // <-- throws InvalidOperationException

Images

Attached image is commercial licensed to the company I work for, and MUST NOT be redistributed.

colourbox1227365

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