Skip to content

Commit 29509b8

Browse files
KostyaShaTheRealMDoerr
authored andcommitted
8211795: ArrayIndexOutOfBoundsException in PNGImageReader after JDK-6788458
Backport-of: b9bf598
1 parent b2ff90d commit 29509b8

File tree

2 files changed

+141
-5
lines changed

2 files changed

+141
-5
lines changed

src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageReader.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,8 +1166,7 @@ private void decodePass(int passNum,
11661166
// same bit depth as the source data
11671167
boolean adjustBitDepths = false;
11681168
int[] outputSampleSize = imRas.getSampleModel().getSampleSize();
1169-
int numBands = outputSampleSize.length;
1170-
for (int b = 0; b < numBands; b++) {
1169+
for (int b = 0; b < inputBands; b++) {
11711170
if (outputSampleSize[b] != bitDepth) {
11721171
adjustBitDepths = true;
11731172
break;
@@ -1180,8 +1179,8 @@ private void decodePass(int passNum,
11801179
if (adjustBitDepths) {
11811180
int maxInSample = (1 << bitDepth) - 1;
11821181
int halfMaxInSample = maxInSample/2;
1183-
scale = new int[numBands][];
1184-
for (int b = 0; b < numBands; b++) {
1182+
scale = new int[inputBands][];
1183+
for (int b = 0; b < inputBands; b++) {
11851184
int maxOutSample = (1 << outputSampleSize[b]) - 1;
11861185
scale[b] = new int[maxInSample + 1];
11871186
for (int s = 0; s <= maxInSample; s++) {
@@ -1307,7 +1306,7 @@ private void decodePass(int passNum,
13071306

13081307
passRow.getPixel(newSrcX, 0, ps);
13091308
if (adjustBitDepths) {
1310-
for (int b = 0; b < numBands; b++) {
1309+
for (int b = 0; b < inputBands; b++) {
13111310
ps[b] = scale[b][ps[b]];
13121311
}
13131312
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8211795
27+
* @summary Test verifies that PNGImageReader maintains proper
28+
* number of bands for scale array when PNG image
29+
* has tRNS chunk.
30+
* @run main VerifyBitDepthScalingWithTRNSChunk
31+
*/
32+
33+
import java.awt.Graphics2D;
34+
import java.awt.image.BufferedImage;
35+
import java.awt.Color;
36+
import java.awt.image.IndexColorModel;
37+
import java.io.ByteArrayInputStream;
38+
import java.io.ByteArrayOutputStream;
39+
import java.io.IOException;
40+
import java.io.InputStream;
41+
import java.util.Iterator;
42+
import javax.imageio.ImageTypeSpecifier;
43+
import javax.imageio.ImageWriter;
44+
import javax.imageio.ImageIO;
45+
import javax.imageio.ImageWriteParam;
46+
import javax.imageio.metadata.IIOInvalidTreeException;
47+
import javax.imageio.metadata.IIOMetadata;
48+
import javax.imageio.metadata.IIOMetadataNode;
49+
import javax.imageio.stream.ImageOutputStream;
50+
import javax.imageio.IIOImage;
51+
52+
public class VerifyBitDepthScalingWithTRNSChunk {
53+
54+
private static BufferedImage img;
55+
private static ImageWriter writer;
56+
private static ImageWriteParam param;
57+
private static IIOMetadata metadata;
58+
private static byte[] imageByteArray;
59+
60+
private static void initialize(int type) {
61+
int width = 1;
62+
int height = 1;
63+
// create Palette & IndexColorModel for bitdepth 1
64+
int size = 2;
65+
int bitDepth = 1;
66+
byte[] r = new byte[size];
67+
byte[] g = new byte[size];
68+
byte[] b = new byte[size];
69+
70+
r[0] = g[0] = b[0] = 0;
71+
r[1] = g[1] = b[1] = (byte)255;
72+
73+
IndexColorModel cm = new IndexColorModel(bitDepth, size, r, g, b);
74+
img = new BufferedImage(width, height, type, cm);
75+
Graphics2D g2D = img.createGraphics();
76+
g2D.setColor(new Color(255, 255, 255));
77+
g2D.fillRect(0, 0, width, height);
78+
79+
Iterator<ImageWriter> iterWriter =
80+
ImageIO.getImageWritersBySuffix("png");
81+
writer = iterWriter.next();
82+
83+
param = writer.getDefaultWriteParam();
84+
ImageTypeSpecifier specifier =
85+
ImageTypeSpecifier.
86+
createFromBufferedImageType(type);
87+
metadata = writer.getDefaultImageMetadata(specifier, param);
88+
}
89+
90+
private static void createTRNSNode(String tRNS_value)
91+
throws IIOInvalidTreeException {
92+
IIOMetadataNode tRNS_gray = new IIOMetadataNode("tRNS_Grayscale");
93+
tRNS_gray.setAttribute("gray", tRNS_value);
94+
95+
IIOMetadataNode tRNS = new IIOMetadataNode("tRNS");
96+
tRNS.appendChild(tRNS_gray);
97+
IIOMetadataNode root = new IIOMetadataNode("javax_imageio_png_1.0");
98+
root.appendChild(tRNS);
99+
metadata.mergeTree("javax_imageio_png_1.0", root);
100+
}
101+
102+
private static void writeImage() throws IOException {
103+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
104+
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
105+
writer.setOutput(ios);
106+
writer.write(metadata, new IIOImage(img, null, metadata), param);
107+
writer.dispose();
108+
109+
baos.flush();
110+
imageByteArray = baos.toByteArray();
111+
baos.close();
112+
}
113+
114+
private static void verifyBitDepthScalingWithTRNSChunk()
115+
throws IOException {
116+
initialize(BufferedImage.TYPE_BYTE_BINARY);
117+
// Create tRNS node with some value and merge it with default metadata
118+
createTRNSNode("255");
119+
120+
writeImage();
121+
122+
InputStream input= new ByteArrayInputStream(imageByteArray);
123+
/*
124+
* Read 1 bit PNG Gray image with tRNS chunk.
125+
* Since bitDepth is 1 there will be scaling of each channel,
126+
* and it has tRNS chunk for which we will add extra alpha channel.
127+
* This will result in creation of scale array in PNGImageReader.
128+
*/
129+
ImageIO.read(input);
130+
input.close();
131+
}
132+
133+
public static void main(String[] args) throws IOException {
134+
verifyBitDepthScalingWithTRNSChunk();
135+
}
136+
}
137+

0 commit comments

Comments
 (0)