Repository: netty/netty
Issue: NetworkInterface.getNetworkInterfaces() can return null

To my suprise NetworkInterface.getNetworkInterfaces() can return null:
```
    public static Enumeration<NetworkInterface> getNetworkInterfaces()
        throws SocketException {
        final NetworkInterface[] netifs = getAll();

        // specified to return null if no network interfaces
        if (netifs == null)
            return null;
```

(not sure where its "specified")

https://github.com/netty/netty/blob/42c035982093d6e08201af9572ea43c7cf269adf/common/src/main/java/io/netty/util/internal/MacAddressUtil.java#L55

https://github.com/netty/netty/blob/3344cd21acc61819c327976ed5281daa49b0c05e/common/src/main/java/io/netty/util/NetUtil.java#L166

Issue comments:
(derived from a bug report by a customer, not sure how they ended up having no network interfaces, also not sure if its relevant to use netty if you do not have network interfaces :))
@CodingFabian let me fix this
Fixed by https://github.com/netty/netty/issues/6389

Pull request review comments:
remove?
+1
![MINOR](https://raw.githubusercontent.com/SonarCommunity/sonar-github/master/images/severity-minor.png) Remove this unused import 'java.util.Collections'. [![rule](https://raw.githubusercontent.com/SonarCommunity/sonar-github/master/images/rule.png)](https://garage.netty.io/sonarqube/coding_rules#rule_key=squid%3AUselessImportCheck)

fixed

Patch that resolved the issue:
diff --git a/common/src/main/java/io/netty/util/NetUtil.java b/common/src/main/java/io/netty/util/NetUtil.java
index 97570fa165d..88d4cdd015c 100644
--- a/common/src/main/java/io/netty/util/NetUtil.java
+++ b/common/src/main/java/io/netty/util/NetUtil.java
@@ -163,11 +163,14 @@ public final class NetUtil {
         // Retrieve the list of available network interfaces.
         List<NetworkInterface> ifaces = new ArrayList<NetworkInterface>();
         try {
-            for (Enumeration<NetworkInterface> i = NetworkInterface.getNetworkInterfaces(); i.hasMoreElements();) {
-                NetworkInterface iface = i.nextElement();
-                // Use the interface with proper INET addresses only.
-                if (SocketUtils.addressesFromNetworkInterface(iface).hasMoreElements()) {
-                    ifaces.add(iface);
+            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
+            if (interfaces != null) {
+                while (interfaces.hasMoreElements()) {
+                    NetworkInterface iface = interfaces.nextElement();
+                    // Use the interface with proper INET addresses only.
+                    if (SocketUtils.addressesFromNetworkInterface(iface).hasMoreElements()) {
+                        ifaces.add(iface);
+                    }
                 }
             }
         } catch (SocketException e) {
diff --git a/common/src/main/java/io/netty/util/internal/MacAddressUtil.java b/common/src/main/java/io/netty/util/internal/MacAddressUtil.java
index 4ac088750c3..eaf7a40a64c 100644
--- a/common/src/main/java/io/netty/util/internal/MacAddressUtil.java
+++ b/common/src/main/java/io/netty/util/internal/MacAddressUtil.java
@@ -24,6 +24,7 @@
 import java.net.NetworkInterface;
 import java.net.SocketException;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.Enumeration;
 import java.util.LinkedHashMap;
 import java.util.Map;
@@ -52,14 +53,17 @@ public static byte[] bestAvailableMac() {
         // Retrieve the list of available network interfaces.
         Map<NetworkInterface, InetAddress> ifaces = new LinkedHashMap<NetworkInterface, InetAddress>();
         try {
-            for (Enumeration<NetworkInterface> i = NetworkInterface.getNetworkInterfaces(); i.hasMoreElements();) {
-                NetworkInterface iface = i.nextElement();
-                // Use the interface with proper INET addresses only.
-                Enumeration<InetAddress> addrs = SocketUtils.addressesFromNetworkInterface(iface);
-                if (addrs.hasMoreElements()) {
-                    InetAddress a = addrs.nextElement();
-                    if (!a.isLoopbackAddress()) {
-                        ifaces.put(iface, a);
+            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
+            if (interfaces != null) {
+                while (interfaces.hasMoreElements()) {
+                    NetworkInterface iface = interfaces.nextElement();
+                    // Use the interface with proper INET addresses only.
+                    Enumeration<InetAddress> addrs = SocketUtils.addressesFromNetworkInterface(iface);
+                    if (addrs.hasMoreElements()) {
+                        InetAddress a = addrs.nextElement();
+                        if (!a.isLoopbackAddress()) {
+                            ifaces.put(iface, a);
+                        }
                     }
                 }
             }


Test patch: