8386671: Raster factory methods fail to throw specified exceptions for invalid bandOffsets and bankIndices

Reviewed-by: azvegint, kizune, jdv
This commit is contained in:
Phil Race 2026-06-24 15:44:37 +00:00
parent 0c709fdcf2
commit becdbb1496
2 changed files with 49 additions and 3 deletions

View File

@ -308,6 +308,13 @@ public class Raster {
if (bandOffsets == null) {
throw new NullPointerException("bandOffsets is null");
}
for (int i = 0; i < bandOffsets.length; i++) {
int off = bandOffsets[i];
if ((off > pixelStride) || (off > scanlineStride)) {
throw new IllegalArgumentException("Band offset " + off + " is too large for stride");
}
}
lsz = (long)w * pixelStride;
if (lsz > scanlineStride) {
throw new IllegalArgumentException("w * pixelStride is too large");
@ -803,6 +810,21 @@ public class Raster {
if (dataBuffer == null) {
throw new NullPointerException("DataBuffer cannot be null");
}
if (pixelStride < 0) {
throw new IllegalArgumentException("pixelStride is < 0");
}
if (scanlineStride < 0) {
throw new IllegalArgumentException("scanlineStride is < 0");
}
if (bandOffsets == null) {
throw new NullPointerException("bandOffsets is null");
}
for (int i = 0; i < bandOffsets.length; i++) {
int off = bandOffsets[i];
if ((off > pixelStride) || (off > scanlineStride)) {
throw new IllegalArgumentException("Band offset " + off + " is too large for stride");
}
}
if (location == null) {
location = new Point(0, 0);
@ -914,6 +936,14 @@ public class Raster {
"bankIndices.length != bandOffsets.length");
}
int numBanks = dataBuffer.getNumBanks();
for (int i = 0; i < bands; i++) {
if (bankIndices[i] >= numBanks) {
throw new ArrayIndexOutOfBoundsException("Bank[" + i + "] == " + bankIndices[i] +
" and there are only " + numBanks + " banks.");
}
}
if (location == null) {
location = new Point(0,0);
} else {

View File

@ -23,7 +23,7 @@
/*
* @test
* @bug 8255800 8369129 8376297
* @bug 8255800 8369129 8376297 8386671
* @summary verify Raster + SampleModel creation vs spec.
*/
@ -931,9 +931,10 @@ public class CreateRasterExceptionTest {
/* @throws ArrayIndexOutOfBoundsException if any element of {@code bankIndices}
* is greater or equal to the number of bands in {@code dataBuffer}
*/
DataBuffer dBuffer2Bands = new DataBufferByte(15, 2);
int[] indices = new int[] { 0, 1, 2 };
int[] offsets = new int[] { 0, 0, 0 };
Raster.createBandedRaster(dBuffer, 1, 1, 1,
Raster.createBandedRaster(dBuffer2Bands, 1, 1, 1,
indices, offsets, null);
noException();
} catch (ArrayIndexOutOfBoundsException t) {
@ -1198,6 +1199,21 @@ public class CreateRasterExceptionTest {
"Got expected exception for bad databuffer type");
System.out.println(t);
}
try {
/* @throws IllegalArgumentException if any element of {@code bandOffsets} is greater
* than {@code pixelStride} or the {@code scanlineStride}
*/
int[] offsets = new int[] {2};
Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
1, 1, 1, 1, offsets, null);
noException();
} catch (IllegalArgumentException t) {
System.out.println(
"Got expected exception for element too large");
System.out.println(t);
}
}
/* createInterleavedRaster(DataBuffer dBuffer,
@ -1344,7 +1360,7 @@ public class CreateRasterExceptionTest {
/* @throws IllegalArgumentException if any element of {@code bandOffsets} is greater
* than {@code pixelStride} or the {@code scanlineStride}
*/
int[] offsets = new int[] { 0, 1, 2};
int[] offsets = new int[] {2};
Raster.createInterleavedRaster(dBuffer,
1, 1, 1, 1, offsets, null);
noException();