Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-1538

Dynamic removal of filter may cause NPE

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.6.2
    • 2.7
    • Core
    • None
    • Important

    Description

      In my application, I need to add and remove a filter dynamically.
      The problem is, that it occasionally causes NPE because of the subtle bug in
      AppenderControl.isFilteredByAppenderControl:

      @PerformanceSensitive
          private boolean isFilteredByAppenderControl(LogEvent event) {
              return this.getFilter() != null && Result.DENY == this.getFilter().filter(event);
          }
      

      here, this.getFilter() is called twice.
      getFilter() is defined in superclass, AbstractFilterable:

      private volatile Filter filter;

      so, it's volatile, and in my case, when I dynamically remove the filter it becomes null, and in certain cases it happens right between the two calls to getFilter().
      The first call to getFilter() returns non-null, so the secand call is performed, which returns null and it causes NPE.

      The easy fix for this is not to call getFilter() twice:

      @PerformanceSensitive
          private boolean isFilteredByAppenderControl(LogEvent event) {
              Filter filter = this.getFilter(); // do not inline, filter is volatile
              return filter != null && Result.DENY == filter.filter(event);
          }
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            semicontinuity Igor Karpov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - 1m
                1m
                Remaining:
                Remaining Estimate - 1m
                1m
                Logged:
                Time Spent - Not Specified
                Not Specified