6773586: java.awt.image.SampleModel.getPixels() methods not allways throw ArrayIndexOutOfBoundsException
authorbae
Tue, 22 Mar 2011 12:28:03 +0300
changeset 8938 8e3cd84800e3
parent 8937 38ffd0c8fd2b
child 8939 04615dca2a76
6773586: java.awt.image.SampleModel.getPixels() methods not allways throw ArrayIndexOutOfBoundsException Reviewed-by: jgodinez, prr
jdk/src/share/classes/java/awt/image/BandedSampleModel.java
jdk/src/share/classes/java/awt/image/ComponentSampleModel.java
jdk/src/share/classes/java/awt/image/SampleModel.java
jdk/src/share/classes/java/awt/image/SinglePixelPackedSampleModel.java
--- a/jdk/src/share/classes/java/awt/image/BandedSampleModel.java	Tue Mar 22 11:22:38 2011 +0300
+++ b/jdk/src/share/classes/java/awt/image/BandedSampleModel.java	Tue Mar 22 12:28:03 2011 +0300
@@ -408,7 +408,12 @@
      */
     public int[] getPixels(int x, int y, int w, int h,
                            int iArray[], DataBuffer data) {
-        if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
+        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
                 ("Coordinate out of bounds!");
         }
@@ -690,7 +695,12 @@
      */
     public void setPixels(int x, int y, int w, int h,
                           int iArray[], DataBuffer data) {
-        if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
+        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
                 ("Coordinate out of bounds!");
         }
--- a/jdk/src/share/classes/java/awt/image/ComponentSampleModel.java	Tue Mar 22 11:22:38 2011 +0300
+++ b/jdk/src/share/classes/java/awt/image/ComponentSampleModel.java	Tue Mar 22 12:28:03 2011 +0300
@@ -739,7 +739,12 @@
      */
     public int[] getPixels(int x, int y, int w, int h,
                            int iArray[], DataBuffer data) {
-        if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
+        int x1 = x + w;
+        int y1 = y + h;
+
+        if (x < 0 || x >= width || w > width || x1 < 0 || x1 > width ||
+            y < 0 || y >= height || y > height || y1 < 0 || y1 >  height)
+        {
             throw new ArrayIndexOutOfBoundsException
                 ("Coordinate out of bounds!");
         }
@@ -1025,7 +1030,12 @@
      */
     public void setPixels(int x, int y, int w, int h,
                           int iArray[], DataBuffer data) {
-        if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
+        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
                 ("Coordinate out of bounds!");
         }
--- a/jdk/src/share/classes/java/awt/image/SampleModel.java	Tue Mar 22 11:22:38 2011 +0300
+++ b/jdk/src/share/classes/java/awt/image/SampleModel.java	Tue Mar 22 12:28:03 2011 +0300
@@ -759,14 +759,22 @@
 
         int pixels[];
         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.");
+        }
 
         if (iArray != null)
             pixels = iArray;
         else
             pixels = new int[numBands * w * h];
 
-        for (int i=y; i<(h+y); i++) {
-            for (int j=x; j<(w+x); j++) {
+        for (int i=y; i<y1; i++) {
+            for (int j=x; j<x1; j++) {
                 for(int k=0; k<numBands; k++) {
                     pixels[Offset++] = getSample(j, i, k, data);
                 }
@@ -799,14 +807,22 @@
 
         float pixels[];
         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.");
+        }
 
         if (fArray != null)
             pixels = fArray;
         else
             pixels = new float[numBands * w * h];
 
-        for (int i=y; i<(h+y); i++) {
-            for(int j=x; j<(w+x); j++) {
+        for (int i=y; i<y1; i++) {
+            for(int j=x; j<x1; j++) {
                 for(int k=0; k<numBands; k++) {
                     pixels[Offset++] = getSampleFloat(j, i, k, data);
                 }
@@ -838,6 +854,14 @@
                               double dArray[], DataBuffer data) {
         double pixels[];
         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.");
+        }
 
         if (dArray != null)
             pixels = dArray;
@@ -845,8 +869,8 @@
             pixels = new double[numBands * w * h];
 
         // Fix 4217412
-        for (int i=y; i<(h+y); i++) {
-            for (int j=x; j<(w+x); j++) {
+        for (int i=y; i<y1; i++) {
+            for (int j=x; j<x1; j++) {
                 for (int k=0; k<numBands; k++) {
                     pixels[Offset++] = getSampleDouble(j, i, k, data);
                 }
@@ -1146,9 +1170,17 @@
     public void setPixels(int x, int y, int w, int h,
                           int iArray[], 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++) {
                 for (int k=0; k<numBands; k++) {
                     setSample(j, i, k, iArray[Offset++], data);
                 }
@@ -1176,9 +1208,17 @@
     public void setPixels(int x, int y, int w, int h,
                           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++) {
                 for(int k=0; k<numBands; k++) {
                     setSample(j, i, k, fArray[Offset++], data);
                 }
@@ -1206,9 +1246,17 @@
     public void setPixels(int x, int y, int w, int h,
                           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++) {
                 for (int k=0; k<numBands; k++) {
                     setSample(j, i, k, dArray[Offset++], data);
                 }
--- a/jdk/src/share/classes/java/awt/image/SinglePixelPackedSampleModel.java	Tue Mar 22 11:22:38 2011 +0300
+++ b/jdk/src/share/classes/java/awt/image/SinglePixelPackedSampleModel.java	Tue Mar 22 12:28:03 2011 +0300
@@ -461,7 +461,12 @@
      */
     public int[] getPixels(int x, int y, int w, int h,
                            int iArray[], DataBuffer data) {
-        if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
+        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
                 ("Coordinate out of bounds!");
         }
@@ -659,7 +664,12 @@
      */
     public void setPixels(int x, int y, int w, int h,
                           int iArray[], DataBuffer data) {
-        if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
+        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
                 ("Coordinate out of bounds!");
         }