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/12/19 15:26:12 UTC
[logging-log4j2] 02/02: Fix NPE when input is null in StrSubstitutor.replace(String, Properties).
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 632569d725462b261d2aa2aa9ba83684693199a3
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Dec 19 10:26:04 2021 -0500
Fix NPE when input is null in StrSubstitutor.replace(String,
Properties).
---
.../apache/logging/log4j/core/lookup/StrSubstitutor.java | 2 +-
.../logging/log4j/core/lookup/StrSubstitutorTest.java | 16 ++++++++++++++--
src/changes/changes.xml | 5 ++++-
3 files changed, 19 insertions(+), 4 deletions(-)
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java
index 4818ab0..df344dc 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java
@@ -437,7 +437,7 @@ public class StrSubstitutor implements ConfigurationAware {
*/
public static String replace(final Object source, final Properties valueProperties) {
if (valueProperties == null) {
- return source.toString();
+ return Objects.toString(source, null);
}
final Map<String, String> valueMap = new HashMap<>();
final Enumeration<?> propNames = valueProperties.propertyNames();
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java
index 93ef200..40568c6 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java
@@ -16,8 +16,12 @@
*/
package org.apache.logging.log4j.core.lookup;
+import static org.junit.Assert.assertNull;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
import java.util.HashMap;
import java.util.Map;
+import java.util.Properties;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.core.LogEvent;
@@ -25,8 +29,6 @@ import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.*;
-
public class StrSubstitutorTest {
private static final String TESTKEY = "TestKey";
@@ -207,4 +209,14 @@ public class StrSubstitutorTest {
subst.setRecursiveEvaluationAllowed(true);
assertEquals("${ctx:first}", subst.replace("${ctx:first}"));
}
+
+ @Test
+ public void testReplaceProperties() {
+ Properties properties = new Properties();
+ properties.put("a", "A");
+ assertNull(StrSubstitutor.replace((String) null, properties));
+ assertNull(StrSubstitutor.replace((String) null, (Properties) null));
+ assertEquals("A", StrSubstitutor.replace("${a}", properties));
+ assertEquals("${a}", StrSubstitutor.replace("${a}", (Properties) null));
+ }
}
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index cbb6904..895bd75 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -30,7 +30,10 @@
- "remove" - Removed
-->
<release version="2.17.1" date="2021-MM-dd" description="GA Release 2.17.1">
- </release>
+ <action issue="LOG4J2-3230" dev="ggregory" type="fix">
+ Fix NPE when input is null in StrSubstitutor.replace(String, Properties).
+ </action>
+ </release>
<release version="2.17.0" date="2021-12-17" description="GA Release 2.17.0">
<action issue="LOG4J2-3230" dev="ckozak" type="fix">
Fix string substitution recursion.