You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ki...@apache.org on 2019/05/16 02:15:01 UTC

[commons-imaging] branch master updated: IMAGING-211: Fix boolean logic to produce correct error for invalid PNG chunk size

This is an automated email from the ASF dual-hosted git repository.

kinow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-imaging.git


The following commit(s) were added to refs/heads/master by this push:
     new f55035a  IMAGING-211: Fix boolean logic to produce correct error for invalid PNG chunk size
     new c4eeebe  Merge pull request #42 from kinow/IMAGING-211
f55035a is described below

commit f55035ac7fb451e85e3139c959a25f47f4ffd04a
Author: Bruno P. Kinoshita <ki...@users.noreply.github.com>
AuthorDate: Thu May 16 14:04:40 2019 +1200

    IMAGING-211: Fix boolean logic to produce correct error for invalid PNG chunk size
---
 src/changes/changes.xml                            |   3 ++
 .../imaging/formats/png/chunks/PngChunkIhdr.java   |   2 +-
 .../png/PngWithInvalidPngChunkSizeTest.java        |  53 +++++++++++++++++++++
 src/test/resources/IMAGING-211/testfile_2.png      | Bin 0 -> 35144 bytes
 4 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 0070c41..6592068 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -51,6 +51,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action issue="IMAGING-167" dev="kinow" type="fix" due-to="Michael Groß">
         Possible infinite loop at XpmImageParser::writeImage
       </action>
+      <action issue="IMAGING-211" dev="kinow" type="fix">
+      	Imaging.getBufferedImage fails throwing java.lang.ArrayIndexOutOfBoundsException for specific inputs
+      </action>
     </release>
     <release version="1.0-alpha1" date="2019-04-28" description="First 1.0 alpha release">
       <action issue="IMAGING-199" dev="kinow" type="fix" due-to="Ric Emery">
diff --git a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkIhdr.java b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkIhdr.java
index f8ad532..614b12d 100644
--- a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkIhdr.java
+++ b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkIhdr.java
@@ -50,7 +50,7 @@ public class PngChunkIhdr extends PngChunk {
         compressionMethod = readByte("CompressionMethod", is, "Not a Valid Png File: IHDR Corrupt");
         filterMethod = readByte("FilterMethod", is, "Not a Valid Png File: IHDR Corrupt");
         final int method = readByte("InterlaceMethod", is, "Not a Valid Png File: IHDR Corrupt");
-        if (method < 0 && method >= InterlaceMethod.values().length) {
+        if (method < 0 || method >= InterlaceMethod.values().length) {
             throw new ImageReadException("PNG: unknown interlace method: " + method);
         }
         interlaceMethod = InterlaceMethod.values()[method];
diff --git a/src/test/java/org/apache/commons/imaging/formats/png/PngWithInvalidPngChunkSizeTest.java b/src/test/java/org/apache/commons/imaging/formats/png/PngWithInvalidPngChunkSizeTest.java
new file mode 100644
index 0000000..faa5d65
--- /dev/null
+++ b/src/test/java/org/apache/commons/imaging/formats/png/PngWithInvalidPngChunkSizeTest.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.imaging.formats.png;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.imaging.ImageReadException;
+import org.apache.commons.imaging.Imaging;
+import org.apache.commons.imaging.ImagingConstants;
+import org.apache.commons.imaging.examples.ImageReadExample.ManagedImageBufferedImageFactory;
+import org.apache.commons.imaging.formats.jpeg.JpegWithInvalidDhtSegmentTest;
+import org.junit.Test;
+
+/**
+ * Tests for PNG files with invalid chunk sizes.
+ */
+public class PngWithInvalidPngChunkSizeTest {
+
+	/**
+	 * Test that an image with an invalid PNG chunk size causes an
+	 * ImageReadException instead of other exception types.
+	 *
+	 * @throws IOException        if it fails to read from the input source
+	 * @throws ImageReadException if it fails to read the image
+	 */
+	@Test(expected = ImageReadException.class)
+	public void testPngWithInvalidPngChunkSize() throws IOException, ImageReadException {
+		final File imageFile = new File(
+				JpegWithInvalidDhtSegmentTest.class.getResource("/IMAGING-211/testfile_2.png").getFile());
+		final Map<String, Object> params = new HashMap<>();
+		params.put(ImagingConstants.BUFFERED_IMAGE_FACTORY, new ManagedImageBufferedImageFactory());
+		Imaging.getBufferedImage(imageFile, params);
+	}
+
+}
diff --git a/src/test/resources/IMAGING-211/testfile_2.png b/src/test/resources/IMAGING-211/testfile_2.png
new file mode 100644
index 0000000..3ffb79b
Binary files /dev/null and b/src/test/resources/IMAGING-211/testfile_2.png differ