#330 ImageReaderBase.getDestination now throws IIOException for too large dimension/size.

This commit is contained in:
Harald Kuhr 2020-11-17 23:07:09 +01:00
parent 918f92aba7
commit 0d5577a9a4
2 changed files with 34 additions and 21 deletions

View File

@ -30,20 +30,16 @@
package com.twelvemonkeys.imageio;
import com.twelvemonkeys.image.BufferedImageIcon;
import com.twelvemonkeys.image.ImageUtil;
import com.twelvemonkeys.imageio.util.IIOUtil;
import javax.imageio.*;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.stream.ImageInputStream;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.io.File;
@ -52,6 +48,20 @@ import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Iterator;
import javax.imageio.IIOException;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.stream.ImageInputStream;
import javax.swing.*;
import com.twelvemonkeys.image.BufferedImageIcon;
import com.twelvemonkeys.image.ImageUtil;
import com.twelvemonkeys.imageio.util.IIOUtil;
/**
* Abstract base class for image readers.
*
@ -314,12 +324,12 @@ public abstract class ImageReaderBase extends ImageReader {
long dimension = (long) destWidth * destHeight;
if (dimension > Integer.MAX_VALUE) {
throw new IllegalArgumentException(String.format("destination width * height > Integer.MAX_VALUE: %d", dimension));
throw new IIOException(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));
throw new IIOException(String.format("destination width * height * samplesPerPixel > Integer.MAX_VALUE: %d", size));
}
// Create a new image based on the type specifier

View File

@ -30,11 +30,11 @@
package com.twelvemonkeys.imageio;
import org.junit.Test;
import static java.util.Collections.singleton;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import javax.imageio.IIOException;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageTypeSpecifier;
import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
@ -44,8 +44,11 @@ import java.util.Collections;
import java.util.List;
import java.util.Set;
import static java.util.Collections.singleton;
import static org.junit.Assert.*;
import javax.imageio.IIOException;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageTypeSpecifier;
import org.junit.Test;
/**
* ImageReaderBaseTest
@ -212,19 +215,19 @@ public class ImageReaderBaseTest {
assertEquals(TYPES.get(0).getBufferedImageType(), destination.getType());
}
@Test(expected = IllegalArgumentException.class)
@Test(expected = IIOException.class)
public void testGetDestinationParamDestinationExceedsIntegerMax() throws IIOException {
ImageReadParam param = new ImageReadParam();
param.setSourceRegion(new Rectangle(3 * Short.MAX_VALUE, 2 * Short.MAX_VALUE)); // 6 442 057 734 pixels
ImageReaderBase.getDestination(param, TYPES.iterator(), 6 * Short.MAX_VALUE, 4 * Short.MAX_VALUE); // 25 768 230 936 pixels
}
@Test(expected = IllegalArgumentException.class)
@Test(expected = IIOException.class)
public void testGetDestinationDimensionExceedsIntegerMax() throws IIOException {
ImageReaderBase.getDestination(null, TYPES.iterator(), 3 * Short.MAX_VALUE, 2 * Short.MAX_VALUE); // 6 442 057 734 pixels
}
@Test(expected = IllegalArgumentException.class)
@Test(expected = IIOException.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