Skip to content

Huge images cause negative allocation size error #2959

@mfeemster

Description

@mfeemster

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

Latest as of 6/25/2025

Other ImageSharp packages and versions

None

Environment (Operating system, version and so on)

Windows 10

.NET Framework version

.NET 9

Description

I am trying to decode this extremely large 14GB TIF file: https://rubinobservatory.org/gallery/collections/first-look-gallery/mlis3sriah6pn6nfr5ecp46h3i

and get an error: "Failed to allocate buffers for possibly degenerate dimensions: 0x0."

My machine has 64GB RAM and I set the buffer sizes to 40,000 (40GB) and it still happens.

From the stack trace below, it appears that when an image gets sufficiently large, the buffer sizes overflow and wrap to negative, thus throwing an exception. Possibly related to:

#2874

and also noted in:

#2870

try
{
	Configuration.Default.MemoryAllocator = MemoryAllocator.Create(new MemoryAllocatorOptions()
	{
		MaximumPoolSizeMegabytes = 20000,
		AllocationLimitMegabytes = 20000,
	});

	var filename = @"D:\Temp Downloads\Vera Rubin Cosmic Treasure Chest Im1.tif";
	var image = SixLabors.ImageSharp.Image.Load<Rgba32>(filename);
}
catch (Exception ex)
{
	Console.WriteLine(ex);
}

It threw an exception with this stack trace:

SixLabors.ImageSharp.InvalidImageContentException: Cannot decode image. Failed to allocate buffers for possibly degenerate dimensions: 0x0.
 ---> SixLabors.ImageSharp.Memory.InvalidMemoryOperationException: Attempted to allocate a buffer of negative length=-2037097840.
   at SixLabors.ImageSharp.Memory.InvalidMemoryOperationException.ThrowNegativeAllocationException(Int64 length) in D:\Dev\ImageSharpTest\ImageSharp\src\ImageSharp\Memory\InvalidMemoryOperationException.cs:line 32
   at SixLabors.ImageSharp.Memory.UniformUnmanagedMemoryPoolMemoryAllocator.Allocate[T](Int32 length, AllocationOptions options) in D:\Dev\ImageSharpTest\ImageSharp\src\ImageSharp\Memory\Allocators\UniformUnmanagedMemoryPoolMemoryAllocator.cs:line 88
   at SixLabors.ImageSharp.Formats.Tiff.TiffDecoderCore.DecodeStripsChunky[TPixel](ImageFrame`1 frame, Int32 width, Int32 height, Int32 rowsPerStrip, Span`1 stripOffsets, Span`1 stripByteCounts, CancellationToken cancellationToken) in D:\Dev\ImageSharpTest\ImageSharp\src\ImageSharp\Formats\Tiff\TiffDecoderCore.cs:line 513
   at SixLabors.ImageSharp.Formats.Tiff.TiffDecoderCore.DecodeImageWithStrips[TPixel](ExifProfile tags, ImageFrame`1 frame, Int32 width, Int32 height, CancellationToken cancellationToken) in D:\Dev\ImageSharpTest\ImageSharp\src\ImageSharp\Formats\Tiff\TiffDecoderCore.cs:line 339
   at SixLabors.ImageSharp.Formats.Tiff.TiffDecoderCore.DecodeFrame[TPixel](ExifProfile tags, Nullable`1 size, CancellationToken cancellationToken) in D:\Dev\ImageSharpTest\ImageSharp\src\ImageSharp\Formats\Tiff\TiffDecoderCore.cs:line 272
   at SixLabors.ImageSharp.Formats.Tiff.TiffDecoderCore.Decode[TPixel](BufferedReadStream stream, CancellationToken cancellationToken) in D:\Dev\ImageSharpTest\ImageSharp\src\ImageSharp\Formats\Tiff\TiffDecoderCore.cs:line 176
   at SixLabors.ImageSharp.Formats.ImageDecoderCore.Decode[TPixel](Configuration configuration, Stream stream, CancellationToken cancellationToken) in D:\Dev\ImageSharpTest\ImageSharp\src\ImageSharp\Formats\ImageDecoderCore.cs:line 82
   --- End of inner exception stack trace ---
   at SixLabors.ImageSharp.Formats.ImageDecoderCore.Decode[TPixel](Configuration configuration, Stream stream, CancellationToken cancellationToken) in D:\Dev\ImageSharpTest\ImageSharp\src\ImageSharp\Formats\ImageDecoderCore.cs:line 86
   at SixLabors.ImageSharp.Formats.Tiff.TiffDecoder.Decode[TPixel](DecoderOptions options, Stream stream, CancellationToken cancellationToken) in D:\Dev\ImageSharpTest\ImageSharp\src\ImageSharp\Formats\Tiff\TiffDecoder.cs:line 38
   at SixLabors.ImageSharp.Formats.ImageDecoder.<>c__DisplayClass0_0`1.<Decode>b__0(Stream s) in D:\Dev\ImageSharpTest\ImageSharp\src\ImageSharp\Formats\ImageDecoder.cs:line 24
   at SixLabors.ImageSharp.Formats.ImageDecoder.<WithSeekableStream>g__PerformActionAndResetPosition|11_0[T](Stream s, Int64 position, <>c__DisplayClass11_0`1&) in D:\Dev\ImageSharpTest\ImageSharp\src\ImageSharp\Formats\ImageDecoder.cs:line 201
   at SixLabors.ImageSharp.Formats.ImageDecoder.WithSeekableStream[T](DecoderOptions options, Stream stream, Func`2 action)
   at SixLabors.ImageSharp.Formats.ImageDecoder.Decode[TPixel](DecoderOptions options, Stream stream) in D:\Dev\ImageSharpTest\ImageSharp\src\ImageSharp\Formats\ImageDecoder.cs:line 21
   at SixLabors.ImageSharp.Image.Decode[TPixel](DecoderOptions options, Stream stream) in D:\Dev\ImageSharpTest\ImageSharp\src\ImageSharp\Image.Decode.cs:line 185
   at SixLabors.ImageSharp.Image.<>c__DisplayClass91_0`1.<Load>b__0(Stream s) in D:\Dev\ImageSharpTest\ImageSharp\src\ImageSharp\Image.FromStream.cs:line 234
   at SixLabors.ImageSharp.Image.WithSeekableStream[T](DecoderOptions options, Stream stream, Func`2 action) in D:\Dev\ImageSharpTest\ImageSharp\src\ImageSharp\Image.FromStream.cs:line 301
   at SixLabors.ImageSharp.Image.Load[TPixel](DecoderOptions options, Stream stream) in D:\Dev\ImageSharpTest\ImageSharp\src\ImageSharp\Image.FromStream.cs:line 234
   at SixLabors.ImageSharp.Image.Load[TPixel](DecoderOptions options, String path) in D:\Dev\ImageSharpTest\ImageSharp\src\ImageSharp\Image.FromFile.cs:line 254
   at SixLabors.ImageSharp.Image.Load[TPixel](String path) in D:\Dev\ImageSharpTest\ImageSharp\src\ImageSharp\Image.FromFile.cs:line 233
   at SixLabors.ImageSharp.Tests.ProfilingSandbox.Program.Main(String[] args) in D:\Dev\ImageSharpTest\ImageSharp\tests\ImageSharp.Tests.ProfilingSandbox\Program.cs:line 43

Steps to Reproduce

Download the file linked in the description.

Run the code in the description on a machine with sufficient RAM.

See the exception get thrown.

Images

No response

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