8025293: JNI exception pending checks in java.net
authormsheppar
Fri, 21 Mar 2014 00:01:59 +0000
changeset 23563 71ed058849d4
parent 23562 596efd9dfcaf
child 23564 62146f638e22
8025293: JNI exception pending checks in java.net Summary: enhance the return check for JNI native calls, check for NULL and pending exceptions Reviewed-by: alanb, chegar
jdk/src/solaris/native/java/net/NetworkInterface.c
jdk/src/windows/native/java/net/NetworkInterface.c
--- a/jdk/src/solaris/native/java/net/NetworkInterface.c	Thu Mar 20 23:34:38 2014 +0000
+++ b/jdk/src/solaris/native/java/net/NetworkInterface.c	Fri Mar 21 00:01:59 2014 +0000
@@ -231,7 +231,11 @@
     }
 
     name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
-
+    if (name_utf == NULL) {
+       if (!(*env)->ExceptionCheck(env))
+           JNU_ThrowOutOfMemoryError(env, NULL);
+       return NULL;
+    }
     /*
      * Search the list of interface based on name
      */
@@ -499,7 +503,11 @@
     const char* name_utf;
 
     name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
-
+    if (name_utf == NULL) {
+       if (!(*env)->ExceptionCheck(env))
+           JNU_ThrowOutOfMemoryError(env, NULL);
+       return NULL;
+    }
     if ((sock =openSocketWithFallback(env, name_utf)) < 0) {
        (*env)->ReleaseStringUTFChars(env, name, name_utf);
        return JNI_FALSE;
@@ -546,6 +554,11 @@
     const char* name_utf;
 
     name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
+    if (name_utf == NULL) {
+       if (!(*env)->ExceptionCheck(env))
+           JNU_ThrowOutOfMemoryError(env, NULL);
+       return ret;
+    }
 
     if ((sock =openSocketWithFallback(env, name_utf)) < 0) {
        (*env)->ReleaseStringUTFChars(env, name, name_utf);
@@ -569,7 +582,11 @@
     int flags = 0;
 
     name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
-
+    if (name_utf == NULL) {
+       if (!(*env)->ExceptionCheck(env))
+           JNU_ThrowOutOfMemoryError(env, NULL);
+       return -1;
+    }
     if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
         (*env)->ReleaseStringUTFChars(env, name, name_utf);
         return -1;
@@ -613,10 +630,9 @@
      * Create a NetworkInterface object and populate it
      */
     netifObj = (*env)->NewObject(env, ni_class, ni_ctrID);
+    CHECK_NULL_RETURN(netifObj, NULL);
     name = (*env)->NewStringUTF(env, ifs->name);
-    if (netifObj == NULL || name == NULL) {
-        return NULL;
-    }
+    CHECK_NULL_RETURN(name, NULL);
     (*env)->SetObjectField(env, netifObj, ni_nameID, name);
     (*env)->SetObjectField(env, netifObj, ni_descID, name);
     (*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
@@ -655,6 +671,8 @@
             iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
             if (iaObj) {
                  setInetAddress_addr(env, iaObj, htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr));
+            } else {
+                return NULL;
             }
             ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
             if (ibObj) {
@@ -665,10 +683,14 @@
                     if (ia2Obj) {
                        setInetAddress_addr(env, ia2Obj, htonl(((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
                        (*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj);
+                    } else {
+                        return NULL;
                     }
                  }
                  (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
                  (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
+            } else {
+                return NULL;
             }
         }
 
@@ -688,20 +710,20 @@
                     setInet6Address_scopeid(env, iaObj, scope);
                     setInet6Address_scopeifname(env, iaObj, netifObj);
                 }
+            } else {
+                return NULL;
             }
             ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
             if (ibObj) {
                 (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
                 (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
                 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
+            } else {
+                return NULL;
             }
         }
 #endif
 
-        if (iaObj == NULL) {
-            return NULL;
-        }
-
         (*env)->SetObjectArrayElement(env, addrArr, addr_index++, iaObj);
         addrP = addrP->next;
     }
@@ -893,9 +915,14 @@
        // Deal with broadcast addr & subnet mask
        struct sockaddr * brdcast_to = (struct sockaddr *) ((char *) addrP + sizeof(netaddr) + addr_size);
        addrP->brdcast = getBroadcast(env, sock, name,  brdcast_to );
-
-       if ((mask = getSubnet(env, sock, name)) != -1)
+       if ((*env)->ExceptionCheck(env) == JNI_TRUE) {
+           return ifs;
+       }
+       if ((mask = getSubnet(env, sock, name)) != -1) {
            addrP->mask = mask;
+       } else if((*env)->ExceptionCheck(env)) {
+           return ifs;
+       }
      }
 
     /**
@@ -1377,6 +1404,7 @@
     nddp = (struct kinfo_ndd *)malloc(size);
 
     if (!nddp) {
+        JNU_ThrowOutOfMemoryError(env, "Network interface getMacAddress native buffer allocation failed");
         return -1;
     }
 
--- a/jdk/src/windows/native/java/net/NetworkInterface.c	Thu Mar 20 23:34:38 2014 +0000
+++ b/jdk/src/windows/native/java/net/NetworkInterface.c	Fri Mar 21 00:01:59 2014 +0000
@@ -543,16 +543,16 @@
      * Create a NetworkInterface object and populate it
      */
     netifObj = (*env)->NewObject(env, ni_class, ni_ctor);
+    CHECK_NULL_RETURN(netifObj, NULL);
     name = (*env)->NewStringUTF(env, ifs->name);
+    CHECK_NULL_RETURN(name, NULL);
     if (ifs->dNameIsUnicode) {
         displayName = (*env)->NewString(env, (PWCHAR)ifs->displayName,
                                        (jsize)wcslen ((PWCHAR)ifs->displayName));
     } else {
         displayName = (*env)->NewStringUTF(env, ifs->displayName);
     }
-    if (netifObj == NULL || name == NULL || displayName == NULL) {
-        return NULL;
-    }
+    CHECK_NULL_RETURN(displayName, NULL);
     (*env)->SetObjectField(env, netifObj, ni_nameID, name);
     (*env)->SetObjectField(env, netifObj, ni_displayNameID, displayName);
     (*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
@@ -682,24 +682,29 @@
 
     /* get the name as a C string */
     name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
+    if (name_utf != NULL) {
 
-    /* Search by name */
-    curr = ifList;
-    while (curr != NULL) {
-        if (strcmp(name_utf, curr->name) == 0) {
-            break;
+        /* Search by name */
+        curr = ifList;
+        while (curr != NULL) {
+            if (strcmp(name_utf, curr->name) == 0) {
+                break;
+            }
+            curr = curr->next;
         }
-        curr = curr->next;
+
+        /* if found create a NetworkInterface */
+        if (curr != NULL) {;
+            netifObj = createNetworkInterface(env, curr, -1, NULL);
+        }
+
+        /* release the UTF string */
+        (*env)->ReleaseStringUTFChars(env, name, name_utf);
+    } else {
+        if (!(*env)->ExceptionCheck(env))
+            JNU_ThrowOutOfMemoryError(env, NULL);
     }
 
-    /* if found create a NetworkInterface */
-    if (curr != NULL) {;
-        netifObj = createNetworkInterface(env, curr, -1, NULL);
-    }
-
-    /* release the UTF string */
-    (*env)->ReleaseStringUTFChars(env, name, name_utf);
-
     /* release the interface list */
     free_netif(ifList);