changeset 9618:5c5645171558

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
author msheppar
date Thu, 29 May 2014 13:08:02 +0100
parents 31bf32b1d2ec
children 7a56aaa09ab1
files src/solaris/native/java/net/NetworkInterface.c src/windows/native/java/net/NetworkInterface.c
diffstat 2 files changed, 62 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/src/solaris/native/java/net/NetworkInterface.c	Thu May 29 01:50:50 2014 +0400
+++ b/src/solaris/native/java/net/NetworkInterface.c	Thu May 29 13:08:02 2014 +0100
@@ -250,7 +250,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
      */
@@ -518,7 +522,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;
@@ -565,6 +573,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);
@@ -588,7 +601,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;
@@ -632,10 +649,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);
@@ -674,6 +690,8 @@
             iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
             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) {
@@ -684,10 +702,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;
             }
         }
 
@@ -707,20 +729,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;
     }
@@ -912,9 +934,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;
+       }
      }
 
     /**
@@ -1396,6 +1423,7 @@
     nddp = (struct kinfo_ndd *)malloc(size);
 
     if (!nddp) {
+        JNU_ThrowOutOfMemoryError(env, "Network interface getMacAddress native buffer allocation failed");
         return -1;
     }
 
--- a/src/windows/native/java/net/NetworkInterface.c	Thu May 29 01:50:50 2014 +0400
+++ b/src/windows/native/java/net/NetworkInterface.c	Thu May 29 13:08:02 2014 +0100
@@ -567,16 +567,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);
@@ -706,24 +706,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);