Skip to content

IMAGING-403: Fix int overflow in GIF frame and XPM raster sizing - #585

Open
ppkarwasz wants to merge 2 commits into
apache:masterfrom
ppkarwasz:fix/IMAGING-403-gif-xpm-overflow
Open

ppkarwasz wants to merge 2 commits into
apache:masterfrom
ppkarwasz:fix/IMAGING-403-gif-xpm-overflow

Conversation

@ppkarwasz

Copy link
Copy Markdown
Member

Fixes IMAGING-403.

Two decoders multiplied the image dimensions in int before the allocation check:

  • GifImageParser.readImageDescriptor computed imageWidth * imageHeight and passed it to the LZW decompressor. For a 65535 × 65535 frame the size was -131071 and new ByteArrayOutputStream(-131071) threw IllegalArgumentException.
  • XpmImageParser.readXpmImage called Allocator.check(Integer.SIZE, width * height) (and Byte.SIZE / Short.SIZE for 8- and 16-bit images): arguments swapped, element size in bits, product in int. For 65535 × 65535 the check passed and creating the raster threw IllegalArgumentException or NegativeArraySizeException.

Both now compute the pixel count as a long and call Allocator.check(long, int) with the element size in bytes, so these inputs throw AllocationRequestException, as other decoders do. For 8- and 16-bit XPM images the check was previously 8 times stricter than intended (bits instead of bytes); it now matches the other formats.

The XPM part is the same change as in #401 (Marcono1234), which also covers many other call sites and has not been merged; this PR only fixes these two decoders.

Tests: GifReadTest.testFrameSizeExceedsAllocationLimit (28-byte GIF) and XpmParserTest.testImageSizeExceedsAllocationLimit (8-, 16- and 32-bit XPM headers); all fail without the change.

  • Read the contribution guidelines.
  • Read the ASF Generative Tooling Guidance.
  • I used AI to create any part of, or all of, this pull request: Claude Code (Claude Opus 5.5) helped analyze the decoders, draft the change and tests, and write this description; I reviewed and verified everything.
  • Run a successful build using the default Maven goal with mvn.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request has a meaningful subject line and body.

GifImageParser computed the frame size as imageWidth * imageHeight in
int and XpmImageParser checked the raster size with
Allocator.check(Integer.SIZE, width * height): arguments swapped, a bit
count as element size and an int product. Dimensions of 65535 x 65535
overflowed to a negative size that the check accepted, so decoding
failed with IllegalArgumentException or NegativeArraySizeException.

Compute the pixel count as a long and check it with
Allocator.check(long, int) and the element size in bytes, so both cases
throw AllocationRequestException.

Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant