6773022: java.awt.image.SampleModel.getDataElements() does't throw ArrayIndexOutOfBoundsEx for Integer.MAX_V
authorbae
Fri, 15 Oct 2010 11:26:43 +0400
changeset 6815 eba82865cf06
parent 6814 c6e347fb5b20
child 6816 6c2a27114265
6773022: java.awt.image.SampleModel.getDataElements() does't throw ArrayIndexOutOfBoundsEx for Integer.MAX_V Reviewed-by: igor, prr
jdk/src/share/classes/java/awt/image/SampleModel.java
jdk/test/java/awt/image/GetDataElementsTest.java
--- a/jdk/src/share/classes/java/awt/image/SampleModel.java	Fri Oct 15 10:42:39 2010 +0400
+++ b/jdk/src/share/classes/java/awt/image/SampleModel.java	Fri Oct 15 11:26:43 2010 +0400
@@ -358,6 +358,15 @@
         int cnt = 0;
         Object o = null;
 
+        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.");
+        }
+
         switch(type) {
 
         case DataBuffer.TYPE_BYTE:
@@ -370,8 +379,8 @@
             else
                 bdata = (byte[])obj;
 
-            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++) {
                     o = getDataElements(j, i, o, data);
                     btemp = (byte[])o;
                     for (int k=0; k<numDataElems; k++) {
@@ -393,8 +402,8 @@
             else
                 sdata = (short[])obj;
 
-            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++) {
                     o = getDataElements(j, i, o, data);
                     stemp = (short[])o;
                     for (int k=0; k<numDataElems; k++) {
@@ -416,8 +425,8 @@
             else
                 idata = (int[])obj;
 
-            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++) {
                     o = getDataElements(j, i, o, data);
                     itemp = (int[])o;
                     for (int k=0; k<numDataElems; k++) {
@@ -439,8 +448,8 @@
             else
                 fdata = (float[])obj;
 
-            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++) {
                     o = getDataElements(j, i, o, data);
                     ftemp = (float[])o;
                     for (int k=0; k<numDataElems; k++) {
@@ -462,8 +471,8 @@
             else
                 ddata = (double[])obj;
 
-            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++) {
                     o = getDataElements(j, i, o, data);
                     dtemp = (double[])o;
                     for (int k=0; k<numDataElems; k++) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/image/GetDataElementsTest.java	Fri Oct 15 11:26:43 2010 +0400
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2010, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug     6773022
+ * @summary Test verifies that SampleModel.getDataElements() throws an appropriate
+ *           exception if coordinates are not in bounds.
+ *
+ * @run     main GetDataElementsTest
+ */
+
+import java.awt.image.ComponentSampleModel;
+import java.awt.image.DataBuffer;
+import java.awt.image.SampleModel;
+
+public class GetDataElementsTest {
+
+    public static int width = 100;
+    public static int height = 100;
+    public static int dataType = DataBuffer.TYPE_BYTE;
+    public static int numBands = 4;
+
+    public static void main(String[] args) {
+        SampleModel sm = new ComponentSampleModel(dataType, width, height, 4, width * 4, new int[] { 0, 1, 2, 3 } );
+
+        DataBuffer db = sm.createDataBuffer();
+        Object o = null;
+
+        boolean testPassed = false;
+        try {
+            o = sm.getDataElements(Integer.MAX_VALUE, 0, 1, 1, o, db);
+        } catch (ArrayIndexOutOfBoundsException e) {
+            System.out.println(e.getMessage());
+            testPassed = true;
+        }
+
+        if (!testPassed) {
+            throw new RuntimeException("Excpected excprion was not thrown.");
+        }
+    }
+}