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 2021/07/28 18:03:10 UTC

[logging-log4j2] branch master updated: [LOG4J2-3131] Attempting to call getExtendedStackTraceAsString() after deserializing JSON LogEvent results in a NPE.

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 bcf8adb  [LOG4J2-3131] Attempting to call getExtendedStackTraceAsString() after deserializing JSON LogEvent results in a NPE.
bcf8adb is described below

commit bcf8adb88cc6c35e387421f2873522db32f81c34
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Jul 28 08:36:13 2021 -0400

    [LOG4J2-3131] Attempting to call getExtendedStackTraceAsString() after
    deserializing JSON LogEvent results in a NPE.
---
 .../log4j/core/impl/ExtendedStackTraceElement.java |  2 ++
 .../logging/log4j/core/impl/ThrowableProxy.java    |  8 +++--
 .../log4j/core/impl/ThrowableProxyHelper.java      |  4 +--
 .../core/impl/ThrowableProxyRendererTest.java      | 34 ++++++++++++++++++++++
 src/changes/changes.xml                            |  3 ++
 5 files changed, 45 insertions(+), 6 deletions(-)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ExtendedStackTraceElement.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ExtendedStackTraceElement.java
index f207ccd..377d0e6 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ExtendedStackTraceElement.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ExtendedStackTraceElement.java
@@ -35,6 +35,8 @@ import org.apache.logging.log4j.util.Strings;
  */
 public final class ExtendedStackTraceElement implements Serializable {
 
+    static final ExtendedStackTraceElement[] EMPTY_ARRAY = {};
+
     private static final long serialVersionUID = -2171069569241280505L;
 
     private final ExtendedClassInfo extraClassInfo;
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java
index 1c043af..2a5de7d 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java
@@ -48,6 +48,8 @@ import org.apache.logging.log4j.util.Strings;
  */
 public class ThrowableProxy implements Serializable {
 
+    static final ThrowableProxy[] EMPTY_ARRAY = {};
+
     private static final char EOL = '\n';
 
     private static final String EOL_STR = String.valueOf(EOL);
@@ -74,14 +76,14 @@ public class ThrowableProxy implements Serializable {
      * For JSON and XML IO via Jackson.
      */
     @SuppressWarnings("unused")
-    private ThrowableProxy() {
+    ThrowableProxy() {
         this.throwable = null;
         this.name = null;
-        this.extendedStackTrace = null;
+        this.extendedStackTrace = ExtendedStackTraceElement.EMPTY_ARRAY;
         this.causeProxy = null;
         this.message = null;
         this.localizedMessage = null;
-        this.suppressedProxies = ThrowableProxyHelper.EMPTY_THROWABLE_PROXY_ARRAY;
+        this.suppressedProxies = ThrowableProxy.EMPTY_ARRAY;
     }
 
     /**
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxyHelper.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxyHelper.java
index edeaf9f..111111a 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxyHelper.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxyHelper.java
@@ -35,8 +35,6 @@ import java.util.Stack;
  */
 class ThrowableProxyHelper {
 
-    static final ThrowableProxy[] EMPTY_THROWABLE_PROXY_ARRAY = new ThrowableProxy[0];
-
     private ThrowableProxyHelper() {
         // Utility Class
     }
@@ -128,7 +126,7 @@ class ThrowableProxyHelper {
         try {
             final Throwable[] suppressed = thrown.getSuppressed();
             if (suppressed == null || suppressed.length == 0) {
-                return EMPTY_THROWABLE_PROXY_ARRAY;
+                return ThrowableProxy.EMPTY_ARRAY;
             }
             final List<ThrowableProxy> proxies = new ArrayList<>(suppressed.length);
             if (suppressedVisited == null) {
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyRendererTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyRendererTest.java
new file mode 100644
index 0000000..62725da
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyRendererTest.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.impl;
+
+import java.util.ArrayList;
+
+import org.apache.logging.log4j.core.pattern.PlainTextRenderer;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests ThrowableProxyRenderer.
+ */
+public class ThrowableProxyRendererTest {
+
+    @Test
+    public void test_formatExtendedStackTraceTo() {
+        ThrowableProxyRenderer.formatExtendedStackTraceTo(new ThrowableProxy(), new StringBuilder(), new ArrayList<>(),
+            new PlainTextRenderer(), "", System.lineSeparator());
+    }
+}
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ff1b534..05bc509 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -356,6 +356,9 @@
       <action issue="LOG4J2-3014" dev="ggregory" type="fix" due-to="Lee Breisacher, Gary Gregory">
         Log4j1ConfigurationConverter on Windows produces "&#xd;" at end of every line.
       </action>
+      <action issue="LOG4J2-3131" dev="ggregory" type="fix" due-to="Adam Long, Gary Gregory">
+        Attempting to call getExtendedStackTraceAsString() after deserializing JSON LogEvent results in a NPE.
+      </action>
       <!-- ADDS -->
       <action issue="LOG4J2-2962" dev="vy" type="add">
         Enrich "map" resolver by unifying its backend with "mdc" resolver.