Repository navigation
[IMAGING-385] Fix int overflow in ImageBuilder pixel array sizing - #559
Open
sharanggupta wants to merge 1 commit into
Open
sharanggupta wants to merge 1 commit into
sharanggupta wants to merge 1 commit into
Conversation
ImageBuilder allocated its pixel array with Allocator.intArray(width * height). The product is an int, so a 2359300 x 65538 image wrapped to 4,980,744 and slipped past the allocation limit, and getBufferedImage() then failed in the JDK with an IllegalArgumentException from Raster.createPackedRaster. Add Allocator.intArray(long), next to the existing byteArray(long), and use it with (long) width * height, like the PCX fix (apache#552). A pixel count that does not fit an int or exceeds the limit is now rejected with an AllocationRequestException.
7 tasks done
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes IMAGING-385.
What
new ImageBuilder(width, height, ...)sized its pixel array withAllocator.intArray(width * height). The product is anint, so for large dimensions it wraps. The ticket's BMP is 2359300 x 65538: the product is 154,623,803,400, which wraps to 4,980,744.Allocatorthen accepts a 19 MB array, but the builder keeps the original width and height, andImaging.getBufferedImagefails later inRaster.createPackedRasterwith a JDKIllegalArgumentException("Dimensions ... are too large"), which is not one of the documentedImagingException/IOException.How
The same class of bug was fixed for PCX in #552 by computing the size as a
longand handing it to the long overload of theAllocator. There is nolongoverload forintarrays yet, so this addsAllocator.intArray(long)next to the existingbyteArray(long)(it usescheck(long, int), so a request that does not fit anintor exceeds the limit throwsAllocationRequestException) andImageBuildernow calls it with(long) width * height. The only behaviour change is for sizes that used to wrap: they are now rejected up front with anAllocationRequestException, as in the PCX fix. All other callers ofintArray(int)resolve to the existing overload.I did not touch the other sizing overflows in the 38x tickets (PNG scanline size in IMAGING-388, TIFF strip length in IMAGING-387, and
jpeg.decoder.Blockhas the samewidth * heightpattern); they are separate code paths and I wanted to keep this one reviewable. Happy to look at them in follow-ups if you want.Tests
ImageBuilderTest.testConstructorDimensionsOverflowingInt: the constructor rejects 2359300 x 65538.BmpImageParserTest.testDimensionsOverflowingInt: the 90-byte file attached to the ticket (added asimages/bmp/IMAGING-385/crash-e506...bmp, next to the existing IMAGING-325 one) now givesAllocationRequestExceptioninstead ofIllegalArgumentException.AllocatorTest: the long overload rejects an overflowing request and allocates a normal one.Without the change the first two fail (nothing thrown /
IllegalArgumentException); with it all tests pass. Verified locally with the defaultmvngoal on Java 17 (RAT, Checkstyle, SpotBugs, PMD, Javadoc).Before you push a pull request, review this list:
mvn; that'smvnon the command line by itself.