diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 2e8d1a629f..87e19c140e 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -53,6 +53,7 @@ The type attribute can be add,update,fix,remove. Fix int overflow in PCX image buffer sizing, now throws AllocationRequestException (#552). Fix int overflow in Allocator.array/arrayList size checks (#551). Add messages when throwing NullPointerException. + Fail on truncated uncompressed PSD image data instead of padding the image (#580). Add unit tests (#553). diff --git a/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/UncompressedDataReader.java b/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/UncompressedDataReader.java index 5bcd8c625e..7898257c32 100644 --- a/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/UncompressedDataReader.java +++ b/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/UncompressedDataReader.java @@ -17,6 +17,7 @@ package org.apache.commons.imaging.formats.psd.datareaders; import java.awt.image.BufferedImage; +import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.nio.ByteOrder; @@ -63,7 +64,9 @@ public void readData(final InputStream is, final BufferedImage bi, final PsdImag for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { final int b = bbis.readBits(depth); - + if (b < 0) { + throw new EOFException("PSD: Missing Image Data"); + } data[channel][y][x] = (byte) b; } } diff --git a/src/test/java/org/apache/commons/imaging/formats/psd/PsdReadTest.java b/src/test/java/org/apache/commons/imaging/formats/psd/PsdReadTest.java index 6ee7cb327c..8c6de0e5c3 100644 --- a/src/test/java/org/apache/commons/imaging/formats/psd/PsdReadTest.java +++ b/src/test/java/org/apache/commons/imaging/formats/psd/PsdReadTest.java @@ -18,9 +18,12 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.awt.image.BufferedImage; import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; import java.util.List; import org.apache.commons.imaging.ImageInfo; @@ -53,4 +56,20 @@ void test() throws Exception { } } + /** + * Tests that uncompressed image data shorter than the header declares is rejected. + */ + @Test + void testTruncatedUncompressedImageData() { + final ByteBuffer psd = ByteBuffer.allocate(41); + // Header: signature, version, reserved, channels, rows, columns, depth, mode (RGB) + psd.put(new byte[] { '8', 'B', 'P', 'S' }).putShort((short) 1).put(new byte[6]); + psd.putShort((short) 3).putInt(1000).putInt(1000).putShort((short) 8).putShort((short) 3); + // Empty color mode data, image resources, and layer and mask sections + psd.putInt(0).putInt(0).putInt(0); + // Image data: raw compression, then a single byte instead of 3 * 1000 * 1000 + psd.putShort((short) 0).put((byte) 0); + assertThrows(IOException.class, () -> Imaging.getBufferedImage(psd.array())); + } + }