You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2020/05/13 13:45:11 UTC

[logging-log4j2] branch master updated: [LOG4J2-2844] Null pointer exception when no network interfaces are available.

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/master by this push:
     new 9fe9fda  [LOG4J2-2844] Null pointer exception when no network interfaces are available.
9fe9fda is described below

commit 9fe9fdafe08bc5fe09a40a6f1da86a173ad3055f
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed May 13 09:45:06 2020 -0400

    [LOG4J2-2844] Null pointer exception when no network interfaces are
    available.
---
 .../apache/logging/log4j/core/util/ArrayUtils.java | 11 +++++++
 .../apache/logging/log4j/core/util/NetUtils.java   | 38 ++++++++++++----------
 .../logging/log4j/core/util/NetUtilsTest.java      |  6 ++--
 src/changes/changes.xml                            |  3 ++
 4 files changed, 38 insertions(+), 20 deletions(-)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ArrayUtils.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ArrayUtils.java
index bcf8d54..c71a166 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ArrayUtils.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ArrayUtils.java
@@ -52,6 +52,17 @@ public class ArrayUtils {
     }
 
     /**
+     * Checks if an array of Objects is empty or {@code null}.
+     *
+     * @param array  the array to test
+     * @return {@code true} if the array is empty or {@code null}
+     * @since 2.1
+     */
+    public static boolean isEmpty(final byte[] array) {
+        return getLength(array) == 0;
+    }    
+
+    /**
      * <p>Removes the element at the specified position from the specified array.
      * All subsequent elements are shifted to the left (subtracts one from
      * their indices).</p>
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/NetUtils.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/NetUtils.java
index e848822..195dbff 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/NetUtils.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/NetUtils.java
@@ -52,19 +52,21 @@ public final class NetUtils {
     public static String getLocalHostname() {
         try {
             final InetAddress addr = InetAddress.getLocalHost();
-            return addr.getHostName();
+            return addr == null ? UNKNOWN_LOCALHOST : addr.getHostName();
         } catch (final UnknownHostException uhe) {
             try {
                 final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
-                while (interfaces.hasMoreElements()) {
-                    final NetworkInterface nic = interfaces.nextElement();
-                    final Enumeration<InetAddress> addresses = nic.getInetAddresses();
-                    while (addresses.hasMoreElements()) {
-                        final InetAddress address = addresses.nextElement();
-                        if (!address.isLoopbackAddress()) {
-                            final String hostname = address.getHostName();
-                            if (hostname != null) {
-                                return hostname;
+                if (interfaces != null) {
+                    while (interfaces.hasMoreElements()) {
+                        final NetworkInterface nic = interfaces.nextElement();
+                        final Enumeration<InetAddress> addresses = nic.getInetAddresses();
+                        while (addresses.hasMoreElements()) {
+                            final InetAddress address = addresses.nextElement();
+                            if (!address.isLoopbackAddress()) {
+                                final String hostname = address.getHostName();
+                                if (hostname != null) {
+                                    return hostname;
+                                }
                             }
                         }
                     }
@@ -95,18 +97,20 @@ public final class NetUtils {
                 }
                 if (mac == null) {
                     final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
-                    while (networkInterfaces.hasMoreElements() && mac == null) {
-                        final NetworkInterface nic = networkInterfaces.nextElement();
-                        if (isUpAndNotLoopback(nic)) {
-                            mac = nic.getHardwareAddress();
+                    if (networkInterfaces != null) {
+                        while (networkInterfaces.hasMoreElements() && mac == null) {
+                            final NetworkInterface nic = networkInterfaces.nextElement();
+                            if (isUpAndNotLoopback(nic)) {
+                                mac = nic.getHardwareAddress();
+                            }
                         }
                     }
                 }
             } catch (final SocketException e) {
                 LOGGER.catching(e);
             }
-            if (mac == null || mac.length == 0) {
-                // Emulate a mac address with an IP v4 or v6
+            if (ArrayUtils.isEmpty(mac) && localHost != null) {
+                // Emulate a MAC address with an IP v4 or v6
                 final byte[] address = localHost.getAddress();
                 // Take only 6 bytes if the address is an IPv6 otherwise will pad with two zero bytes
                 mac = Arrays.copyOf(address, 6);
@@ -123,7 +127,7 @@ public final class NetUtils {
      */
     public static String getMacAddressString() {
         final byte[] macAddr = getMacAddress();
-        if (macAddr != null && macAddr.length > 0) {
+        if (!ArrayUtils.isEmpty(macAddr)) {
             StringBuilder sb = new StringBuilder(String.format("%02x", macAddr[0]));
             for (int i = 1; i < macAddr.length; ++i) {
                 sb.append(":").append(String.format("%02x", macAddr[i]));
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/NetUtilsTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/NetUtilsTest.java
index c624798..fc40037 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/NetUtilsTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/NetUtilsTest.java
@@ -32,7 +32,7 @@ public class NetUtilsTest {
     private static final boolean IS_WINDOWS = PropertiesUtil.getProperties().isOsWindows();
 
     @Test
-    public void testToUriWithoutBackslashes() throws URISyntaxException {
+    public void testToUriWithoutBackslashes() {
         final String config = "file:///path/to/something/on/unix";
         final URI uri = NetUtils.toURI(config);
 
@@ -41,7 +41,7 @@ public class NetUtilsTest {
     }
 
     @Test
-    public void testToUriWindowsWithBackslashes() throws URISyntaxException {
+    public void testToUriWindowsWithBackslashes() {
         Assume.assumeTrue(IS_WINDOWS);
         final String config = "file:///D:\\path\\to\\something/on/windows";
         final URI uri = NetUtils.toURI(config);
@@ -51,7 +51,7 @@ public class NetUtilsTest {
     }
 
     @Test
-    public void testToUriWindowsAbsolutePath() throws URISyntaxException {
+    public void testToUriWindowsAbsolutePath() {
         Assume.assumeTrue(IS_WINDOWS);
         final String config = "D:\\path\\to\\something\\on\\windows";
         final URI uri = NetUtils.toURI(config);
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 1465280..c227468 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -167,6 +167,9 @@
       <action issue="LOG4J2-2572" dev="ggregory" type="update">
         Update Apache Flume from 1.8.0 to 1.9.0.
       </action>
+      <action issue="LOG4J2-2844" dev="ggregory" type="fix">
+        Null pointer exception when no network interfaces are available.
+      </action>
     </release>
     <release version="2.13.3" date="2020-05-10" description="GA Release 2.13.3">
       <action issue="LOG4J2-2838" dev="rgoers" type="fix">