8378464: PixelInterleavedSampleModel constructors and methods do not specify behavior when arguments are null or out of bounds

Reviewed-by: kizune, psadhukhan
This commit is contained in:
Phil Race 2026-06-03 18:32:56 +00:00
parent fac043a97a
commit 9f83006f10
6 changed files with 196 additions and 3 deletions

View File

@ -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) {

View File

@ -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)

View File

@ -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<bands.length; i++) {
newBandOffsets[i] = bandOffsets[bands[i]];

View File

@ -307,9 +307,13 @@ public class SinglePixelPackedSampleModel extends SampleModel
* SinglePixelPackedSampleModel/DataBuffer combination will represent
* an image with a subset of the bands of the original
* SinglePixelPackedSampleModel/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 length of the bands argument is
* greater than the number of bands in
* the sample model.
* @throws ArrayIndexOutOfBoundsException if any of the bank indices is out of bounds
*/
public SampleModel createSubsetSampleModel(int[] bands) {
if (bands.length > numBands)

View File

@ -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);
}
}

View File

@ -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);
}
}
}