Skip to content

Conversation

@JimBobSquarePants
Copy link
Member

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

Description

Fixes #2959

Note: Image from issue was tested locally. It's far too large at 14.1GB to add to references.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR prevents negative allocation attempts when processing huge TIFF files with uncompressed strips larger than Int32.MaxValue bytes. The changes address overflow issues in buffer size calculations and implement a fallback mechanism for handling extremely large strips.

Key Changes:

  • Modified CalculateStripBufferSize to return ulong instead of int to prevent overflow in size calculations
  • Added validation checks to prevent allocating buffers larger than Int32.MaxValue
  • Implemented row-by-row processing for uncompressed TIFF files with extremely large strips

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
TiffDecoderCore.cs Changed strip buffer size calculation to use ulong, added validation for Int32.MaxValue limits, and implemented fallback row-by-row processing for huge uncompressed strips
TiffBaseDecompressor.cs Renamed parameters for clarity and updated validation to check count against int.MaxValue instead of long.MaxValue

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

// 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.");
Copy link

Copilot AI Oct 24, 2025

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.

Suggested change
TiffThrowHelper.ThrowNotSupported("Strips larger than Int32.MaxValue bytes are not supported for compressed images.");
TiffThrowHelper.ThrowNotSupported("Strips larger than Int32.MaxValue bytes are not supported for uncompressed images.");

Copilot uses AI. Check for mistakes.
Comment on lines +547 to +549
int stripHeight = stripIndex < stripOffsets.Length - 1 || height % rowsPerStrip == 0
? rowsPerStrip
: height % rowsPerStrip;
Copy link

Copilot AI Oct 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] This strip height calculation logic is duplicated from the standard path below (around line 603). Consider extracting this into a helper method to reduce code duplication and improve maintainability.

Copilot uses AI. Check for mistakes.
@JimBobSquarePants JimBobSquarePants merged commit f758fad into main Oct 24, 2025
25 checks passed
@JimBobSquarePants JimBobSquarePants deleted the js/fix-2959 branch October 24, 2025 07:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Huge images cause negative allocation size error

2 participants