Skip to content

Avoid NPE from ContextSelector.getContext() when "entry=null" is passed #1538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2023

Conversation

pbodnar
Copy link
Contributor

@pbodnar pbodnar commented Jul 4, 2023

This fixes an apparent bug which was reported already in https://issues.apache.org/jira/browse/LOG4J2-3217.

Note that only ClassLoaderContextSelector provided a custom implementation of this method, where it also checks for null-ness of entry. So all other selector classes like BasicContextSelector would throw NullPointerException when e.g.
Configurator.initialize(String, ClassLoader, URI) is called.

This fixes an apparent bug which was reported already in
https://issues.apache.org/jira/browse/LOG4J2-3217.

Note that only `ClassLoaderContextSelector` provided a custom implementation
of this method, where it also checks for null-ness of `entry`.
So all other selector classes like `BasicContextSelector `
would throw NullPointerException when e.g.
`Configurator.initialize(String, ClassLoader, URI)` is called.
@pbodnar
Copy link
Contributor Author

pbodnar commented Jul 4, 2023

Note: I've across this issue when trying to deactivate asynchronous logging as described in CAS documentation:

To turn off asynchronous logging, include the following in log4j2.component.properites or as a system property:

Log4jContextSelector=org.apache.logging.log4j.core.selector.BasicContextSelector

Even after doing this and fixing the NPE, CAS still logs asynchronously, but that is possibly for another bug, possibly in another repository. 😅


Regarding unit tests and changelog - I'm not sure. Let me know if it is needed.

@vy vy added this to the 2.21.0 milestone Jul 6, 2023
@vy vy self-assigned this Jul 6, 2023
@vy
Copy link
Member

vy commented Jul 6, 2023

@pbodnar, thanks so much! I will take care of the changelog.

Even after doing this and fixing the NPE, CAS still logs asynchronously

That indeed warrants another ticket.

@vy vy merged commit 1b8b8d8 into apache:2.x Jul 6, 2023
vy pushed a commit that referenced this pull request Jul 6, 2023
@@ -108,7 +108,7 @@ default LoggerContext getContext(String fqcn, ClassLoader loader, Map.Entry<Stri
default LoggerContext getContext(String fqcn, ClassLoader loader, Map.Entry<String, Object> entry,
boolean currentContext, URI configLocation) {
final LoggerContext lc = getContext(fqcn, loader, currentContext, configLocation);
if (lc != null) {
if (lc != null && entry != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lc should be not null here, we just need to check if entry is not null.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since getContext() is to be implemented by the subclass, I thought we cannot have such an assumption. Nevertheless, if you think otherwise, be my guest to update it.

@ppkarwasz ppkarwasz modified the milestones: 2.21.0, 2.20.1 Aug 1, 2023
jvz pushed a commit to jvz/logging-log4j2 that referenced this pull request Sep 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants