From a17018d489e1f6848bb9dcc5fdb4c13e8143acdf Mon Sep 17 00:00:00 2001 From: "Piotr P. Karwasz" Date: Mon, 30 Nov 2026 12:09:17 +0100 Subject: [PATCH 1/2] IMAGING-397: Fail on truncated uncompressed PSD image data UncompressedDataReader read channels x rows x columns samples from the image data section without checking for the end of the stream, filling a truncated image with 0xFF samples. A file declaring more pixels than it contains was decoded as if it were complete. Throw an EOFException when the image data ends early, as CompressedDataReader already does for missing scanlines. Assisted-By: Claude Opus 5.5 --- src/changes/changes.xml | 1 + .../datareaders/UncompressedDataReader.java | 5 ++++- .../imaging/formats/psd/PsdReadTest.java | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 2e8d1a629f..cf15e3cb3a 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. 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())); + } + } From 908e38dfc748f7006c315ff8da73be1aecc14eba Mon Sep 17 00:00:00 2001 From: "Piotr P. Karwasz" Date: Mon, 30 Nov 2026 13:55:51 +0100 Subject: [PATCH 2/2] Add the pull request number to the changes.xml entry Assisted-By: Claude Opus 5.5 --- src/changes/changes.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index cf15e3cb3a..87e19c140e 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -53,7 +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. + Fail on truncated uncompressed PSD image data instead of padding the image (#580). Add unit tests (#553).