You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rm...@apache.org on 2025/02/02 15:49:21 UTC

(logging-log4cxx) branch master updated: Fix empty appender ref recursion (#472)

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

rmiddleton pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git


The following commit(s) were added to refs/heads/master by this push:
     new 323945bb Fix empty appender ref recursion (#472)
323945bb is described below

commit 323945bb7e2a9613baf8016886c7c53328ffdd4a
Author: Robert Middleton <rm...@users.noreply.github.com>
AuthorDate: Sun Feb 2 10:49:13 2025 -0500

    Fix empty appender ref recursion (#472)
    
    If an appender-ref tag is added with no ref, parsing the XML file gets stuck
    in a recursive loop until the stack overflows.  Check to make sure that the
    ref attribute is in the XML file.
---
 src/main/cpp/domconfigurator.cpp                          |  6 +++++-
 src/test/cpp/xml/domtestcase.cpp                          |  8 ++++++++
 src/test/resources/input/xml/DOMConfiguratorRecursive.xml | 10 ++++++++++
 3 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/src/main/cpp/domconfigurator.cpp b/src/main/cpp/domconfigurator.cpp
index 41141381..12648642 100644
--- a/src/main/cpp/domconfigurator.cpp
+++ b/src/main/cpp/domconfigurator.cpp
@@ -299,7 +299,7 @@ AppenderPtr DOMConfigurator::parseAppender(Pool& p,
 			{
 				LogString refName = subst(getAttribute(utf8Decoder, currentElement, REF_ATTR));
 
-				if (appender->instanceof(AppenderAttachable::getStaticClass()))
+				if (!refName.empty() && appender->instanceof(AppenderAttachable::getStaticClass()))
 				{
 					AppenderAttachablePtr aa = LOG4CXX_NS::cast<AppenderAttachable>(appender);
 					if (LogLog::isDebugEnabled())
@@ -310,6 +310,10 @@ AppenderPtr DOMConfigurator::parseAppender(Pool& p,
 					}
 					aa->addAppender(findAppenderByReference(p, utf8Decoder, currentElement, doc, appenders));
 				}
+				else if (refName.empty())
+				{
+					LogLog::error(LOG4CXX_STR("Can't add appender with empty ref attribute"));
+				}
 				else
 				{
 					LogLog::error(LOG4CXX_STR("Requesting attachment of appender named [") +
diff --git a/src/test/cpp/xml/domtestcase.cpp b/src/test/cpp/xml/domtestcase.cpp
index f5210ff5..5f98b29e 100644
--- a/src/test/cpp/xml/domtestcase.cpp
+++ b/src/test/cpp/xml/domtestcase.cpp
@@ -53,6 +53,7 @@ LOGUNIT_CLASS(DOMTestCase)
 #endif
 	LOGUNIT_TEST(test3);
 	LOGUNIT_TEST(test4);
+	LOGUNIT_TEST(recursiveAppenderRef);
 	LOGUNIT_TEST(invalidAppender);
 	LOGUNIT_TEST(invalidLevel);
 	LOGUNIT_TEST_SUITE_END();
@@ -229,6 +230,13 @@ public:
 		LOGUNIT_ASSERT(exists);
 	}
 
+
+	void recursiveAppenderRef()
+	{
+		// Load a bad XML file, make sure that we don't crash in endless recursion
+		DOMConfigurator::configure(LOG4CXX_TEST_STR("input/xml/DOMConfiguratorRecursive.xml"));
+  }
+
 	void invalidAppender()
 	{
 		// Load an XML file that attempts to use a levelmatchfilter as an appender.
diff --git a/src/test/resources/input/xml/DOMConfiguratorRecursive.xml b/src/test/resources/input/xml/DOMConfiguratorRecursive.xml
new file mode 100644
index 00000000..1ee21f7a
--- /dev/null
+++ b/src/test/resources/input/xml/DOMConfiguratorRecursive.xml
@@ -0,0 +1,10 @@
+<log4j:configuration xmlns:log4j=" ">
+  <appender class="AsyncAppender">
+    <appender-ref/>
+  </appender>
+
+  <logger>
+    <appender-ref/>
+  </logger>
+
+</log4j:configuration>