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-397" type="fix" dev="pkarwasz" due-to="Piotr P. Karwasz, Gary Gregory">Fail on truncated uncompressed PSD image data instead of padding the image (#580).</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 @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
}

}
Loading