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:
parent
8a3a3245df
commit
1843b31885
@ -163,13 +163,16 @@ 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();
|
||||
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) {
|
||||
logger.warn("Failed to retrieve the list of available network interfaces", e);
|
||||
}
|
||||
|
@ -52,8 +52,10 @@ public final class MacAddressUtil {
|
||||
// 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();
|
||||
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()) {
|
||||
@ -63,6 +65,7 @@ public final class MacAddressUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SocketException e) {
|
||||
logger.warn("Failed to retrieve the list of available network interfaces", e);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user