Skip to content

fix: null-check parseAppender result in log4j-1.2-api XmlConfiguration#4172

Open
SebTardif wants to merge 2 commits into
apache:2.xfrom
SebTardif:fix/xml-parse-appender-npe
Open

fix: null-check parseAppender result in log4j-1.2-api XmlConfiguration#4172
SebTardif wants to merge 2 commits into
apache:2.xfrom
SebTardif:fix/xml-parse-appender-npe

Conversation

@SebTardif

Copy link
Copy Markdown
Contributor

Summary

In log4j-1.2-api XmlConfiguration, parseAppender() can return null. The code called appender.getName() unconditionally, causing an NPE on bad appender configuration.

Fix

Skip null appenders after parseAppender.

Testing

Logic-only guard; existing suite in CI.

parseAppender can return null when the appender cannot be instantiated.
Dereferencing getName() then caused an NPE during configuration parsing.

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
@ramanathan1504

ramanathan1504 commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

@SebTardif, thanks for the fix. we'll need a unit test reproducing the NPE and changelog also.

Also, could you check if the other parsers in this switch statement (parseLogger, etc.) need the exact same null check? Let's fix them all at once. Lastly, let's make sure we log a warning when appender == null so it doesn't fail silently for the user.

  1. Unit Test --> add a test case with an invalid XML configuration that triggers this scenario to prevent future regressions.
  2. Audit sibling methods: Please check the other tags in this parse() switch block (e.g., parseCategory, parseLogger). If they can also return null on invalid configurations, they need the exact same null-guard.
  3. Logging: Does parseAppender() emit a warning via the StatusLogger when it fails? If not, please add a LOGGER.warn(...) inside your if block so we don't silently skip configuration errors.
  4. Name check: Can appender.getName() ever be null here? If so, we should guard against that as well so map insertion doesn't fail.

- 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
@SebTardif

Copy link
Copy Markdown
Contributor Author

@ramanathan1504

we'll need a unit test reproducing the NPE and changelog also.

Also, could you check if the other parsers in this switch statement (parseLogger, etc.) need the exact same null check? ... log a warning when appender == null ... Can appender.getName() ever be null here?

Thanks for the review. Addressed in the latest commit:

  1. Unit test XmlConfigurationTest#testInvalidAppenderDoesNotNpe with log4j1-invalid-appender.xml (bogus appender class). Without the guard, parse hits NPE on appender.getName() and the valid list appender never registers; with the guard both the NPE is avoided and list loads.
  2. Changelog src/changelog/.2.x.x/fix_xml_parse_appender_npe.xml.
  3. Sibling audit of the same parse() switch:
    • parseCategory / parseRoot are void and do not use a nullable return (no equivalent NPE).
    • Renderer / factory tags already only log warnings.
    • Only APPENDER_TAG called parseAppender(...).getName() without a null check.
  4. LOGGER.warn when parseAppender returns null (includes name + class from the element).
  5. Null name guard before appenderMap.put / addAppender.

Spotless clean; local green:
./mvnw -pl log4j-1.2-api test -Dtest=XmlConfigurationTest#testInvalidAppenderDoesNotNpe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

2 participants