diff --git a/src/java.desktop/share/classes/java/awt/image/Kernel.java b/src/java.desktop/share/classes/java/awt/image/Kernel.java index d8ebc501db6d3..d2b9760aba50d 100644 --- a/src/java.desktop/share/classes/java/awt/image/Kernel.java +++ b/src/java.desktop/share/classes/java/awt/image/Kernel.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -59,21 +59,38 @@ public class Kernel implements Cloneable { * @param width width of the kernel * @param height height of the kernel * @param data kernel data in row major order + * @throws IllegalArgumentException if {@code data} is null + * @throws IllegalArgumentException if {@code width} or {@code height} + * is not positive + * @throws IllegalArgumentException if product of {@code width} and + * {@code height} overflows an int * @throws IllegalArgumentException if the length of {@code data} * is less than the product of {@code width} and * {@code height} */ public Kernel(int width, int height, float[] data) { - this.width = width; - this.height = height; - this.xOrigin = (width-1)>>1; - this.yOrigin = (height-1)>>1; - int len = width*height; + if (data == null) { + throw new IllegalArgumentException("Data must not be null"); + } + if ((width <= 0) || (height <= 0)) { + throw new IllegalArgumentException("Invalid width or height"); + } + int len = 0; + try { + len = Math.multiplyExact(width, height); + } catch (ArithmeticException e) { + throw new IllegalArgumentException("width * height results in" + + " integer overflow"); + } if (data.length < len) { throw new IllegalArgumentException("Data array too small "+ "(is "+data.length+ " and should be "+len); } + this.width = width; + this.height = height; + this.xOrigin = (width - 1) >> 1; + this.yOrigin = (height - 1) >> 1; this.data = new float[len]; System.arraycopy(data, 0, this.data, 0, len); diff --git a/test/jdk/java/awt/image/ConvolveOp/KernelInitialisationTest.java b/test/jdk/java/awt/image/ConvolveOp/KernelInitialisationTest.java new file mode 100644 index 0000000000000..607f0a7a8dc66 --- /dev/null +++ b/test/jdk/java/awt/image/ConvolveOp/KernelInitialisationTest.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8368729 + * @summary Tests that passing invalid values to Kernel constructor + * throws only IllegalArgumentException + */ + +import java.awt.image.Kernel; + +public class KernelInitialisationTest { + private static void expectIllegalArgumentException(Runnable code) { + try { + code.run(); + throw new RuntimeException("Expected IllegalArgumentException" + + " but no exception was thrown"); + } catch (IllegalArgumentException e) { + // we expect IllegalArgumentException + } + } + + private static void testKernel(int width, int height, float[] data) { + System.out.println("Testing for width: " + width + ", height: " + + height + ", data: " + (data == null ? "null" : "not null")); + expectIllegalArgumentException(() -> new Kernel(width, height, data)); + } + + public static void main(String[] args) { + testKernel(-1, 1, new float[100]); + testKernel(1, -1, new float[100]); + testKernel(-1, -1, new float[100]); + testKernel(1, 1, null); + + int width = 50; + int height = Integer.MAX_VALUE; + testKernel(width, height, new float[100]); + } +}