Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="alhuda, Gary Gregory">Fix int overflow in PCX image buffer sizing, now throws AllocationRequestException (#552).</action>
<action type="fix" dev="ggregory" due-to="alhuda, Gary Gregory">Fix int overflow in Allocator.array/arrayList size checks (#551).</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Add messages when throwing NullPointerException.</action>
<action issue="IMAGING-403" type="fix" dev="pkarwasz" due-to="Piotr P. Karwasz, Gary Gregory">Fix int overflow in GIF frame and XPM raster sizing, now throws AllocationRequestException (#585).</action>
<!-- ADD -->
<action type="fix" dev="ggregory" due-to="Sean R, Gary Gregory">Add unit tests (#553).</action>
<!-- UPDATE -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ private ImageDescriptor readImageDescriptor(final GifHeaderInfo ghi, final int b
final byte[] bytes = block.appendSubBlocks();
final InputStream bais = new ByteArrayInputStream(bytes);

final int size = imageWidth * imageHeight;
final int size = Allocator.check((long) imageWidth * imageHeight, Byte.BYTES);
final MyLzwDecompressor myLzwDecompressor = new MyLzwDecompressor(lzwMinimumCodeSize, ByteOrder.LITTLE_ENDIAN, false);
imageData = myLzwDecompressor.decompress(bais, size);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,14 +534,8 @@ private BufferedImage readXpmImage(final XpmHeader xpmHeader, final BasicCParser
palette[paletteEntry.index] = paletteEntry.getBestArgb();
}
colorModel = new IndexColorModel(8, xpmHeader.palette.size(), palette, 0, true, -1, DataBuffer.TYPE_BYTE);
// Check allocation
final int bands = 1;
final int scanlineStride = xpmHeader.width * bands;
final int pixelStride = bands;
final int size = scanlineStride * (xpmHeader.height - 1) + // first (h - 1) scans
pixelStride * xpmHeader.width; // last scan
Allocator.check(Byte.SIZE, size);
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, xpmHeader.width, xpmHeader.height, bands, null);
Allocator.check((long) xpmHeader.width * xpmHeader.height, Byte.BYTES);
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, xpmHeader.width, xpmHeader.height, 1, null);
bpp = 8;
} else if (xpmHeader.palette.size() <= 1 << 16) {
final int[] palette = Allocator.intArray(xpmHeader.palette.size());
Expand All @@ -550,18 +544,12 @@ private BufferedImage readXpmImage(final XpmHeader xpmHeader, final BasicCParser
palette[paletteEntry.index] = paletteEntry.getBestArgb();
}
colorModel = new IndexColorModel(16, xpmHeader.palette.size(), palette, 0, true, -1, DataBuffer.TYPE_USHORT);
// Check allocation
final int bands = 1;
final int scanlineStride = xpmHeader.width * bands;
final int pixelStride = bands;
final int size = scanlineStride * (xpmHeader.height - 1) + // first (h - 1) scans
pixelStride * xpmHeader.width; // last scan
Allocator.check(Short.SIZE, size);
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_USHORT, xpmHeader.width, xpmHeader.height, bands, null);
Allocator.check((long) xpmHeader.width * xpmHeader.height, Short.BYTES);
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_USHORT, xpmHeader.width, xpmHeader.height, 1, null);
bpp = 16;
} else {
colorModel = new DirectColorModel(32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
Allocator.check(Integer.SIZE, xpmHeader.width * xpmHeader.height);
Allocator.check((long) xpmHeader.width * xpmHeader.height, Integer.BYTES);
raster = Raster.createPackedRaster(DataBuffer.TYPE_INT, xpmHeader.width, xpmHeader.height,
new int[] { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, null);
bpp = 32;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Stream;

import org.apache.commons.imaging.ImageInfo;
import org.apache.commons.imaging.Imaging;
import org.apache.commons.imaging.ImagingException;
import org.apache.commons.imaging.bytesource.ByteSource;
import org.apache.commons.imaging.common.AllocationRequestException;
import org.apache.commons.imaging.common.ImageMetadata;
import org.apache.commons.imaging.test.TestResources;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -108,6 +112,21 @@ void testCreateMetadataWithDisposalMethods() {
}
}

/**
* Tests that the size of a 65535 x 65535 frame, which overflows int, is rejected by the allocation check.
*/
@Test
void testFrameSizeExceedsAllocationLimit() {
final ByteBuffer gif = ByteBuffer.allocate(28).order(ByteOrder.LITTLE_ENDIAN);
// Header and logical screen descriptor, without global color table
gif.put("GIF89a".getBytes(StandardCharsets.US_ASCII)).putShort((short) 0xFFFF).putShort((short) 0xFFFF).put((byte) 0).put((byte) 0).put((byte) 0);
// Image descriptor: position (0, 0), size 65535 x 65535, no local color table
gif.put((byte) 0x2C).putShort((short) 0).putShort((short) 0).putShort((short) 0xFFFF).putShort((short) 0xFFFF).put((byte) 0);
// LZW minimum code size, one data sub-block, block terminator and trailer
gif.put((byte) 2).put((byte) 1).put((byte) 0x44).put((byte) 0).put((byte) 0x3B);
assertThrows(AllocationRequestException.class, () -> Imaging.getBufferedImage(gif.array()));
}

@ParameterizedTest
@MethodSource("data")
void testImageDimensions(final File imageFile) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,49 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;

import org.apache.commons.imaging.bytesource.ByteSource;
import org.apache.commons.imaging.common.AllocationRequestException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class XpmParserTest {

private static final String PIXEL_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

/**
* Builds the header and palette of a 65535 x 65535 XPM image with the given number of colors and 3 characters per pixel, without pixel data.
*/
private static byte[] hugeXpm(final int numColors) {
final StringBuilder xpm = new StringBuilder("/* XPM */\nstatic char *image[] = {\n\"65535 65535 ").append(numColors).append(" 3\",\n");
final int base = PIXEL_CHARS.length();
for (int i = 0; i < numColors; i++) {
xpm.append('"').append(PIXEL_CHARS.charAt(i / (base * base))).append(PIXEL_CHARS.charAt(i / base % base)).append(PIXEL_CHARS.charAt(i % base))
.append(" c #000000\",\n");
}
return xpm.append("};\n").toString().getBytes(StandardCharsets.US_ASCII);
}

/**
* Tests that the 65535 x 65535 raster, whose size overflows int, is rejected by the allocation check for 8-bit, 16-bit and 32-bit images.
*/
@ParameterizedTest
@ValueSource(ints = { 1, 257, 65_537 })
void testImageSizeExceedsAllocationLimit(final int numColors) {
final byte[] xpm = hugeXpm(numColors);
assertThrows(AllocationRequestException.class, () -> new XpmImageParser().getBufferedImage(ByteSource.array(xpm), new XpmImagingParameters()));
}

@Test
public void testPaletteEntry() throws Exception {
final Class<?> entryClass = Class.forName("org.apache.commons.imaging.formats.xpm.XpmImageParser$PaletteEntry");
Expand Down
Loading