From 3408acfa6cb7a15951a8a9b4e89f31be13ecbd61 Mon Sep 17 00:00:00 2001 From: prrace Date: Fri, 3 Oct 2025 11:33:34 -0700 Subject: [PATCH 1/4] 8369129 --- .../share/classes/java/awt/image/Raster.java | 192 ++++++-- .../Raster/CreateRasterExceptionTest.java | 437 +++++++++++++++++- 2 files changed, 584 insertions(+), 45 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 9f346cb304a53..c33e12e7c9ad6 100644 --- a/src/java.desktop/share/classes/java/awt/image/Raster.java +++ b/src/java.desktop/share/classes/java/awt/image/Raster.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2023, 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 @@ -267,6 +267,10 @@ public static WritableRaster createInterleavedRaster(int dataType, * @throws IllegalArgumentException if {@code scanlineStride} * is less than 0 * @throws IllegalArgumentException if {@code pixelStride} is less than 0 + * @throws IllegalArgumentException if {@code w * pixelStride} is greater + * than {@code scanlineStride} + * @throws IllegalArgumentException if the data size need to store all + * lines of the image is greater than {@code Integer.MAX_VALUE} * @throws NullPointerException if {@code bandOffsets} is null */ public static WritableRaster createInterleavedRaster(int dataType, @@ -291,8 +295,19 @@ public static WritableRaster createInterleavedRaster(int dataType, if (scanlineStride < 0) { throw new IllegalArgumentException("scanlineStride is < 0"); } - int size = scanlineStride * (h - 1) + // first (h - 1) scans - pixelStride * w; // last scan + if (bandOffsets == null) { + throw new NullPointerException("bandOffsets is null"); + } + lsz = (long)w * pixelStride; + if (lsz > scanlineStride) { + throw new IllegalArgumentException("w * pixelStride is too large"); + } + lsz = (long)scanlineStride * (long)(h - 1) + // first (h - 1) scans + (long)pixelStride * (long)w; // last scan + if (lsz > Integer.MAX_VALUE) { + throw new IllegalArgumentException("size too large to store image"); + } + int size = (int)lsz; if (location == null) { location = new Point(0, 0); @@ -415,6 +430,8 @@ public static WritableRaster createBandedRaster(int dataType, * is less than 0 * @throws ArrayIndexOutOfBoundsException if {@code bankIndices} * is {@code null} + * @throws IllegalArgumentException if the data size need to store all + * lines of a bank of the image is greater than {@code Integer.MAX_VALUE} * @throws NullPointerException if {@code bandOffsets} is {@code null} */ public static WritableRaster createBandedRaster(int dataType, @@ -423,9 +440,6 @@ public static WritableRaster createBandedRaster(int dataType, int[] bankIndices, int[] bandOffsets, Point location) { - DataBuffer d; - int bands = bandOffsets.length; - if (w <= 0 || h <= 0) { throw new IllegalArgumentException("w and h must be positive"); } @@ -440,7 +454,7 @@ public static WritableRaster createBandedRaster(int dataType, } if (bandOffsets == null) { throw new - ArrayIndexOutOfBoundsException("Band offsets array is null"); + NullPointerException("Band offsets array is null"); } if (location != null) { if ((w + location.getX() > Integer.MAX_VALUE) || @@ -451,6 +465,9 @@ public static WritableRaster createBandedRaster(int dataType, } } + DataBuffer d; + int bands = bandOffsets.length; + // Figure out the #banks and the largest band offset int maxBank = bankIndices[0]; int maxBandOff = bandOffsets[0]; @@ -463,9 +480,15 @@ public static WritableRaster createBandedRaster(int dataType, } } int banks = maxBank + 1; - int size = maxBandOff + - scanlineStride * (h - 1) + // first (h - 1) scans - w; // last scan + + lsz = (long) maxBandOff + + (long)scanlineStride * (h - 1) + // first (h - 1) scans + w; // last scan + if (lsz > Integer.MAX_VALUE) { + throw new IllegalArgumentException("storage size is too large"); + } + + int size = (int)lsz; switch(dataType) { case DataBuffer.TYPE_BYTE: @@ -508,11 +531,14 @@ public static WritableRaster createBandedRaster(int dataType, * @param location the upper-left corner of the {@code Raster} * @return a WritableRaster object with the specified data type, * width, height, and band masks. - * @throws RasterFormatException if {@code w} or {@code h} - * is less than or equal to zero, or computing either + * @throws NullPointerException if {@code bandMasks} is null + * @throws IllegalArgumentException if {@code w} and {@code h} + * are not both greater than 0 + * @throws IllegalArgumentException if the product of {@code w} + * and {@code h} is greater than {@code Integer.MAX_VALUE} + * @throws RasterFormatException if computing either * {@code location.x + w} or - * {@code location.y + h} results in integer - * overflow + * {@code location.y + h} results in integer overflow * @throws IllegalArgumentException if {@code dataType} is not * one of the supported data types, which are * {@code DataBuffer.TYPE_BYTE}, @@ -525,6 +551,24 @@ public static WritableRaster createPackedRaster(int dataType, Point location) { DataBuffer d; + if (w <= 0 || h <= 0) { + throw new IllegalArgumentException("w and h must be positive"); + } + long lsz = (long)w * h; + if (lsz > Integer.MAX_VALUE) { + throw new IllegalArgumentException("Dimensions (width="+w+ + " height="+h+") are too large"); + } + + if (location != null) { + if ((w + location.getX() > Integer.MAX_VALUE) || + (h + location.getY() > Integer.MAX_VALUE)) { + throw new RasterFormatException( + "location.x + w and location.y + h " + + " cannot exceed Integer.MAX_VALUE"); + } + } + switch(dataType) { case DataBuffer.TYPE_BYTE: d = new DataBufferByte(w*h); @@ -573,17 +617,19 @@ public static WritableRaster createPackedRaster(int dataType, * @param location the upper-left corner of the {@code Raster} * @return a WritableRaster object with the specified data type, * width, height, number of bands, and bits per band. - * @throws RasterFormatException if {@code w} or {@code h} - * is less than or equal to zero, or computing either - * {@code location.x + w} or - * {@code location.y + h} results in integer - * overflow + * @throws IllegalArgumentException if {@code bitsPerBand} or + * {@code bands} is not greater than zero * @throws IllegalArgumentException if the product of * {@code bitsPerBand} and {@code bands} is * greater than the number of bits held by * {@code dataType} - * @throws IllegalArgumentException if {@code bitsPerBand} or - * {@code bands} is not greater than zero + * @throws IllegalArgumentException if {@code w} and {@code h} + * are not both greater than 0 + * @throws IllegalArgumentException if the product of {@code w} + * and {@code h} is greater than {@code Integer.MAX_VALUE} + * @throws RasterFormatException if computing either + * {@code location.x + w} or + * {@code location.y + h} results in integer overflow * @throws IllegalArgumentException if {@code dataType} is not * one of the supported data types, which are * {@code DataBuffer.TYPE_BYTE}, @@ -607,18 +653,37 @@ public static WritableRaster createPackedRaster(int dataType, ") must be greater than 0"); } + if (w <= 0 || h <= 0) { + throw new IllegalArgumentException("w and h must be positive"); + } + long lsz = (long)w * h; + if (lsz > Integer.MAX_VALUE) { + throw new IllegalArgumentException("Dimensions (width="+w+ + " height="+h+") are too large"); + } + + if (location != null) { + if ((w + location.getX() > Integer.MAX_VALUE) || + (h + location.getY() > Integer.MAX_VALUE)) { + throw new RasterFormatException( + "location.x + w and location.y + h " + + " cannot exceed Integer.MAX_VALUE"); + } + } + + int shift = (bands-1)*bitsPerBand; + + /* Make sure the total mask size will fit in the data type */ + if (shift+bitsPerBand > DataBuffer.getDataTypeSize(dataType)) { + throw new IllegalArgumentException("bitsPerBand("+ + bitsPerBand+") * bands is "+ + " greater than data type "+ + "size."); + } if (bands != 1) { int[] masks = new int[bands]; int mask = (1 << bitsPerBand) - 1; - int shift = (bands-1)*bitsPerBand; - - /* Make sure the total mask size will fit in the data type */ - if (shift+bitsPerBand > DataBuffer.getDataTypeSize(dataType)) { - throw new IllegalArgumentException("bitsPerBand("+ - bitsPerBand+") * bands is "+ - " greater than data type "+ - "size."); - } + switch(dataType) { case DataBuffer.TYPE_BYTE: case DataBuffer.TYPE_USHORT: @@ -883,11 +948,14 @@ public static WritableRaster createBandedRaster(DataBuffer dataBuffer, * @return a WritableRaster object with the specified * {@code DataBuffer}, width, height, scanline stride, * and band masks. - * @throws RasterFormatException if {@code w} or {@code h} - * is less than or equal to zero, or computing either + * @throws NullPointerException if {@code bandMasks} is null + * @throws IllegalArgumentException if {@code w} and {@code h} + * are not both greater than 0 + * @throws IllegalArgumentException if the product of {@code w} + * and {@code h} is greater than {@code Integer.MAX_VALUE} + * @throws RasterFormatException if computing either * {@code location.x + w} or - * {@code location.y + h} results in integer - * overflow + * {@code location.y + h} results in integer overflow * @throws IllegalArgumentException if {@code dataBuffer} is not * one of the supported data types, which are * {@code DataBuffer.TYPE_BYTE}, @@ -906,6 +974,25 @@ public static WritableRaster createPackedRaster(DataBuffer dataBuffer, if (dataBuffer == null) { throw new NullPointerException("DataBuffer cannot be null"); } + + if (w <= 0 || h <= 0) { + throw new IllegalArgumentException("w and h must be positive"); + } + long lsz = (long)w * h; + if (lsz > Integer.MAX_VALUE) { + throw new IllegalArgumentException("Dimensions (width="+w+ + " height="+h+") are too large"); + } + + if (location != null) { + if ((w + location.getX() > Integer.MAX_VALUE) || + (h + location.getY() > Integer.MAX_VALUE)) { + throw new RasterFormatException( + "location.x + w and location.y + h " + + " cannot exceed Integer.MAX_VALUE"); + } + } + if (location == null) { location = new Point(0,0); } @@ -960,11 +1047,13 @@ public static WritableRaster createPackedRaster(DataBuffer dataBuffer, * @return a WritableRaster object with the specified * {@code DataBuffer}, width, height, and * bits per pixel. - * @throws RasterFormatException if {@code w} or {@code h} - * is less than or equal to zero, or computing either + * @throws IllegalArgumentException if {@code w} and {@code h} + * are not both greater than 0 + * @throws IllegalArgumentException if the product of {@code w} + * and {@code h} is greater than {@code Integer.MAX_VALUE} + * @throws IllegalArgumentException if computing either * {@code location.x + w} or - * {@code location.y + h} results in integer - * overflow + * {@code location.y + h} results in integer overflow * @throws IllegalArgumentException if {@code dataType} is not * one of the supported data types, which are * {@code DataBuffer.TYPE_BYTE}, @@ -972,6 +1061,8 @@ public static WritableRaster createPackedRaster(DataBuffer dataBuffer, * or {@code DataBuffer.TYPE_INT} * @throws RasterFormatException if {@code dataBuffer} has more * than one bank. + * @throws RasterFormatException if {@code bitsPixel} is less than 1 or + * not a power of 2 or exceeds the {@code dataBuffer} element size. * @throws NullPointerException if {@code dataBuffer} is null */ public static WritableRaster createPackedRaster(DataBuffer dataBuffer, @@ -982,6 +1073,25 @@ public static WritableRaster createPackedRaster(DataBuffer dataBuffer, if (dataBuffer == null) { throw new NullPointerException("DataBuffer cannot be null"); } + if (w <= 0 || h <= 0) { + throw new IllegalArgumentException("w and h must be positive"); + } + long lsz = (long)w * h; + + if (lsz > Integer.MAX_VALUE) { + throw new IllegalArgumentException("Dimensions (width="+w+ + " height="+h+") are too large"); + } + + if (location != null) { + if ((w + location.getX() > Integer.MAX_VALUE) || + (h + location.getY() > Integer.MAX_VALUE)) { + throw new RasterFormatException( + "location.x + w and location.y + h " + + " cannot exceed Integer.MAX_VALUE"); + } + } + if (location == null) { location = new Point(0,0); } @@ -1000,6 +1110,12 @@ public static WritableRaster createPackedRaster(DataBuffer dataBuffer, " must only have 1 bank."); } + if ((bitsPerPixel < 1) || (bitsPerPixel > DataBuffer.getDataTypeSize(dataType))) { + // NB MPPSM checks power of 2 condition + throw new + RasterFormatException("bitsPerPixel must be > 0 and a power of 2 that " + + "does not exceed data buffer element size"); + } MultiPixelPackedSampleModel mppsm = new MultiPixelPackedSampleModel(dataType, w, h, bitsPerPixel); diff --git a/test/jdk/java/awt/image/Raster/CreateRasterExceptionTest.java b/test/jdk/java/awt/image/Raster/CreateRasterExceptionTest.java index defef9b3b65c8..775d1513db7ec 100644 --- a/test/jdk/java/awt/image/Raster/CreateRasterExceptionTest.java +++ b/test/jdk/java/awt/image/Raster/CreateRasterExceptionTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 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 @@ -42,6 +42,7 @@ public class CreateRasterExceptionTest { static int[] negBankIndices = new int[] { -1, 0}; static int[] bandOffsets = new int[] { 0, 0}; static int[] bandOffsets2 = new int[] { 0, 0, 0, 0}; + static int[] bandMasks1 = new int[] { 0x0ff }; static int[] zeroBandOffsets = new int[] {}; static DataBuffer dBuffer = new DataBufferByte(15); @@ -50,18 +51,30 @@ static void noException() { throw new RuntimeException("No expected exception"); } - /* Except a version starting with "17" or higher */ - static void checkIsOldVersion(Throwable t) { + /** + * If running on a JDK of the targetVersion or later, throw + * a RuntimeException becuase the exception argument + * should not have occured. However it is expected on + * prior versions because that was the previous behaviour. + * @param targetVersion to check + * @param t the thrown exception to print + */ + static void checkIsOldVersion(int targetVersion, Throwable t) { String version = System.getProperty("java.version"); version = version.split("\\D")[0]; int v = Integer.parseInt(version); - if (v >= 17) { + if (v >= targetVersion) { t.printStackTrace(); throw new RuntimeException( "Unexpected exception for version " + v); } } + /* Except a version starting with "17" or higher */ + static void checkIsOldVersion(Throwable t) { + checkIsOldVersion(17, t); + } + public static void main(String[] args) { componentSampleModelTests1(); componentSampleModelTests2(); @@ -73,6 +86,10 @@ public static void main(String[] args) { interleavedRasterTests1(); interleavedRasterTests2(); interleavedRasterTests3(); + packedRasterTests1(); + packedRasterTests2(); + packedRasterTests3(); + packedRasterTests4(); System.out.println(); System.out.println(" ** Test Passed **"); } @@ -734,7 +751,7 @@ static void bandedRasterTests2() { /* @throws ArrayIndexOutOfBoundsException if * {@code bankIndices} is null */ - Raster.createBandedRaster(DataBuffer.TYPE_INT, 1, 1, 0, + Raster.createBandedRaster(DataBuffer.TYPE_INT, 1, 1, 1, null, bandOffsets, null); noException(); } catch (ArrayIndexOutOfBoundsException t) { @@ -747,7 +764,7 @@ static void bandedRasterTests2() { /* @throws NullPointerException if {@code bandOffsets} * is null */ - Raster.createBandedRaster(DataBuffer.TYPE_INT, 1, 1, 0, + Raster.createBandedRaster(DataBuffer.TYPE_INT, 1, 1, 1, bankIndices, null, null); noException(); } catch (NullPointerException t) { @@ -1045,6 +1062,25 @@ static void interleavedRasterTests2() { System.out.println(t); } + try { + /* @throws IllegalArgumentException if the data size + * needs to store all lines is greater than + * {@code Integer.MAX_VALUE} + */ + Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, + 1000, 1000, + Integer.MAX_VALUE/2 , 1, + bandOffsets, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println("Got expected exception for overflow"); + System.out.println(t); + } catch (NegativeArraySizeException t) { + checkIsOldVersion(26, t); + System.out.println("Got expected exception for overflow"); + System.out.println(t); + } + try { /* @throws RasterFormatException if computing either * {@code location.x + w} or @@ -1091,12 +1127,25 @@ static void interleavedRasterTests2() { System.out.println(t); } + try { + /* @throws IllegalArgumentException if (w * pixelStride) + * is greater than scanlineStride + */ + Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, + 1, 1, 0, 1, bandOffsets, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for incorrect stride"); + System.out.println(t); + } + try { /* @throws NullPointerException if {@code bandOffsets} * is null */ Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, - 1, 1, 0, 1, null, null); + 1, 1, 1, 1, null, null); noException(); } catch (NullPointerException t) { System.out.println( @@ -1242,9 +1291,383 @@ static void interleavedRasterTests3() { bandOffsets, null); noException(); } catch (RasterFormatException t) { + System.out.println( + "Got expected exception for bad databuffer banks"); + System.out.println(t); + } + } + + /* createPackedRaster(int dataType, + * int w, int h, + * int[] bandMasks, + * Point location) + * + */ + static void packedRasterTests1() { + + System.out.println(); + System.out.println("** packedRasterTests1"); + + try { + /* @throws IllegalArgumentException if {@code w} and {@code h} + * are not both greater than 0 + */ + Raster.createPackedRaster(DataBuffer.TYPE_BYTE, 0, 0, bandMasks1, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for zero w / h"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if {@code w} * {@code h} + * is greater than {@code Integer.MAX_VALUE} + */ + Raster.createPackedRaster(DataBuffer.TYPE_BYTE, + Integer.MAX_VALUE/10, Integer.MAX_VALUE/10, + bandMasks1, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for overflow"); + System.out.println(t); + } + + try { + /* @throws RasterFormatException if computing either + * {@code location.x + w} or + * {@code location.y + h} results in integer overflow + */ + Point pt = new Point(5, 1); + Raster.createPackedRaster(DataBuffer.TYPE_BYTE, + Integer.MAX_VALUE-2, 1, + bandMasks1, pt); + noException(); + } catch (RasterFormatException t) { + System.out.println("Got expected exception for overflow"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if {@code dataType} + * is not one of the supported data types + */ + Raster.createPackedRaster(1000, 1, 1, bandMasks1, null); + noException(); + } catch (IllegalArgumentException t) { System.out.println( "Got expected exception for bad databuffer type"); System.out.println(t); } + } + + /* createPackedRaster(int dataType, + * int w, int h, + * int bands, + * int bitsPerBand, + * Point location) + */ + static void packedRasterTests2() { + + System.out.println(); + System.out.println("** packedRasterTests2"); + + try { + /* @throws IllegalArgumentException if {@code w} and {@code h} + * are not both greater than 0 + */ + Raster.createPackedRaster(DataBuffer.TYPE_BYTE, 0, 0, 1, 8, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for zero w / h"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if {@code w} * {@code h} + * is greater than {@code Integer.MAX_VALUE} + */ + Raster.createPackedRaster(DataBuffer.TYPE_BYTE, + Integer.MAX_VALUE/10, Integer.MAX_VALUE/10, + 1, 8, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for overflow"); + System.out.println(t); + } + + try { + /* @throws RasterFormatException if computing either + * {@code location.x + w} or + * {@code location.y + h} results in integer overflow + */ + Point pt = new Point(5, 1); + Raster.createPackedRaster(DataBuffer.TYPE_BYTE, + Integer.MAX_VALUE-2, 1, + 1, 8, pt); + noException(); + } catch (RasterFormatException t) { + System.out.println("Got expected exception for overflow"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if {@code bitsPerBand} or + * {@code bands} is not greater than zero + */ + Raster.createPackedRaster(DataBuffer.TYPE_BYTE, 1, 1, + 0, 8, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for 0 bands"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if {@code bitsPerBand} or + * {@code bands} is not greater than zero + */ + Raster.createPackedRaster(DataBuffer.TYPE_BYTE, 1, 1, + 8, 0, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for 0 bitsPerBand"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if the product of + * {@code bitsPerBand} and {@code bands} is + * greater than the number of bits held by + * {@code dataType} + */ + Raster.createPackedRaster(DataBuffer.TYPE_BYTE, 1, 1, + 2, 8, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for bands per sample"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if {@code dataType} + * is not one of the supported data types + */ + Raster.createPackedRaster(1000, 1, 1, 1, 8, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for bad databuffer type"); + System.out.println(t); + } + } + + /* createPackedRaster(DataBuffer dataBuffer, + * int w, int h, + * int scanlineStride, + * int[] bandMasks, + * Point location) + */ + static void packedRasterTests3() { + + System.out.println(); + System.out.println("** packedRasterTests3"); + + try { + /* @throws IllegalArgumentException if {@code w} and {@code h} + * are not both greater than 0 + */ + Raster.createPackedRaster(dBuffer, 0, 1, 1, bandMasks1, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for zero w / h"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if {@code w} * {@code h} + * is greater than {@code Integer.MAX_VALUE} + */ + Raster.createPackedRaster(dBuffer, + Integer.MAX_VALUE/10, Integer.MAX_VALUE/10, + 1, bandMasks1, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for overflow"); + System.out.println(t); + } + + try { + /* @throws RasterFormatException if computing either + * {@code location.x + w} or + * {@code location.y + h} results in integer overflow + */ + Point pt = new Point(5, 1); + Raster.createPackedRaster(dBuffer, + Integer.MAX_VALUE-2, 1, + 1, bandMasks1, pt); + noException(); + } catch (RasterFormatException t) { + System.out.println("Got expected exception for overflow"); + System.out.println(t); + } + + try { + /* @throws NullPointerException if databuffer is null. + */ + Raster.createPackedRaster(null, + 1, 1, + 1, bandMasks1, null); + noException(); + } catch (NullPointerException t) { + System.out.println( + "Got expected exception for null data buffer"); + System.out.println(t); + } + + try { + /* @throws RasterFormatException if {@code dataBuffer} + * has more than one bank. + */ + DataBufferByte dbuffer2 = new DataBufferByte(20, 2); + Raster.createPackedRaster(dbuffer2, 1, 1, 1, bandMasks1, null); + noException(); + } catch (RasterFormatException t) { + System.out.println( + "Got expected exception for bad databuffer banks"); + System.out.println(t); + } + try { + /* @throws IllegalArgumentException if {@code dataBuffer} + * is not one of the supported data types + */ + DataBufferFloat dbFloat = new DataBufferFloat(20); + Raster.createPackedRaster(dbFloat, 1, 1, 1, bandMasks1, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for bad databuffer type"); + System.out.println(t); + } + } + + /* createPackedRaster(DataBuffer dataBuffer, + * int w, int h, + * int bitsPerPixel, + * Point location) + */ + static void packedRasterTests4() { + + System.out.println(); + System.out.println("** packedRasterTests4"); + + try { + /* @throws IllegalArgumentException if {@code w} and {@code h} + * are not both greater than 0 + */ + Raster.createPackedRaster(dBuffer, 0, 1, 8, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for zero w / h"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if {@code w} * {@code h} + * is greater than {@code Integer.MAX_VALUE} + */ + Raster.createPackedRaster(dBuffer, + Integer.MAX_VALUE/10, Integer.MAX_VALUE/10, + 8, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for overflow"); + System.out.println(t); + } + + try { + /* @throws RasterFormatException if computing either + * {@code location.x + w} or + * {@code location.y + h} results in integer overflow + */ + Point pt = new Point(5, 1); + Raster.createPackedRaster(dBuffer, + Integer.MAX_VALUE-2, 1, + 8, pt); + noException(); + } catch (RasterFormatException t) { + System.out.println("Got expected exception for overflow"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if {@code dataBuffer} + * is not one of the supported data types + */ + DataBufferFloat dbFloat = new DataBufferFloat(20); + Raster.createPackedRaster(dbFloat, 1, 1, 8, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for bad databuffer type"); + System.out.println(t); + } + + try { + /* @throws RasterFormatException if {@code dataBuffer} + * has more than one bank. + */ + DataBufferByte dbb = new DataBufferByte(100, 2); + Raster.createPackedRaster(dbb, 1, 1, 8, null); + noException(); + } catch (RasterFormatException t) { + System.out.println( + "Got expected exception for bad databuffer banks"); + System.out.println(t); + } + + try { + /* @throws NullPointerException if databuffer is null. + */ + Raster.createPackedRaster(null, 1, 1, 8, null); + noException(); + } catch (NullPointerException t) { + System.out.println( + "Got expected exception for null data buffer"); + System.out.println(t); + } + + int[] badbpp = { 0, 6, 16 }; + for (int bpp : badbpp) { + try { + /* @throws RasterFormatException if {@code bitsPixel} is less than 1 or + * not a power of 2 or exceeds the {@code dataBuffer} element size. + */ + System.out.println("Test bpp=" + bpp); + Raster.createPackedRaster(dBuffer, 1, 1, bpp, null); + noException(); + } catch (RasterFormatException t) { + System.out.println( + "Got expected exception for bitsPerPixel"); + System.out.println(t); + } catch (ArithmeticException t) { + checkIsOldVersion(26, t); + if (bpp != 0) { + throw new RuntimeException("Unexpected arithmetic exception"); + } + System.out.println("Got expected arithmetic exception"); + System.out.println(t); + } + } } } From 7ba884923143efb985dc7e0db92cd75c9b516823 Mon Sep 17 00:00:00 2001 From: prrace Date: Fri, 3 Oct 2025 12:00:57 -0700 Subject: [PATCH 2/4] 8369129 --- test/jdk/java/awt/image/Raster/CreateRasterExceptionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/jdk/java/awt/image/Raster/CreateRasterExceptionTest.java b/test/jdk/java/awt/image/Raster/CreateRasterExceptionTest.java index 775d1513db7ec..671d12423b8a8 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 + * @bug 8255800 8369129 * @summary verify Raster + SampleModel creation vs spec. */ From f00d5bc6dac0fcccb2383906aafe6cb25273ed15 Mon Sep 17 00:00:00 2001 From: prrace Date: Fri, 3 Oct 2025 12:26:40 -0700 Subject: [PATCH 3/4] 8369129 --- src/java.desktop/share/classes/java/awt/image/Raster.java | 3 +++ 1 file changed, 3 insertions(+) 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 c33e12e7c9ad6..632f73fc34796 100644 --- a/src/java.desktop/share/classes/java/awt/image/Raster.java +++ b/src/java.desktop/share/classes/java/awt/image/Raster.java @@ -1054,6 +1054,9 @@ public static WritableRaster createPackedRaster(DataBuffer dataBuffer, * @throws IllegalArgumentException if computing either * {@code location.x + w} or * {@code location.y + h} results in integer overflow + * @throws RasterFormatException if computing either + * {@code location.x + w} or + * {@code location.y + h} results in integer overflow * @throws IllegalArgumentException if {@code dataType} is not * one of the supported data types, which are * {@code DataBuffer.TYPE_BYTE}, From b95c4ad04e5b4affec281617d2808096f1e6a57f Mon Sep 17 00:00:00 2001 From: prrace Date: Fri, 3 Oct 2025 12:32:29 -0700 Subject: [PATCH 4/4] 8369129 --- src/java.desktop/share/classes/java/awt/image/Raster.java | 3 --- 1 file changed, 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 632f73fc34796..b4d566844c8e4 100644 --- a/src/java.desktop/share/classes/java/awt/image/Raster.java +++ b/src/java.desktop/share/classes/java/awt/image/Raster.java @@ -1051,9 +1051,6 @@ public static WritableRaster createPackedRaster(DataBuffer dataBuffer, * are not both greater than 0 * @throws IllegalArgumentException if the product of {@code w} * and {@code h} is greater than {@code Integer.MAX_VALUE} - * @throws IllegalArgumentException if computing either - * {@code location.x + w} or - * {@code location.y + h} results in integer overflow * @throws RasterFormatException if computing either * {@code location.x + w} or * {@code location.y + h} results in integer overflow