#329 JPEGImageReader endless loop fix

This commit is contained in:
Harald Kuhr
2018-05-22 21:10:57 +02:00
parent b32a38bf02
commit 7ab72f0161
14 changed files with 251 additions and 30 deletions
@@ -119,8 +119,15 @@ public class JPEGImageReaderTest extends ImageReaderAbstractTest<JPEGImageReader
new TestData(getClassLoaderResource("/broken-jpeg/broken-invalid-adobe-ycc-gray.jpg"), new Dimension(11, 440)), // Image readable, broken metadata (fixable?)
new TestData(getClassLoaderResource("/broken-jpeg/broken-no-sof-ascii-transfer-mode.jpg"), new Dimension(-1, -1)), // Unreadable, can't find SOFn marker
new TestData(getClassLoaderResource("/broken-jpeg/broken-sos-before-sof.jpg"), new Dimension(-1, -1)), // Unreadable, can't find SOFn marker
new TestData(getClassLoaderResource("/broken-jpeg/broken-adobe-segment-length-beyond-eof.jpg"), new Dimension(-1, -1)) // Unreadable, no EOI
);
new TestData(getClassLoaderResource("/broken-jpeg/broken-adobe-segment-length-beyond-eof.jpg"), new Dimension(-1, -1)), // Unreadable, no EOI
new TestData(getClassLoaderResource("/broken-jpeg/513f29d0-02a8-11e7-9756-6035edb96e79.jpg"), new Dimension(-1, -1)),
new TestData(getClassLoaderResource("/broken-jpeg/51432b02-02a8-11e7-9203-b42b1c43c0c3.jpg"), new Dimension(-1, -1)),
new TestData(getClassLoaderResource("/broken-jpeg/5145e95a-02a8-11e7-8372-4787a7307ab8.jpg"), new Dimension(-1, -1)),
new TestData(getClassLoaderResource("/broken-jpeg/514b20dc-02a8-11e7-92c6-d4fed7b4ebb1.jpg"), new Dimension(-1, -1)),
new TestData(getClassLoaderResource("/broken-jpeg/514c48ea-02a8-11e7-8789-bb75321f404f.jpg"), new Dimension(-1, -1)),
new TestData(getClassLoaderResource("/broken-jpeg/514e4122-02a8-11e7-8c03-0830d60cd585.jpg"), new Dimension(-1, -1)),
new TestData(getClassLoaderResource("/broken-jpeg/513f29d0-02a8-11e7-9756-6035edb96e79.jpg"), new Dimension(-1, -1))
);
// More test data in specific tests below
}
@@ -446,7 +453,7 @@ public class JPEGImageReaderTest extends ImageReaderAbstractTest<JPEGImageReader
@Test
public void testBrokenReadRasterAfterGetMetadataException() throws IOException {
// See issue 107, from PDFBox team
// See issue #107, from PDFBox team
JPEGImageReader reader = createReader();
try {
@@ -458,7 +465,6 @@ public class JPEGImageReaderTest extends ImageReaderAbstractTest<JPEGImageReader
}
catch (IOException ignore) {
// Expected IOException here, due to broken file
// ignore.printStackTrace();
}
try {
@@ -477,6 +483,122 @@ public class JPEGImageReaderTest extends ImageReaderAbstractTest<JPEGImageReader
}
}
@Test
public void testSPIRecognizesBrokenJPEG() throws IOException {
// TODO: There's a bug in com.sun.imageio.plugins.png.PNGImageReaderSpi.canDecode
// causing files < 8 bytes to not be recognized as anything...
ImageReaderSpi provider = createProvider();
for (TestData data : getBrokenTestData()) {
assertTrue(data.toString(), provider.canDecodeInput(data.getInputStream()));
}
}
// TODO: Consider wrapping the delegate in JPEGImageReader with methods that don't throw
// runtime exceptions, and instead throw IIOException?
@Test
public void testBrokenGetRawImageType() throws IOException {
JPEGImageReader reader = createReader();
try {
for (TestData broken : getBrokenTestData()) {
reader.setInput(broken.getInputStream());
try {
reader.getRawImageType(0);
}
catch (IIOException expected) {
assertNotNull(expected.getMessage());
}
catch (IOException expected) {
if (!(expected instanceof EOFException)) {
assertNotNull(expected.getMessage());
}
}
}
}
finally {
reader.dispose();
}
}
@Test(timeout = 200)
public void testBrokenGetRawImageTypeIgnoreMetadata() throws IOException {
JPEGImageReader reader = createReader();
try {
for (TestData broken : getBrokenTestData()) {
reader.setInput(broken.getInputStream(), true, true);
try {
reader.getRawImageType(0);
}
catch (IIOException expected) {
assertNotNull(expected.getMessage());
}
catch (IOException expected) {
if (!(expected instanceof EOFException)) {
assertNotNull(expected.getMessage());
}
}
}
}
finally {
reader.dispose();
}
}
@Test
public void testBrokenGetImageTypes() throws IOException {
JPEGImageReader reader = createReader();
try {
for (TestData broken : getBrokenTestData()) {
reader.setInput(broken.getInputStream());
try {
reader.getImageTypes(0);
}
catch (IIOException expected) {
assertNotNull(expected.getMessage());
}
catch (IOException expected) {
if (!(expected instanceof EOFException)) {
assertNotNull(expected.getMessage());
}
}
}
}
finally {
reader.dispose();
}
}
@Test(timeout = 200)
public void testBrokenGetImageTypesIgnoreMetadata() throws IOException {
JPEGImageReader reader = createReader();
try {
for (TestData broken : getBrokenTestData()) {
reader.setInput(broken.getInputStream(), true, true);
try {
reader.getImageTypes(0);
}
catch (IIOException expected) {
assertNotNull(expected.getMessage());
}
catch (IOException expected) {
if (!(expected instanceof EOFException)) {
assertNotNull(expected.getMessage());
}
}
}
}
finally {
reader.dispose();
}
}
@Test
public void testBrokenRead() throws IOException {
JPEGImageReader reader = createReader();
@@ -172,6 +172,32 @@ public class JPEGSegmentImageInputStreamTest {
assertEquals(1061L, length); // Sanity check: same as file size, except padding and the filtered ICC_PROFILE segment
}
@Test
public void testEOFExceptionInSegmentParsingShouldNotCreateBadState2() throws IOException {
ImageInputStream iis = new JPEGSegmentImageInputStream(ImageIO.createImageInputStream(getClassLoaderResource("/broken-jpeg/51432b02-02a8-11e7-9203-b42b1c43c0c3.jpg")));
byte[] buffer = new byte[4096];
// NOTE: This is a simulation of how the native parts of com.sun...JPEGImageReader would read the image...
assertEquals(2, iis.read(buffer, 0, buffer.length));
assertEquals(2, iis.getStreamPosition());
iis.seek(2000); // Just a random postion beyond EOF
assertEquals(2000, iis.getStreamPosition());
// So far, so good (but stream position is now really beyond EOF)...
// This however, will blow up with an EOFException internally (but we'll return -1 to be good)
assertEquals(-1, iis.read(buffer, 0, buffer.length));
assertEquals(-1, iis.read());
assertEquals(2000, iis.getStreamPosition());
// Again, should just continue returning -1 for ever
assertEquals(-1, iis.read());
assertEquals(-1, iis.read(buffer, 0, buffer.length));
assertEquals(2000, iis.getStreamPosition());
}
@Test
public void testEOFExceptionInSegmentParsingShouldNotCreateBadState() throws IOException {
ImageInputStream iis = new JPEGSegmentImageInputStream(ImageIO.createImageInputStream(getClassLoaderResource("/broken-jpeg/broken-no-sof-ascii-transfer-mode.jpg")));
@@ -189,10 +215,12 @@ public class JPEGSegmentImageInputStreamTest {
// This however, will blow up with an EOFException internally (but we'll return -1 to be good)
assertEquals(-1, iis.read(buffer, 0, buffer.length));
assertEquals(-1, iis.read());
assertEquals(0x2012, iis.getStreamPosition());
// Again, should just continue returning -1 for ever
assertEquals(-1, iis.read(buffer, 0, buffer.length));
assertEquals(-1, iis.read());
assertEquals(0x2012, iis.getStreamPosition());
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 232 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB