Skip to content

Commit 4233178

Browse files
committed
8368729: Add appropriate checks in java.awt.image.Kernel constructor
Reviewed-by: azvegint, prr, kizune
1 parent 3d6824e commit 4233178

File tree

2 files changed

+83
-6
lines changed

2 files changed

+83
-6
lines changed

src/java.desktop/share/classes/java/awt/image/Kernel.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025, 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
@@ -59,21 +59,38 @@ public class Kernel implements Cloneable {
5959
* @param width width of the kernel
6060
* @param height height of the kernel
6161
* @param data kernel data in row major order
62+
* @throws IllegalArgumentException if {@code data} is null
63+
* @throws IllegalArgumentException if {@code width} or {@code height}
64+
* is not positive
65+
* @throws IllegalArgumentException if product of {@code width} and
66+
* {@code height} overflows an int
6267
* @throws IllegalArgumentException if the length of {@code data}
6368
* is less than the product of {@code width} and
6469
* {@code height}
6570
*/
6671
public Kernel(int width, int height, float[] data) {
67-
this.width = width;
68-
this.height = height;
69-
this.xOrigin = (width-1)>>1;
70-
this.yOrigin = (height-1)>>1;
71-
int len = width*height;
72+
if (data == null) {
73+
throw new IllegalArgumentException("Data must not be null");
74+
}
75+
if ((width <= 0) || (height <= 0)) {
76+
throw new IllegalArgumentException("Invalid width or height");
77+
}
78+
int len = 0;
79+
try {
80+
len = Math.multiplyExact(width, height);
81+
} catch (ArithmeticException e) {
82+
throw new IllegalArgumentException("width * height results in" +
83+
" integer overflow");
84+
}
7285
if (data.length < len) {
7386
throw new IllegalArgumentException("Data array too small "+
7487
"(is "+data.length+
7588
" and should be "+len);
7689
}
90+
this.width = width;
91+
this.height = height;
92+
this.xOrigin = (width - 1) >> 1;
93+
this.yOrigin = (height - 1) >> 1;
7794
this.data = new float[len];
7895
System.arraycopy(data, 0, this.data, 0, len);
7996

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2025, 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 8368729
27+
* @summary Tests that passing invalid values to Kernel constructor
28+
* throws only IllegalArgumentException
29+
*/
30+
31+
import java.awt.image.Kernel;
32+
33+
public class KernelInitialisationTest {
34+
private static void expectIllegalArgumentException(Runnable code) {
35+
try {
36+
code.run();
37+
throw new RuntimeException("Expected IllegalArgumentException" +
38+
" but no exception was thrown");
39+
} catch (IllegalArgumentException e) {
40+
// we expect IllegalArgumentException
41+
}
42+
}
43+
44+
private static void testKernel(int width, int height, float[] data) {
45+
System.out.println("Testing for width: " + width + ", height: "
46+
+ height + ", data: " + (data == null ? "null" : "not null"));
47+
expectIllegalArgumentException(() -> new Kernel(width, height, data));
48+
}
49+
50+
public static void main(String[] args) {
51+
testKernel(-1, 1, new float[100]);
52+
testKernel(1, -1, new float[100]);
53+
testKernel(-1, -1, new float[100]);
54+
testKernel(1, 1, null);
55+
56+
int width = 50;
57+
int height = Integer.MAX_VALUE;
58+
testKernel(width, height, new float[100]);
59+
}
60+
}

0 commit comments

Comments
 (0)