Blob Blame History Raw
# HG changeset patch
# User jdowland
# Date 1616424665 0
#      Mon Mar 22 14:51:05 2021 +0000
# Node ID 71c0c9061867c51e4cc59c286eb1a1eae15bc6b5
# Parent  a4cb0f6662eb43880683e1c859238d0d39f07886
8187450: JNI local refs exceeds capacity warning in NetworkInterface::getAll
Reviewed-by: chegar, dfuchs, aph

diff --git a/src/java.base/unix/native/libnet/NetworkInterface.c b/src/java.base/unix/native/libnet/NetworkInterface.c
--- a/src/java.base/unix/native/libnet/NetworkInterface.c
+++ b/src/java.base/unix/native/libnet/NetworkInterface.c
@@ -461,6 +461,7 @@
 
         // put the NetworkInterface into the array
         (*env)->SetObjectArrayElement(env, netIFArr, arr_index++, netifObj);
+        (*env)->DeleteLocalRef(env, netifObj);
 
         curr = curr->next;
     }
@@ -724,12 +725,14 @@
                             ((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
                         JNU_CHECK_EXCEPTION_RETURN(env, NULL);
                         (*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj);
+                        (*env)->DeleteLocalRef(env, ia2Obj);
                     } else {
                         return NULL;
                     }
                 }
                 (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
                 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
+                (*env)->DeleteLocalRef(env, ibObj);
             } else {
                 return NULL;
             }
@@ -758,12 +761,14 @@
                 (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
                 (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
                 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
+                (*env)->DeleteLocalRef(env, ibObj);
             } else {
                 return NULL;
             }
         }
 
         (*env)->SetObjectArrayElement(env, addrArr, addr_index++, iaObj);
+        (*env)->DeleteLocalRef(env, iaObj);
         addrP = addrP->next;
     }
 
@@ -796,6 +801,11 @@
     (*env)->SetObjectField(env, netifObj, ni_bindsID, bindArr);
     (*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
 
+    (*env)->DeleteLocalRef(env, name);
+    (*env)->DeleteLocalRef(env, addrArr);
+    (*env)->DeleteLocalRef(env, bindArr);
+    (*env)->DeleteLocalRef(env, childArr);
+
     // return the NetworkInterface
     return netifObj;
 }
diff --git a/src/java.base/windows/native/libnet/NetworkInterface.c b/src/java.base/windows/native/libnet/NetworkInterface.c
--- a/src/java.base/windows/native/libnet/NetworkInterface.c
+++ b/src/java.base/windows/native/libnet/NetworkInterface.c
@@ -609,8 +609,10 @@
                   return NULL;
               }
               (*env)->SetObjectField(env, ibObj, ni_ibbroadcastID, ia2Obj);
+              (*env)->DeleteLocalRef(env, ia2Obj);
               (*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask);
               (*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj);
+              (*env)->DeleteLocalRef(env, ibObj);
             }
         } else /* AF_INET6 */ {
             int scope;
@@ -635,9 +637,11 @@
                 (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
                 (*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask);
                 (*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj);
+                (*env)->DeleteLocalRef(env, ibObj);
             }
         }
         (*env)->SetObjectArrayElement(env, addrArr, addr_index, iaObj);
+        (*env)->DeleteLocalRef(env, iaObj);
         addrs = addrs->next;
         addr_index++;
     }
@@ -645,6 +649,10 @@
     (*env)->SetObjectField(env, netifObj, ni_bindsID, bindsArr);
 
     free_netaddr(netaddrP);
+    (*env)->DeleteLocalRef(env, name);
+    (*env)->DeleteLocalRef(env, displayName);
+    (*env)->DeleteLocalRef(env, addrArr);
+    (*env)->DeleteLocalRef(env, bindsArr);
 
     /*
      * Windows doesn't have virtual interfaces, so child array
@@ -655,6 +663,7 @@
       return NULL;
     }
     (*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
+    (*env)->DeleteLocalRef(env, childArr);
 
     /* return the NetworkInterface */
     return netifObj;
@@ -875,6 +884,7 @@
 
         /* put the NetworkInterface into the array */
         (*env)->SetObjectArrayElement(env, netIFArr, arr_index++, netifObj);
+        (*env)->DeleteLocalRef(env, netifObj);
 
         curr = curr->next;
     }
diff --git a/test/jdk/java/net/NetworkInterface/Test.java b/test/jdk/java/net/NetworkInterface/Test.java
--- a/test/jdk/java/net/NetworkInterface/Test.java
+++ b/test/jdk/java/net/NetworkInterface/Test.java
@@ -23,8 +23,8 @@
 
 /* @test
  * @bug 4405354 6594296 8058216
- * @run main Test
- * @run main/othervm -Djava.net.preferIPv4Stack=true Test
+ * @run main/othervm -Xcheck:jni Test
+ * @run main/othervm -Xcheck:jni -Djava.net.preferIPv4Stack=true Test
  * @summary Basic tests for NetworkInterface
  */
 import java.net.NetworkInterface;