From 1843b318850ac7a3990c13b1d275e0e37491301e Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Wed, 15 Feb 2017 19:43:52 +0100 Subject: [PATCH] Guard against having NetworkInterface.getNetworkInterfaces() return null Motivation: NetworkInterface.getNetworkInterfaces() may return null if no network interfaces are found. We should guard against it. Modifications: Check for null return value. Result: Fixes [#6384] --- .../src/main/java/io/netty/util/NetUtil.java | 13 ++++++++----- .../netty/util/internal/MacAddressUtil.java | 19 +++++++++++-------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/common/src/main/java/io/netty/util/NetUtil.java b/common/src/main/java/io/netty/util/NetUtil.java index 97570fa165..88d4cdd015 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 ifaces = new ArrayList(); try { - for (Enumeration 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 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 4ac088750c..ca4dc75a05 100644 --- a/common/src/main/java/io/netty/util/internal/MacAddressUtil.java +++ b/common/src/main/java/io/netty/util/internal/MacAddressUtil.java @@ -52,14 +52,17 @@ public final class MacAddressUtil { // Retrieve the list of available network interfaces. Map ifaces = new LinkedHashMap(); try { - for (Enumeration i = NetworkInterface.getNetworkInterfaces(); i.hasMoreElements();) { - NetworkInterface iface = i.nextElement(); - // Use the interface with proper INET addresses only. - Enumeration addrs = SocketUtils.addressesFromNetworkInterface(iface); - if (addrs.hasMoreElements()) { - InetAddress a = addrs.nextElement(); - if (!a.isLoopbackAddress()) { - ifaces.put(iface, a); + Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); + if (interfaces != null) { + while (interfaces.hasMoreElements()) { + NetworkInterface iface = interfaces.nextElement(); + // Use the interface with proper INET addresses only. + Enumeration addrs = SocketUtils.addressesFromNetworkInterface(iface); + if (addrs.hasMoreElements()) { + InetAddress a = addrs.nextElement(); + if (!a.isLoopbackAddress()) { + ifaces.put(iface, a); + } } } }