Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,21 @@ private void parse(Element element) {
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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -99,6 +101,17 @@ void testXML() throws Exception {
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 {
Expand Down
35 changes: 35 additions & 0 deletions log4j-1.2-api/src/test/resources/log4j1-invalid-appender.xml
Original file line number Diff line number Diff line change
@@ -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>
8 changes: 8 additions & 0 deletions src/changelog/.2.x.x/fix_xml_parse_appender_npe.xml
Original file line number Diff line number Diff line change
@@ -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"/>
Comment thread
SebTardif marked this conversation as resolved.
Resolved
<description format="asciidoc">Fix NPE in log4j-1.2-api `XmlConfiguration` when `parseAppender` returns null for an invalid appender element</description>
</entry>
Loading