# HG changeset patch # User bae # Date 1285742642 -14400 # Node ID 8bb1126f01d3f3fa2aefa2024fb8851da2b4412c # Parent f960f117f1623629f64203e2b09a92a8f6f14ff5 6735275: java.awt.image.SampleModel.getSamples() methods not allways throw ArrayIndexOutOfBoundsException Reviewed-by: igor, prr diff -r f960f117f162 -r 8bb1126f01d3 jdk/src/share/classes/java/awt/image/SampleModel.java --- a/jdk/src/share/classes/java/awt/image/SampleModel.java Wed Jul 05 17:23:18 2017 +0200 +++ b/jdk/src/share/classes/java/awt/image/SampleModel.java Wed Sep 29 10:44:02 2010 +0400 @@ -937,14 +937,22 @@ int iArray[], DataBuffer data) { int pixels[]; int Offset=0; + int x1 = x + w; + int y1 = y + h; + + if (x < 0 || x1 < x || x1 > width || + y < 0 || y1 < y || y1 > height) + { + throw new ArrayIndexOutOfBoundsException("Invalid coordinates."); + } if (iArray != null) pixels = iArray; else pixels = new int[w * h]; - for(int i=y; i<(h+y); i++) { - for (int j=x; j<(w+x); j++) { + for(int i=y; i width || + y < 0 || y1 < y || y1 > height) + { + throw new ArrayIndexOutOfBoundsException("Invalid coordinates"); + } if (fArray != null) pixels = fArray; else pixels = new float[w * h]; - for (int i=y; i<(h+y); i++) { - for (int j=x; j<(w+x); j++) { + for (int i=y; i width || + y < 0 || y1 < y || y1 > height) + { + throw new ArrayIndexOutOfBoundsException("Invalid coordinates"); + } if (dArray != null) pixels = dArray; else pixels = new double[w * h]; - for (int i=y; i<(y+h); i++) { - for (int j=x; j<(x+w); j++) { + for (int i=y; i> classes = new Vector>(); + + classes.add(ComponentSampleModel.class); + classes.add(MultiPixelPackedSampleModel.class); + classes.add(SinglePixelPackedSampleModel.class); + classes.add(BandedSampleModel.class); + classes.add(PixelInterleavedSampleModel.class); + + for (Class c : classes) { + doTest(c); + } + } + private static void doTest(Class c) { + System.out.println("Test for: " + c.getName()); + SampleModel sm = createSampleModel(c); + + DataBuffer db = sm.createDataBuffer(); + + int[] iArray = new int[ width * height + numBands]; + float[] fArray = new float[ width * height + numBands]; + double[] dArray = new double[ width * height + numBands]; + + boolean iOk = false; + boolean fOk = false; + boolean dOk = false; + + try { + sm.getSamples(Integer.MAX_VALUE, 0, 1, 1, 0, iArray, db); + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println(e.getMessage()); + iOk = true; + } + + try { + sm.getSamples(Integer.MAX_VALUE, 0, 1, 1, 0, fArray, db); + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println(e.getMessage()); + fOk = true; + } + + try { + sm.getSamples(0, Integer.MAX_VALUE, 1, 1, 0, dArray, db); + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println(e.getMessage()); + dOk = true; + } + if (!iOk || !fOk || !dOk) { + throw new RuntimeException("Test for " + c.getSimpleName() + + " failed: iOk=" + iOk + "; fOk=" + fOk + "; dOk=" + dOk); + } + } + + private static SampleModel createSampleModel(Class cls) { + SampleModel res = null; + + if (cls == ComponentSampleModel.class) { + res = new ComponentSampleModel(dataType, width, height, 4, width * 4, new int[] { 0, 1, 2, 3 } ); + } else if (cls == MultiPixelPackedSampleModel.class) { + res = new MultiPixelPackedSampleModel(dataType, width, height, 4); + } else if (cls == SinglePixelPackedSampleModel.class) { + res = new SinglePixelPackedSampleModel(dataType, width, height, + new int[]{ 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff }); + } else if (cls == BandedSampleModel.class) { + res = new BandedSampleModel(dataType, width, height, numBands); + } else if (cls == PixelInterleavedSampleModel.class) { + res = new PixelInterleavedSampleModel(dataType, width, height, 4, width * 4, new int[] { 0, 1, 2, 3 }); + } else { + throw new RuntimeException("Unknown class " + cls); + } + return res; + } +}