From e94c501b5d160f84575faf6f60ab16f08adb65fb Mon Sep 17 00:00:00 2001 From: msheppar Date: Thu, 29 May 2014 13:08:02 +0100 Subject: [PATCH] 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 --- .../native/java/net/NetworkInterface.c | 52 ++++++++++++++----- .../native/java/net/NetworkInterface.c | 37 +++++++------ 2 files changed, 61 insertions(+), 28 deletions(-) diff --git a/src/solaris/native/java/net/NetworkInterface.c b/src/solaris/native/java/net/NetworkInterface.c index 16acf07a2..7ee0c38c6 100644 --- a/src/solaris/native/java/net/NetworkInterface.c +++ b/src/solaris/native/java/net/NetworkInterface.c @@ -250,7 +250,11 @@ JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0 } 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 @@ JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0(JNIEnv * 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 @@ JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0(JNIEnv *env, jclas 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 @@ static int getFlags0(JNIEnv *env, jstring name) { 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 @@ jobject createNetworkInterface(JNIEnv *env, netif *ifs) { * 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 @@ jobject createNetworkInterface(JNIEnv *env, netif *ifs) { 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 @@ jobject createNetworkInterface(JNIEnv *env, netif *ifs) { 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 @@ jobject createNetworkInterface(JNIEnv *env, netif *ifs) { 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 @@ netif *addif(JNIEnv *env, int sock, const char * if_name, // 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 @@ static int getMacAddress(JNIEnv *env, int sock, const char* ifname, const struct nddp = (struct kinfo_ndd *)malloc(size); if (!nddp) { + JNU_ThrowOutOfMemoryError(env, "Network interface getMacAddress native buffer allocation failed"); return -1; } diff --git a/src/windows/native/java/net/NetworkInterface.c b/src/windows/native/java/net/NetworkInterface.c index eda951a6d..c436e8669 100644 --- a/src/windows/native/java/net/NetworkInterface.c +++ b/src/windows/native/java/net/NetworkInterface.c @@ -567,16 +567,16 @@ jobject createNetworkInterface * 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,23 +706,28 @@ JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0 /* 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); - } + /* 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 UTF string */ + (*env)->ReleaseStringUTFChars(env, name, name_utf); + } else { + if (!(*env)->ExceptionCheck(env)) + JNU_ThrowOutOfMemoryError(env, NULL); + } /* release the interface list */ free_netif(ifList); -- GitLab