< prev index next >

src/java.base/unix/native/libnet/NetworkInterface.c

Print this page
rev 16158 : 8170544: Fix code scan findings in libnet

@@ -769,11 +769,16 @@
 static netif *enumInterfaces(JNIEnv *env) {
     netif *ifs = NULL;
     int sock;
 
     sock = openSocket(env, AF_INET);
-    if (sock < 0 && (*env)->ExceptionOccurred(env)) {
+    if (sock < 0) {
+        return NULL;
+    } else if ((*env)->ExceptionOccurred(env)) {
+        if (sock > 0) {
+            close(sock);
+        }
         return NULL;
     }
 
     // enumerate IPv4 addresses
     ifs = enumIPv4Interfaces(env, sock, NULL);

@@ -787,11 +792,17 @@
     // If IPv6 is available then enumerate IPv6 addresses.
     // User can disable ipv6 explicitly by -Djava.net.preferIPv4Stack=true,
     // so we have to call ipv6_available()
     if (ipv6_available()) {
         sock = openSocket(env, AF_INET6);
-        if (sock < 0 && (*env)->ExceptionOccurred(env)) {
+        if (sock < 0) {
+            freeif(ifs);
+            return NULL;
+        } else if ((*env)->ExceptionOccurred(env)) {
+            if (sock > 0) {
+                close(sock);
+            }
             freeif(ifs);
             return NULL;
         }
 
         ifs = enumIPv6Interfaces(env, sock, ifs);
< prev index next >