You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ck...@apache.org on 2020/10/06 14:41:57 UTC

[logging-log4j2] branch release-2.x updated (b417ca7 -> 2f798b0)

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

ckozak pushed a change to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git.


    from b417ca7  Add changelog entry for LOG4J2-2889
     new ec34bd2  LOG4J2-2939: Fix NPE in MDCContextMap (#430)
     new 2f798b0  LOG4J2-2939 changelog

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../org/apache/logging/slf4j/MDCContextMap.java    |  6 ++--
 .../java/org/apache/logging/slf4j/LoggerTest.java  | 41 +++++++++++++++++++++-
 src/changes/changes.xml                            |  3 ++
 3 files changed, 47 insertions(+), 3 deletions(-)


[logging-log4j2] 01/02: LOG4J2-2939: Fix NPE in MDCContextMap (#430)

Posted by ck...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ckozak pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit ec34bd2f1ebc34bc80cde64d51ec6060d7bd250e
Author: Constantin Hirsch (SemVox) <63...@users.noreply.github.com>
AuthorDate: Tue Oct 6 16:34:38 2020 +0200

    LOG4J2-2939: Fix NPE in MDCContextMap (#430)
    
    Accomodate for the fact that MDC.getCopyOfContextMap() may return null
---
 .../org/apache/logging/slf4j/MDCContextMap.java    |  6 ++--
 .../java/org/apache/logging/slf4j/LoggerTest.java  | 41 +++++++++++++++++++++-
 2 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/MDCContextMap.java b/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/MDCContextMap.java
index f03ee54..b04d358 100644
--- a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/MDCContextMap.java
+++ b/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/MDCContextMap.java
@@ -71,7 +71,8 @@ public class MDCContextMap implements CleanableThreadContextMap {
 
     @Override
     public boolean containsKey(final String key) {
-        return MDC.getCopyOfContextMap().containsKey(key);
+        Map<String, String> map = MDC.getCopyOfContextMap();
+        return map != null && map.containsKey(key);
     }
 
     @Override
@@ -88,7 +89,8 @@ public class MDCContextMap implements CleanableThreadContextMap {
 
     @Override
     public boolean isEmpty() {
-        return MDC.getCopyOfContextMap().isEmpty();
+        Map<String, String> map = MDC.getCopyOfContextMap();
+        return map == null || map.isEmpty();
     }
 
     @Override
diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerTest.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerTest.java
index 77b9c4e..1f14110 100644
--- a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerTest.java
+++ b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerTest.java
@@ -33,6 +33,7 @@ import org.apache.logging.log4j.spi.MessageFactory2Adapter;
 import org.junit.Before;
 import org.junit.ClassRule;
 import org.junit.Test;
+import org.slf4j.MDC;
 
 import static org.hamcrest.Matchers.*;
 import static org.junit.Assert.*;
@@ -183,5 +184,43 @@ public class LoggerTest {
         assertThat(list.strList, hasSize(2));
         assertTrue("Incorrect year", list.strList.get(0).startsWith("2010"));
     }
-}
 
+    @Test
+    public void mdcNullBackedIsEmpty() {
+        assertNull("Setup wrong", MDC.getCopyOfContextMap());
+        assertTrue(ThreadContext.isEmpty());
+    }
+
+    @Test
+    public void mdcNullBackedContainsKey() {
+        assertNull("Setup wrong", MDC.getCopyOfContextMap());
+        assertFalse(ThreadContext.containsKey("something"));
+    }
+
+    @Test
+    public void mdcNullBackedContainsNullKey() {
+        assertNull("Setup wrong", MDC.getCopyOfContextMap());
+        assertFalse(ThreadContext.containsKey(null));
+    }
+
+    @Test
+    public void mdcContainsNullKey() {
+        try {
+            ThreadContext.put("some", "thing");
+            assertNotNull("Setup wrong", MDC.getCopyOfContextMap());
+            assertFalse(ThreadContext.containsKey(null));
+        } finally {
+            ThreadContext.clearMap();
+        }
+    }
+
+    @Test
+    public void mdcCannotContainNullKey() {
+        try {
+            ThreadContext.put(null, "something");
+            fail("should throw");
+        } catch (IllegalArgumentException | NullPointerException e) {
+            // expected
+        }
+    }
+}


[logging-log4j2] 02/02: LOG4J2-2939 changelog

Posted by ck...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ckozak pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit 2f798b01663213428c5d04f1706e752d1f2ba3eb
Author: Carter Kozak <ck...@apache.org>
AuthorDate: Tue Oct 6 10:37:48 2020 -0400

    LOG4J2-2939 changelog
---
 src/changes/changes.xml | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index fcf6d72..6e35f55 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -147,6 +147,9 @@
       <action issue="LOG4J2-2899" dev="ckozak" type="fix">
         Fix log4j-1.2-api LogEventWrapper threadId and priority accessors when called multiple times.
       </action>
+      <action issue="LOG4J2-2939" dev="ckozak" type="fix" due-to="Constantin Hirsch">
+        Fix NPE in MDCContextMap on 'contains' and 'isEmpty' invocations.
+      </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">