#398: Better exception message when BufferedImage size > Integer.MAX

This commit is contained in:
Harald Kuhr 2019-08-09 19:52:07 +02:00
parent b0b5de5fa4
commit 9ce1a86cae
2 changed files with 18 additions and 3 deletions

View File

@ -310,8 +310,14 @@ public abstract class ImageReaderBase extends ImageReader {
int destWidth = destRegion.x + destRegion.width; int destWidth = destRegion.x + destRegion.width;
int destHeight = destRegion.y + destRegion.height; int destHeight = destRegion.y + destRegion.height;
if ((long) destWidth * destHeight > Integer.MAX_VALUE) { long dimension = (long) destWidth * destHeight;
throw new IllegalArgumentException(String.format("destination width * height > Integer.MAX_VALUE: %d", (long) destWidth * destHeight)); if (dimension > Integer.MAX_VALUE) {
throw new IllegalArgumentException(String.format("destination width * height > Integer.MAX_VALUE: %d", dimension));
}
long size = dimension * imageType.getSampleModel().getNumDataElements();
if (size > Integer.MAX_VALUE) {
throw new IllegalArgumentException(String.format("destination width * height * samplesPerPixel > Integer.MAX_VALUE: %d", size));
} }
// Create a new image based on the type specifier // Create a new image based on the type specifier

View File

@ -42,7 +42,9 @@ import java.awt.image.DataBuffer;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set;
import static java.util.Collections.singleton;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/** /**
@ -218,10 +220,17 @@ public class ImageReaderBaseTest {
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testGetDestinationExceedsIntegerMax() throws IIOException { public void testGetDestinationDimensionExceedsIntegerMax() throws IIOException {
ImageReaderBase.getDestination(null, TYPES.iterator(), 3 * Short.MAX_VALUE, 2 * Short.MAX_VALUE); // 6 442 057 734 pixels ImageReaderBase.getDestination(null, TYPES.iterator(), 3 * Short.MAX_VALUE, 2 * Short.MAX_VALUE); // 6 442 057 734 pixels
} }
@Test(expected = IllegalArgumentException.class)
public void testGetDestinationStorageExceedsIntegerMax() throws IIOException {
Set<ImageTypeSpecifier> byteTypes = singleton(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_3BYTE_BGR));
ImageReaderBase.getDestination(null, byteTypes.iterator(), Short.MAX_VALUE, Short.MAX_VALUE); // 1 073 676 289 pixels
// => 3 221 028 867 bytes needed in continuous array, not possible
}
@Test @Test
public void testHasExplicitDestinationNull() { public void testHasExplicitDestinationNull() {
assertFalse(ImageReaderBase.hasExplicitDestination(null)); assertFalse(ImageReaderBase.hasExplicitDestination(null));