Skip to content

Jpeg decoder can lead to finalizer memory leaks with specific malformed images #2078

Closed

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

ImageSharp version

Current main branch

Other ImageSharp packages and versions

None

Environment (Operating system, version and so on)

Windows 10

.NET Framework version

Any supported

Description

Jpeg decoder can leak IDisposable memory wrapper to the finalizer, no memory is actually leaked this way but MemoryDiagnostics reports this as a leak even with proper exception handling via try/catch;

Current decoding pipeline:

1. Gather jpeg info (components, tables, frame size, etc.) <- pixel buffer is allocated here
2. For each scan:
       For each mcu row:
           Decode: huffman -> spectral -> rgb -> TPixel
3. Create Image object using allocated pixel buffer

Any exceptions between step 1 and 3 would cause allocated pixel buffer to be gc'd leading to a false positive memory leak reported by memory diagnostics. Any invalid marker between scans can cause this, it shouldn't crash the entire process.

Steps to Reproduce

This code:

try
{
    using Image img = Image.Load(/* path to image */);
}
catch(Exception ex)
{
    Console.WriteLine($"{ex.GetType()}\n{ex.Message}");
}

GC.Collect();
Console.WriteLine(MemoryDiagnostics.TotalUndisposedAllocationCount);

With attached image prints:

SixLabors.ImageSharp.InvalidImageContentException
Multiple SOF markers. Only single frame jpegs supported.
1

Note that 1 at the end signaling that we have a single leaked memory resource which we can't resolve from user perspective.

Images

progressive_inter_scan_exception.zip

Attached image is a manually malformed progressive jpeg image with multiple scans, it contains second SOS marker right after the first scan which is considered a malformed jpeg. It doesn't contain any actual data after malformed marker to reduce test image size.

Activity

  1. JimBobSquarePants commented on Apr 9, 2022

    @JimBobSquarePants
    Member

    Ace. Thanks for raising this. I'm assuming you already have a solution in mind?

  2. br3aker commented on Apr 9, 2022

    @br3aker
    ContributorAuthor

    It should be fairly simple, I'll create a PR soon (I hope, gotta finish that downscaling stuff).

  3. tocsoft commented on Apr 10, 2022

    @tocsoft
    Member

    I think this might be solved by #2082, we had cases in pretty much every decoder that would leak rented memory when exception where thrown during decode.

  4. br3aker commented on Apr 10, 2022

    @br3aker
    ContributorAuthor

    Cool, thanks for fixing this and creating an API for testing! @tocsoft

  5. br3aker commented on Apr 12, 2022

    @br3aker
    ContributorAuthor

    Closed by #2082.

  6. added this to the 2.1.1 milestone on Apr 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions