From 9f83006f10f5d2889529c500f963d4ad07795cae Mon Sep 17 00:00:00 2001 From: Phil Race Date: Wed, 3 Jun 2026 18:32:56 +0000 Subject: [PATCH] 8378464: PixelInterleavedSampleModel constructors and methods do not specify behavior when arguments are null or out of bounds Reviewed-by: kizune, psadhukhan --- .../java/awt/image/BandedSampleModel.java | 5 +- .../java/awt/image/ComponentSampleModel.java | 5 + .../image/PixelInterleavedSampleModel.java | 14 ++- .../image/SinglePixelPackedSampleModel.java | 4 + .../image/CreateSubsetSampleModelTest.java | 109 ++++++++++++++++++ .../image/PixelInterleavedSMConstructor.java | 62 ++++++++++ 6 files changed, 196 insertions(+), 3 deletions(-) create mode 100644 test/jdk/java/awt/image/CreateSubsetSampleModelTest.java create mode 100644 test/jdk/java/awt/image/PixelInterleavedSMConstructor.java diff --git a/src/java.desktop/share/classes/java/awt/image/BandedSampleModel.java b/src/java.desktop/share/classes/java/awt/image/BandedSampleModel.java index a7f014d7a59..502c82fcaa6 100644 --- a/src/java.desktop/share/classes/java/awt/image/BandedSampleModel.java +++ b/src/java.desktop/share/classes/java/awt/image/BandedSampleModel.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2026, 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 @@ -167,9 +167,10 @@ public final class BandedSampleModel extends ComponentSampleModel * can be used with. The new BandedSampleModel/DataBuffer * combination will represent an image with a subset of the bands * of the original BandedSampleModel/DataBuffer combination. + * @throws NullPointerException if {@code bands} is {@code null} + * @throws IllegalArgumentException if the number of bands is not greater than 0 * @throws RasterFormatException if the number of bands is greater than * the number of banks in this sample model. - * @throws IllegalArgumentException if the number of bands is not greater than 0 * @throws ArrayIndexOutOfBoundsException if any of the bank indices is out of bounds */ public SampleModel createSubsetSampleModel(int[] bands) { diff --git a/src/java.desktop/share/classes/java/awt/image/ComponentSampleModel.java b/src/java.desktop/share/classes/java/awt/image/ComponentSampleModel.java index 4460b0fc101..ea0837400c5 100644 --- a/src/java.desktop/share/classes/java/awt/image/ComponentSampleModel.java +++ b/src/java.desktop/share/classes/java/awt/image/ComponentSampleModel.java @@ -395,6 +395,11 @@ public class ComponentSampleModel extends SampleModel * {@code ComponentSampleModel} * @return a {@code ComponentSampleModel} created with a subset * of bands from this {@code ComponentSampleModel}. + * @throws NullPointerException if {@code bands} is {@code null} + * @throws IllegalArgumentException if the number of bands is not greater than 0 + * @throws RasterFormatException if the number of bands is greater than + * the number of banks in this sample model. + * @throws ArrayIndexOutOfBoundsException if any of the band indices is out of bounds */ public SampleModel createSubsetSampleModel(int[] bands) { if (bands.length > bankIndices.length) diff --git a/src/java.desktop/share/classes/java/awt/image/PixelInterleavedSampleModel.java b/src/java.desktop/share/classes/java/awt/image/PixelInterleavedSampleModel.java index 076fe9cdeaf..06b1739950b 100644 --- a/src/java.desktop/share/classes/java/awt/image/PixelInterleavedSampleModel.java +++ b/src/java.desktop/share/classes/java/awt/image/PixelInterleavedSampleModel.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2026, 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 @@ -78,6 +78,8 @@ public class PixelInterleavedSampleModel extends ComponentSampleModel * less than any offset between bands * @throws IllegalArgumentException if {@code dataType} is not * one of the supported data types + * @throws NullPointerException if {@code bandOffsets} is {@code null} + * @throws IllegalArgumentException if {@code bandOffsets.length} is 0 */ public PixelInterleavedSampleModel(int dataType, int w, int h, @@ -153,8 +155,18 @@ public class PixelInterleavedSampleModel extends ComponentSampleModel * PixelInterleavedSampleModel/DataBuffer combination will represent * an image with a subset of the bands of the original * PixelInterleavedSampleModel/DataBuffer combination. + * @throws NullPointerException if {@code bands} is {@code null} + * @throws IllegalArgumentException if the number of bands is not greater than 0 + * @throws RasterFormatException if the number of bands is greater than + * the number of bands in this sample model. + * @throws ArrayIndexOutOfBoundsException if any of the band indices is out of bounds */ public SampleModel createSubsetSampleModel(int[] bands) { + if (bands.length > bandOffsets.length) { + throw new RasterFormatException("There are only " + + bandOffsets.length + + " bands"); + } int[] newBandOffsets = new int[bands.length]; for (int i=0; i numBands) diff --git a/test/jdk/java/awt/image/CreateSubsetSampleModelTest.java b/test/jdk/java/awt/image/CreateSubsetSampleModelTest.java new file mode 100644 index 00000000000..de4aba2afd7 --- /dev/null +++ b/test/jdk/java/awt/image/CreateSubsetSampleModelTest.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2026, 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 8378464 + * @summary test SampleModel.createSubsetSampleModel() + */ + +import static java.awt.image.DataBuffer.*; +import java.awt.image.BandedSampleModel; +import java.awt.image.SampleModel; +import java.awt.image.ComponentSampleModel; +import java.awt.image.PixelInterleavedSampleModel; +import java.awt.image.RasterFormatException; +import java.awt.image.SinglePixelPackedSampleModel; + +public class CreateSubsetSampleModelTest { + + static final int[] nullbands = null; + static final int[] zerobands = { }; + static final int[] oneband = { 0 }; + static final int[] twobands = { 0, 1 }; + static final int[] threebands = { 0, 1, 2 }; + static final int[] badbands = { 99 }; + + public static void main(String[] args) { + + cons(nullbands, NullPointerException.class); + cons(zerobands, IllegalArgumentException.class); + cons(oneband, null); + + PixelInterleavedSampleModel psm2bands = + new PixelInterleavedSampleModel(TYPE_BYTE, 1, 1, 1, 1, twobands); + testSubset(psm2bands); + + ComponentSampleModel csm2bands = + new ComponentSampleModel(TYPE_BYTE, 1, 1, 1, 1, twobands); + testSubset(csm2bands); + + BandedSampleModel bsm2bands = + new BandedSampleModel(TYPE_BYTE, 1, 1, 1, twobands, twobands); + testSubset(bsm2bands); + + SinglePixelPackedSampleModel sppsm2bands = + new SinglePixelPackedSampleModel(TYPE_BYTE, 1, 1, twobands); + testSubset(sppsm2bands); + } + + static void cons(int[] bands, Class eType) { + try { + new PixelInterleavedSampleModel(TYPE_BYTE, 1, 1, 1, 1, bands); + } catch (Exception e) { + if (eType == null || !(eType.isInstance(e))) { + throw new RuntimeException("failed for " + eType + " got " + e); + } else { + return; + } + } + if (eType != null) { + throw new RuntimeException("No exception for " + bands); + } + } + + static void subset(SampleModel sm, int[] bands, Class eType) { + try { + sm.createSubsetSampleModel(bands); + } catch (Exception e) { + if (eType == null || !(eType.isInstance(e))) { + e.printStackTrace(); + throw new RuntimeException("failed for " + eType + " got " + e); + } else { + return; + } + } + if (eType != null) { + throw new RuntimeException("No exception for " + bands); + } + } + + static void testSubset(SampleModel sm) { + subset(sm, nullbands, NullPointerException.class); + subset(sm, zerobands, IllegalArgumentException.class); + subset(sm, oneband, null); + subset(sm, twobands, null); + subset(sm, threebands, RasterFormatException.class); + subset(sm, badbands, ArrayIndexOutOfBoundsException.class); + } +} diff --git a/test/jdk/java/awt/image/PixelInterleavedSMConstructor.java b/test/jdk/java/awt/image/PixelInterleavedSMConstructor.java new file mode 100644 index 00000000000..6ee7fa6e01b --- /dev/null +++ b/test/jdk/java/awt/image/PixelInterleavedSMConstructor.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2026, 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 8378464 + * @summary test PixelInterleavedSampleModel constructor + */ + +import static java.awt.image.DataBuffer.*; +import java.awt.image.PixelInterleavedSampleModel; + +public class PixelInterleavedSMConstructor { + + static final int[] nullbands = null; + static final int[] zerobands = { }; + static final int[] oneband = { 0 }; + static final int[] negband = { -1 }; + + public static void main(String[] args) { + + cons(nullbands, NullPointerException.class); + cons(zerobands, IllegalArgumentException.class); + cons(oneband, null); + cons(negband, IllegalArgumentException.class); + } + + static void cons(int[] bands, Class eType) { + try { + new PixelInterleavedSampleModel(TYPE_BYTE, 1, 1, 1, 1, bands); + } catch (Exception e) { + if (eType == null || !(eType.isInstance(e))) { + throw new RuntimeException("failed for " + eType + " got " + e); + } else { + return; + } + } + if (eType != null) { + throw new RuntimeException("No exception for " + bands); + } + } +}