diff src/share/native/java/io/io_util.c @ 787:ff572b4f1ca4

6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour Reviewed-by: alanb Contributed-by: gbenson@redhat.com
author martin
date Wed, 07 Jan 2009 11:50:32 -0800
parents b6d6877c1155
children 0272e442cc5b
line wrap: on
line diff
--- a/src/share/native/java/io/io_util.c	Wed Jan 07 14:06:04 2009 +0100
+++ b/src/share/native/java/io/io_util.c	Wed Jan 07 11:50:32 2009 -0800
@@ -58,12 +58,24 @@
  */
 #define BUF_SIZE 8192
 
+/*
+ * Returns true if the array slice defined by the given offset and length
+ * is out of bounds.
+ */
+static int
+outOfBounds(JNIEnv *env, jint off, jint len, jbyteArray array) {
+    return ((off < 0) ||
+            (len < 0) ||
+            // We are very careful to avoid signed integer overflow,
+            // the result of which is undefined in C.
+            ((*env)->GetArrayLength(env, array) - off < len));
+}
 
 int
 readBytes(JNIEnv *env, jobject this, jbyteArray bytes,
           jint off, jint len, jfieldID fid)
 {
-    int nread, datalen;
+    int nread;
     char stackBuf[BUF_SIZE];
     char *buf = 0;
     FD fd;
@@ -72,10 +84,8 @@
         JNU_ThrowNullPointerException(env, 0);
         return -1;
     }
-    datalen = (*env)->GetArrayLength(env, bytes);
 
-    if ((off < 0) || (off > datalen) ||
-        (len < 0) || ((off + len) > datalen) || ((off + len) < 0)) {
+    if (outOfBounds(env, off, len, bytes)) {
         JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", 0);
         return -1;
     }
@@ -136,7 +146,7 @@
 writeBytes(JNIEnv *env, jobject this, jbyteArray bytes,
           jint off, jint len, jfieldID fid)
 {
-    int n, datalen;
+    int n;
     char stackBuf[BUF_SIZE];
     char *buf = 0;
     FD fd;
@@ -145,10 +155,8 @@
         JNU_ThrowNullPointerException(env, 0);
         return;
     }
-    datalen = (*env)->GetArrayLength(env, bytes);
 
-    if ((off < 0) || (off > datalen) ||
-        (len < 0) || ((off + len) > datalen) || ((off + len) < 0)) {
+    if (outOfBounds(env, off, len, bytes)) {
         JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", 0);
         return;
     }