Skip to content

StringMatchFilter would return NPE if configured programmatically with 'null' text. #3153

Closed
@JWT007

Description

@JWT007

StringMatchFilter (Log4j 2.24.1)

When parsing from XML I think if the "text" attribute is missing it will be populated with an empty string due to the Builder field:

@PluginBuilderAttribute
private String text = "";

However, if for whatever reason, someone was programmatically creating this StringMatchFilter and passed null to "Builder#setMatchString", no validation is performed in the "build()" method or the constructor.

This would lead to a deferred NPE in the StringMatchFilter#filter method:

private Result filter(final String msg) {
   return msg.contains(this.text) ? onMatch : onMismatch;
}

Since String#contains(s) assumes s is NotNull.

public boolean contains(CharSequence s) {
   return indexOf(s.toString()) >= 0;  // <== NPE!
}

I thiink standard behaviour would be to log an error and return null in the 'build()' method if the 'text' field is null.

Also there seems to be a copy/paste error in the StringMatchFilter.Builder#setMarkerText javadoc:

/**
 * Sets the logging level to use.
 * @param text the logging level to use
 * @return this
 */
public StringMatchFilter.Builder setMatchString(final String text) {
    this.text = text;
    return this;
}

Metadata

Metadata

Assignees

Labels

bugIncorrect, unexpected, or unintended behavior of existing codeconfigurationAffects the configuration system in a general waygood first issueIssues that are good for contributors looking to get startedjavaPull requests that update Java codewaiting-for-maintainer

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions