Skip to content
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

DefaultFilterComponentBuilder creates builder attributes for onMatch/onMismatch without performing null checks. #3464

Closed
JWT007 opened this issue Feb 12, 2025 · 1 comment

Comments

@JWT007
Copy link
Contributor

JWT007 commented Feb 12, 2025

Log4j 2.24.3

When using a configuration builder and adding a FilterComponentBuilder, generating XML from the ConfigurationBuilder causes exception when the filter is defined with a null OnMatch/OnMismatch Result.

Configuring the onMatch/onMismatch attributes is not required - null should be a valid value.

In this case, no attributtes should be added.

However in tthe DefaulttFilterComponentBuilder, attributes are added to tthe builder without performing a null-check first.

class DefaultFilterComponentBuilder extends DefaultComponentAndConfigurationBuilder<FilterComponentBuilder>
        implements FilterComponentBuilder {

    public DefaultFilterComponentBuilder(
            final DefaultConfigurationBuilder<? extends Configuration> builder,
            final String type,
            final String onMatch,
            final String onMismatch) {
        super(builder, type);
        addAttribute(AbstractFilterBuilder.ATTR_ON_MATCH, onMatch);
        addAttribute(AbstractFilterBuilder.ATTR_ON_MISMATCH, onMismatch);
    }
}

This results attributes with null values in the builder tree which can cause XML serialization problems.

I think this might be better:

class DefaultFilterComponentBuilder extends DefaultComponentAndConfigurationBuilder<FilterComponentBuilder>
        implements FilterComponentBuilder {

    public DefaultFilterComponentBuilder(
            final DefaultConfigurationBuilder<? extends Configuration> builder,
            final String type,
            final String onMatch,
            final String onMismatch) {
        super(builder, type);
        Optional.ofNullable(onMatch).ifPresent(() -> addAttribute(AbstractFilterBuilder.ATTR_ON_MATCH, onMatch));
        Optional.ofNullable(onMismatch).ifPresent(() -> addAttribute(AbstractFilterBuilder.ATTR_ON_MISMATCH, onMismatch));
    }
}
@ppkarwasz
Copy link
Contributor

I am closing this as duplicate of #2791.

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

No branches or pull requests

2 participants