-
-
Notifications
You must be signed in to change notification settings - Fork 887
V4: Prevent negative allocation attempt for huge TIFF files #3003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| using System.Buffers; | ||
| using System.Runtime.CompilerServices; | ||
| using SixLabors.ImageSharp.Formats.Tiff.Compression; | ||
| using SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors; | ||
| using SixLabors.ImageSharp.Formats.Tiff.Constants; | ||
| using SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation; | ||
| using SixLabors.ImageSharp.IO; | ||
|
|
@@ -441,8 +442,14 @@ private void DecodeStripsPlanar<TPixel>( | |
| { | ||
| for (int stripIndex = 0; stripIndex < stripBuffers.Length; stripIndex++) | ||
| { | ||
| int uncompressedStripSize = this.CalculateStripBufferSize(width, rowsPerStrip, stripIndex); | ||
| stripBuffers[stripIndex] = this.memoryAllocator.Allocate<byte>(uncompressedStripSize); | ||
| ulong uncompressedStripSize = this.CalculateStripBufferSize(width, rowsPerStrip, stripIndex); | ||
|
|
||
| if (uncompressedStripSize > int.MaxValue) | ||
| { | ||
| TiffThrowHelper.ThrowNotSupported("Strips larger than Int32.MaxValue bytes are not supported for compressed images."); | ||
| } | ||
|
|
||
| stripBuffers[stripIndex] = this.memoryAllocator.Allocate<byte>((int)uncompressedStripSize); | ||
| } | ||
|
|
||
| using TiffBaseDecompressor decompressor = this.CreateDecompressor<TPixel>(width, bitsPerPixel, frame.Metadata); | ||
|
|
@@ -507,15 +514,83 @@ private void DecodeStripsChunky<TPixel>( | |
| rowsPerStrip = height; | ||
| } | ||
|
|
||
| int uncompressedStripSize = this.CalculateStripBufferSize(width, rowsPerStrip); | ||
| ulong uncompressedStripSize = this.CalculateStripBufferSize(width, rowsPerStrip); | ||
| int bitsPerPixel = this.BitsPerPixel; | ||
|
|
||
| using IMemoryOwner<byte> stripBuffer = this.memoryAllocator.Allocate<byte>(uncompressedStripSize, AllocationOptions.Clean); | ||
| Span<byte> stripBufferSpan = stripBuffer.GetSpan(); | ||
| Buffer2D<TPixel> pixels = frame.PixelBuffer; | ||
|
|
||
| using TiffBaseDecompressor decompressor = this.CreateDecompressor<TPixel>(width, bitsPerPixel, frame.Metadata); | ||
| TiffBaseColorDecoder<TPixel> colorDecoder = this.CreateChunkyColorDecoder<TPixel>(); | ||
| Buffer2D<TPixel> pixels = frame.PixelBuffer; | ||
|
|
||
| // There exists in this world TIFF files with uncompressed strips larger than Int32.MaxValue. | ||
| // We can read them, but we cannot allocate a buffer that large to hold the uncompressed data. | ||
| // In this scenario we fall back to reading and decoding one row at a time. | ||
| // | ||
| // The NoneTiffCompression decompressor can be used to read individual rows since we have | ||
| // a guarantee that each row required the same number of bytes. | ||
| if (decompressor is NoneTiffCompression none && uncompressedStripSize > int.MaxValue) | ||
| { | ||
| ulong bytesPerRowU = this.CalculateStripBufferSize(width, 1); | ||
|
|
||
| // This should never happen, but we check just to be sure. | ||
| if (bytesPerRowU > int.MaxValue) | ||
| { | ||
| TiffThrowHelper.ThrowNotSupported("Strips larger than Int32.MaxValue bytes are not supported for compressed images."); | ||
| } | ||
|
|
||
| int bytesPerRow = (int)bytesPerRowU; | ||
| using IMemoryOwner<byte> rowBufferOwner = this.memoryAllocator.Allocate<byte>(bytesPerRow, AllocationOptions.Clean); | ||
| Span<byte> rowBuffer = rowBufferOwner.GetSpan(); | ||
| for (int stripIndex = 0; stripIndex < stripOffsets.Length; stripIndex++) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| int stripHeight = stripIndex < stripOffsets.Length - 1 || height % rowsPerStrip == 0 | ||
| ? rowsPerStrip | ||
| : height % rowsPerStrip; | ||
|
Comment on lines
+547
to
+549
|
||
|
|
||
| int top = rowsPerStrip * stripIndex; | ||
| if (top + stripHeight > height) | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| ulong baseOffset = stripOffsets[stripIndex]; | ||
| ulong available = stripByteCounts[stripIndex]; | ||
| ulong required = (ulong)bytesPerRow * (ulong)stripHeight; | ||
| if (available < required) | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| for (int r = 0; r < stripHeight; r++) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| ulong rowOffset = baseOffset + ((ulong)r * (ulong)bytesPerRow); | ||
|
|
||
| // Use the NoneTiffCompression decompressor to read exactly one row. | ||
| none.Decompress( | ||
| this.inputStream, | ||
| rowOffset, | ||
| (ulong)bytesPerRow, | ||
| 1, | ||
| rowBuffer, | ||
| cancellationToken); | ||
|
|
||
| colorDecoder.Decode(rowBuffer, pixels, 0, top + r, width, 1); | ||
| } | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (uncompressedStripSize > int.MaxValue) | ||
| { | ||
| TiffThrowHelper.ThrowNotSupported("Strips larger than Int32.MaxValue bytes are not supported for compressed images."); | ||
| } | ||
|
|
||
| using IMemoryOwner<byte> stripBuffer = this.memoryAllocator.Allocate<byte>((int)uncompressedStripSize, AllocationOptions.Clean); | ||
| Span<byte> stripBufferSpan = stripBuffer.GetSpan(); | ||
|
|
||
| for (int stripIndex = 0; stripIndex < stripOffsets.Length; stripIndex++) | ||
| { | ||
|
|
@@ -808,7 +883,7 @@ private IMemoryOwner<ulong> ConvertNumbers(Array array, out Span<ulong> span) | |
| /// <param name="height">The height for the desired pixel buffer.</param> | ||
| /// <param name="plane">The index of the plane for planar image configuration (or zero for chunky).</param> | ||
| /// <returns>The size (in bytes) of the required pixel buffer.</returns> | ||
| private int CalculateStripBufferSize(int width, int height, int plane = -1) | ||
| private ulong CalculateStripBufferSize(int width, int height, int plane = -1) | ||
| { | ||
| DebugGuard.MustBeLessThanOrEqualTo(plane, 3, nameof(plane)); | ||
|
|
||
|
|
@@ -841,8 +916,8 @@ private int CalculateStripBufferSize(int width, int height, int plane = -1) | |
| } | ||
| } | ||
|
|
||
| int bytesPerRow = ((width * bitsPerPixel) + 7) / 8; | ||
| return bytesPerRow * height; | ||
| ulong bytesPerRow = (((ulong)width * (ulong)bitsPerPixel) + 7) / 8; | ||
| return bytesPerRow * (ulong)height; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message states 'compressed images' but this check is within a code path specifically handling uncompressed images (NoneTiffCompression). The message should say 'uncompressed images' or 'rows' to accurately reflect the context.