Skip to content

Commit c733193

Browse files
Masanori Yanojayathirthrao
authored andcommitted
8262297: ImageIO.write() method will throw IndexOutOfBoundsException
Reviewed-by: serb, jdv
1 parent da2be99 commit c733193

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

src/java.desktop/share/classes/com/sun/imageio/plugins/bmp/BMPImageWriter.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -327,7 +327,8 @@ public void write(IIOMetadata streamMetadata,
327327

328328
if (!canEncodeImage(compressionType, colorModel, sampleModel)) {
329329
throw new IOException("Image can not be encoded with compression type "
330-
+ BMPCompressionTypes.getName(compressionType));
330+
+ BMPCompressionTypes.getName(compressionType)
331+
+ " and " + colorModel.getPixelSize() + " bits per pixel");
331332
}
332333

333334
byte[] r = null, g = null, b = null, a = null;
@@ -1456,6 +1457,9 @@ protected boolean canEncodeImage(int compression, ImageTypeSpecifier imgType) {
14561457
}
14571458
int biType = imgType.getBufferedImageType();
14581459
int bpp = imgType.getColorModel().getPixelSize();
1460+
if (bpp != 0 && bpp != 1 && bpp != 4 && bpp != 8 && bpp != 16 && bpp != 24 && bpp != 32) {
1461+
return false;
1462+
}
14591463
if (compressionType == BI_RLE4 && bpp != 4) {
14601464
// only 4bpp images can be encoded as BI_RLE4
14611465
return false;
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2021, 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 8262297
27+
* @summary Test that writing invalid bit per pixel image throws IOException
28+
*/
29+
30+
import java.awt.image.BufferedImage;
31+
import java.awt.image.IndexColorModel;
32+
import java.io.File;
33+
import java.io.IOException;
34+
import javax.imageio.ImageIO;
35+
36+
public class BMPBitsPerPixelTest {
37+
38+
public static void main(String[] args) {
39+
test(1, false);
40+
test(2, true);
41+
test(3, true);
42+
test(4, false);
43+
test(5, true);
44+
test(6, true);
45+
test(7, true);
46+
test(8, false);
47+
}
48+
49+
public static void test(int bpp, boolean shouldThrowException) {
50+
int palettes = (int)Math.pow(2, bpp);
51+
byte[] r = new byte[palettes];
52+
byte[] g = new byte[palettes];
53+
byte[] b = new byte[palettes];
54+
boolean exceptionThrown = false;
55+
try {
56+
IndexColorModel cm = new IndexColorModel(bpp, palettes, r, g, b);
57+
int imageType = BufferedImage.TYPE_BYTE_BINARY;
58+
if (bpp > 4) {
59+
imageType = BufferedImage.TYPE_BYTE_INDEXED;
60+
}
61+
BufferedImage img = new BufferedImage(10, 10, imageType, (IndexColorModel)cm);
62+
File file = File.createTempFile("test", ".bmp", new File("."));
63+
file.deleteOnExit();
64+
ImageIO.write(img, "BMP", file);
65+
} catch (IOException e) {
66+
exceptionThrown = true;
67+
} catch (Exception e) {
68+
e.printStackTrace();
69+
throw new RuntimeException("Unexpected exception: " + e);
70+
}
71+
72+
if (shouldThrowException && !exceptionThrown) {
73+
throw new RuntimeException("IOException was not caught.");
74+
} else if (!shouldThrowException && exceptionThrown) {
75+
throw new RuntimeException("IOException should not be thrown.");
76+
} else {
77+
System.out.println("Test PASSED.");
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)