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]
This commit is contained in:
Norman Maurer 2017-02-15 19:43:52 +01:00
parent 8a3a3245df
commit 1843b31885
2 changed files with 19 additions and 13 deletions

View File

@ -163,11 +163,14 @@ public final class NetUtil {
// Retrieve the list of available network interfaces. // Retrieve the list of available network interfaces.
List<NetworkInterface> ifaces = new ArrayList<NetworkInterface>(); List<NetworkInterface> ifaces = new ArrayList<NetworkInterface>();
try { try {
for (Enumeration<NetworkInterface> i = NetworkInterface.getNetworkInterfaces(); i.hasMoreElements();) { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
NetworkInterface iface = i.nextElement(); if (interfaces != null) {
// Use the interface with proper INET addresses only. while (interfaces.hasMoreElements()) {
if (SocketUtils.addressesFromNetworkInterface(iface).hasMoreElements()) { NetworkInterface iface = interfaces.nextElement();
ifaces.add(iface); // Use the interface with proper INET addresses only.
if (SocketUtils.addressesFromNetworkInterface(iface).hasMoreElements()) {
ifaces.add(iface);
}
} }
} }
} catch (SocketException e) { } catch (SocketException e) {

View File

@ -52,14 +52,17 @@ public final class MacAddressUtil {
// Retrieve the list of available network interfaces. // Retrieve the list of available network interfaces.
Map<NetworkInterface, InetAddress> ifaces = new LinkedHashMap<NetworkInterface, InetAddress>(); Map<NetworkInterface, InetAddress> ifaces = new LinkedHashMap<NetworkInterface, InetAddress>();
try { try {
for (Enumeration<NetworkInterface> i = NetworkInterface.getNetworkInterfaces(); i.hasMoreElements();) { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
NetworkInterface iface = i.nextElement(); if (interfaces != null) {
// Use the interface with proper INET addresses only. while (interfaces.hasMoreElements()) {
Enumeration<InetAddress> addrs = SocketUtils.addressesFromNetworkInterface(iface); NetworkInterface iface = interfaces.nextElement();
if (addrs.hasMoreElements()) { // Use the interface with proper INET addresses only.
InetAddress a = addrs.nextElement(); Enumeration<InetAddress> addrs = SocketUtils.addressesFromNetworkInterface(iface);
if (!a.isLoopbackAddress()) { if (addrs.hasMoreElements()) {
ifaces.put(iface, a); InetAddress a = addrs.nextElement();
if (!a.isLoopbackAddress()) {
ifaces.put(iface, a);
}
} }
} }
} }