From becdbb1496a1e49f4a23ca9e095d14797180ef2d Mon Sep 17 00:00:00 2001 From: Phil Race Date: Wed, 24 Jun 2026 15:44:37 +0000 Subject: [PATCH] 8386671: Raster factory methods fail to throw specified exceptions for invalid bandOffsets and bankIndices Reviewed-by: azvegint, kizune, jdv --- .../share/classes/java/awt/image/Raster.java | 30 +++++++++++++++++++ .../Raster/CreateRasterExceptionTest.java | 22 ++++++++++++-- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/java.desktop/share/classes/java/awt/image/Raster.java b/src/java.desktop/share/classes/java/awt/image/Raster.java index 8f35d5819ab..6ae4214b702 100644 --- a/src/java.desktop/share/classes/java/awt/image/Raster.java +++ b/src/java.desktop/share/classes/java/awt/image/Raster.java @@ -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 { diff --git a/test/jdk/java/awt/image/Raster/CreateRasterExceptionTest.java b/test/jdk/java/awt/image/Raster/CreateRasterExceptionTest.java index 13a88e8c590..aa9d8ff4156 100644 --- a/test/jdk/java/awt/image/Raster/CreateRasterExceptionTest.java +++ b/test/jdk/java/awt/image/Raster/CreateRasterExceptionTest.java @@ -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();