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

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


The following commit(s) were added to refs/heads/2.x by this push:
     new 819f2167ca fix: null-check parseAppender result in log4j-1.2-api 
XmlConfiguration (#4172)
819f2167ca is described below

commit 819f2167cadfc47645926fe7a0ce52b5b33290f9
Author: Sebastien Tardif <[email protected]>
AuthorDate: Mon Jul 20 12:16:03 2026 -0400

    fix: null-check parseAppender result in log4j-1.2-api XmlConfiguration 
(#4172)
    
    * fix: null-check parseAppender result in log4j-1.2-api XmlConfiguration
    
    parseAppender can return null when the appender cannot be instantiated.
    Dereferencing getName() then caused an NPE during configuration parsing.
    
    Signed-off-by: Sebastien Tardif <[email protected]>
    
    * Address review: warn, null name guard, test, changelog
    
    - Log a warning when parseAppender returns null or getName() is null
    - Unit test with invalid appender class that NPE'd without the guard
    - Changelog entry for PR 4172
    - Sibling parse() cases (parseCategory/parseRoot) are void and already
      null-safe; only APPENDER_TAG used a nullable return
    
    * Remove explanatory comments from appender null-guard and test
    
    Address review feedback: drop production and test comments around the
    parseAppender null handling.
    
    Signed-off-by: Sebastien Tardif <[email protected]>
    
    ---------
    
    Signed-off-by: Sebastien Tardif <[email protected]>
    Co-authored-by: Ramanathan <[email protected]>
---
 .../org/apache/log4j/xml/XmlConfiguration.java     | 16 +++++++++-
 .../apache/log4j/config/XmlConfigurationTest.java  | 13 ++++++++
 .../src/test/resources/log4j1-invalid-appender.xml | 35 ++++++++++++++++++++++
 .../.2.x.x/fix_xml_parse_appender_npe.xml          |  8 +++++
 4 files changed, 71 insertions(+), 1 deletion(-)

diff --git 
a/log4j-1.2-api/src/main/java/org/apache/log4j/xml/XmlConfiguration.java 
b/log4j-1.2-api/src/main/java/org/apache/log4j/xml/XmlConfiguration.java
index a4e7916139..e11e8c4767 100644
--- a/log4j-1.2-api/src/main/java/org/apache/log4j/xml/XmlConfiguration.java
+++ b/log4j-1.2-api/src/main/java/org/apache/log4j/xml/XmlConfiguration.java
@@ -774,7 +774,21 @@ public class XmlConfiguration extends Log4j1Configuration {
                     break;
                 case APPENDER_TAG:
                     final Appender appender = parseAppender(currentElement);
-                    appenderMap.put(appender.getName(), appender);
+                    if (appender == null) {
+                        LOGGER.warn(
+                                "Could not create appender named [{}] of class 
[{}]; ignoring.",
+                                subst(currentElement.getAttribute(NAME_ATTR)),
+                                
subst(currentElement.getAttribute(CLASS_ATTR)));
+                        break;
+                    }
+                    final String appenderName = appender.getName();
+                    if (appenderName == null) {
+                        LOGGER.warn(
+                                "Appender of class [{}] has a null name; 
ignoring.",
+                                
subst(currentElement.getAttribute(CLASS_ATTR)));
+                        break;
+                    }
+                    appenderMap.put(appenderName, appender);
                     addAppender(AppenderAdapter.adapt(appender));
                     break;
                 default:
diff --git 
a/log4j-1.2-api/src/test/java/org/apache/log4j/config/XmlConfigurationTest.java 
b/log4j-1.2-api/src/test/java/org/apache/log4j/config/XmlConfigurationTest.java
index 95a260ed86..8cf5862059 100644
--- 
a/log4j-1.2-api/src/test/java/org/apache/log4j/config/XmlConfigurationTest.java
+++ 
b/log4j-1.2-api/src/test/java/org/apache/log4j/config/XmlConfigurationTest.java
@@ -16,9 +16,11 @@
  */
 package org.apache.log4j.config;
 
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.File;
@@ -99,6 +101,17 @@ class XmlConfigurationTest extends 
AbstractLog4j1ConfigurationTest {
         assertTrue(file.length() > 0, "File A2 is empty");
     }
 
+    @Test
+    void testInvalidAppenderDoesNotNpe() throws Exception {
+        final LoggerContext loggerContext =
+                assertDoesNotThrow(() -> 
TestConfigurator.configure("target/test-classes/log4j1-invalid-appender.xml"));
+        final Configuration configuration = loggerContext.getConfiguration();
+        assertNotNull(configuration, "Configuration should still be created");
+        assertNull(configuration.getAppender("bad"), "Invalid appender should 
not be registered");
+        final Appender list = configuration.getAppender("list");
+        assertNotNull(list, "Valid list appender should be registered");
+    }
+
     @Override
     @Test
     public void testConsoleEnhancedPatternLayout() throws Exception {
diff --git a/log4j-1.2-api/src/test/resources/log4j1-invalid-appender.xml 
b/log4j-1.2-api/src/test/resources/log4j1-invalid-appender.xml
new file mode 100644
index 0000000000..7a77470637
--- /dev/null
+++ b/log4j-1.2-api/src/test/resources/log4j1-invalid-appender.xml
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
+
+<!-- Invalid appender class must not NPE during parse(); valid ListAppender 
still loads. -->
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/";>
+  <appender name="bad" class="org.apache.log4j.DoesNotExistAppender"/>
+
+  <appender name="list" class="org.apache.log4j.ListAppender">
+    <layout class="org.apache.log4j.PatternLayout">
+      <param name="ConversionPattern" value="%m%n" />
+    </layout>
+  </appender>
+
+  <root>
+    <priority value="debug" />
+    <appender-ref ref="list" />
+  </root>
+
+</log4j:configuration>
diff --git a/src/changelog/.2.x.x/fix_xml_parse_appender_npe.xml 
b/src/changelog/.2.x.x/fix_xml_parse_appender_npe.xml
new file mode 100644
index 0000000000..d623e40642
--- /dev/null
+++ b/src/changelog/.2.x.x/fix_xml_parse_appender_npe.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xmlns="https://logging.apache.org/xml/ns";
+       xsi:schemaLocation="https://logging.apache.org/xml/ns 
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd";
+       type="fixed">
+  <issue id="4172" link="https://github.com/apache/logging-log4j2/pull/4172"/>
+  <description format="asciidoc">Fix NPE in log4j-1.2-api `XmlConfiguration` 
when `parseAppender` returns null for an invalid appender element</description>
+</entry>

Reply via email to