6993561: java.awt.image.SampleModel.setSamples() methods not always throw ArrayIndexOutOfBoundsException
authorbae
Tue, 22 Mar 2011 11:22:38 +0300
changeset 8937 38ffd0c8fd2b
parent 8751 f317a5eb26da
child 8938 8e3cd84800e3
6993561: java.awt.image.SampleModel.setSamples() methods not always throw ArrayIndexOutOfBoundsException Reviewed-by: jgodinez, prr
jdk/src/share/classes/java/awt/image/SampleModel.java
jdk/test/java/awt/image/GetSamplesTest.java
--- a/jdk/src/share/classes/java/awt/image/SampleModel.java	Fri Mar 18 23:33:34 2011 -0700
+++ b/jdk/src/share/classes/java/awt/image/SampleModel.java	Tue Mar 22 11:22:38 2011 +0300
@@ -1315,9 +1315,16 @@
                            int iArray[], DataBuffer data) {
 
         int Offset=0;
+        int x1 = x + w;
+        int y1 = y + h;
+        if (x < 0 || x >= width || w > width || x1 < 0 || x1 > width ||
+            y < 0 || y >= height || h > height || y1 < 0 || y1 > height)
+        {
+            throw new ArrayIndexOutOfBoundsException("Invalid coordinates.");
+        }
 
-        for (int i=y; i<(y+h); i++) {
-            for (int j=x; j<(x+w); j++) {
+        for (int i=y; i<y1; i++) {
+            for (int j=x; j<x1; j++) {
                 setSample(j, i, b, iArray[Offset++], data);
             }
         }
@@ -1345,9 +1352,17 @@
     public void setSamples(int x, int y, int w, int h, int b,
                            float fArray[], DataBuffer data) {
         int Offset=0;
+        int x1 = x + w;
+        int y1 = y + h;
 
-        for (int i=y; i<(y+h); i++) {
-            for (int j=x; j<(x+w); j++) {
+        if (x < 0 || x >= width || w > width || x1 < 0 || x1 > width ||
+            y < 0 || y >= height || h > height || y1 < 0 || y1 > height)
+        {
+            throw new ArrayIndexOutOfBoundsException("Invalid coordinates.");
+        }
+
+        for (int i=y; i<y1; i++) {
+            for (int j=x; j<x1; j++) {
                 setSample(j, i, b, fArray[Offset++], data);
             }
         }
@@ -1375,9 +1390,18 @@
     public void setSamples(int x, int y, int w, int h, int b,
                            double dArray[], DataBuffer data) {
         int Offset=0;
+        int x1 = x + w;
+        int y1 = y + h;
 
-        for (int i=y; i<(y+h); i++) {
-            for (int j=x; j<(x+w); j++) {
+
+        if (x < 0 || x >= width || w > width || x1 < 0 || x1 > width ||
+            y < 0 || y >= height || h > height || y1 < 0 || y1 > height)
+        {
+            throw new ArrayIndexOutOfBoundsException("Invalid coordinates.");
+        }
+
+        for (int i=y; i<y1; i++) {
+            for (int j=x; j<x1; j++) {
                 setSample(j, i, b, dArray[Offset++], data);
             }
         }
--- a/jdk/test/java/awt/image/GetSamplesTest.java	Fri Mar 18 23:33:34 2011 -0700
+++ b/jdk/test/java/awt/image/GetSamplesTest.java	Tue Mar 22 11:22:38 2011 +0300
@@ -23,9 +23,9 @@
 
 /*
  * @test
- * @bug     6735275
- * @summary Test verifies that SampleModel.getSamples() throws an appropriate
- *           exception if coordinates are not in bounds.
+ * @bug     6735275 6993561
+ * @summary Test verifies that SampleModel.getSamples() SampleModel.setSamples()
+ *          throw an appropriate exception if coordinates are not in bounds.
  *
  * @run     main GetSamplesTest
  */
@@ -75,6 +75,7 @@
 
         try {
             sm.getSamples(Integer.MAX_VALUE, 0, 1, 1, 0, iArray, db);
+            sm.setSamples(Integer.MAX_VALUE, 0, 1, 1, 0, iArray, db);
         } catch (ArrayIndexOutOfBoundsException e) {
             System.out.println(e.getMessage());
             iOk = true;
@@ -82,6 +83,7 @@
 
         try {
             sm.getSamples(Integer.MAX_VALUE, 0, 1, 1, 0, fArray, db);
+            sm.setSamples(Integer.MAX_VALUE, 0, 1, 1, 0, fArray, db);
         } catch (ArrayIndexOutOfBoundsException e) {
             System.out.println(e.getMessage());
             fOk = true;
@@ -89,6 +91,7 @@
 
         try {
             sm.getSamples(0, Integer.MAX_VALUE, 1, 1, 0, dArray, db);
+            sm.setSamples(0, Integer.MAX_VALUE, 1, 1, 0, dArray, db);
         } catch (ArrayIndexOutOfBoundsException e) {
             System.out.println(e.getMessage());
             dOk = true;