Skip to content

Commit

Permalink
Fix SEGV with invalid appenders (#471)
Browse files Browse the repository at this point in the history
If a non-appender class is attempted to be used as an appender, a SEGV can
occur.  Add explicit check to make sure that the class being used is in fact
an appender.
  • Loading branch information
rm5248 authored Feb 2, 2025
1 parent 2cbae95 commit 1d53672
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/cpp/domconfigurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ AppenderPtr DOMConfigurator::parseAppender(Pool& p,
{
ObjectPtr instance = ObjectPtr(Loader::loadClass(className).newInstance());
AppenderPtr appender = LOG4CXX_NS::cast<Appender>(instance);
if(!appender){
LogLog::error(LOG4CXX_STR("Could not cast class of type [") + className + LOG4CXX_STR("] to appender"));
return AppenderPtr();
}
PropertySetter propSetter(appender);

appender->setName(subst(getAttribute(utf8Decoder, appenderElement, NAME_ATTR)));
Expand Down
8 changes: 8 additions & 0 deletions src/test/cpp/xml/domtestcase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ LOGUNIT_CLASS(DOMTestCase)
#endif
LOGUNIT_TEST(test3);
LOGUNIT_TEST(test4);
LOGUNIT_TEST(invalidAppender);
LOGUNIT_TEST(invalidLevel);
LOGUNIT_TEST_SUITE_END();

Expand Down Expand Up @@ -228,6 +229,13 @@ LOGUNIT_CLASS(DOMTestCase)
LOGUNIT_ASSERT(exists);
}

void invalidAppender()
{
// Load an XML file that attempts to use a levelmatchfilter as an appender.
// We should not crash when loading this file.
DOMConfigurator::configure(LOG4CXX_TEST_STR("input/xml/DOMInvalidAppender.xml"));
}

void invalidLevel()
{
// Load an XML file that attempts to use a filter as a level.
Expand Down
7 changes: 7 additions & 0 deletions src/test/resources/input/xml/DOMInvalidAppender.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<log4j:configuration xmlns:log4j=' '>
<appender name="TEMP" class="levelmatchfilter"></appender>

<root>
<appender-ref ref="TEMP"/>
</root>
</log4j:configuration>

0 comments on commit 1d53672

Please sign in to comment.