Skip to content

Commit

Permalink
Fix NPE in PatternProcessor for the UNIX pattern
Browse files Browse the repository at this point in the history
Closes 2346.

The constructor of `PatternProcessor` fails if the date pattern is
`UNIX` and `UNIX_MILLIS`: `DatePatterConverter#getPattern()` returns
intentionally `null` in those cases.
  • Loading branch information
ppkarwasz committed Mar 6, 2024
1 parent 0eb232f commit 054a4f7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.logging.log4j.core.appender.rolling;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.time.Instant;
Expand All @@ -29,6 +30,8 @@
import org.junit.jupiter.api.parallel.ResourceAccessMode;
import org.junit.jupiter.api.parallel.ResourceLock;
import org.junit.jupiter.api.parallel.Resources;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

/**
* Tests the PatternProcessor class.
Expand Down Expand Up @@ -394,4 +397,10 @@ public void testGetNextTimeDailyReturnsFirstHourOfNextDayInGmtIfZoneIsInvalid()
TimeZone.setDefault(old);
}
}

@ParameterizedTest
@ValueSource(strings = {"%d{UNIX}", "%d{UNIX_MILLIS}"})
void does_not_throw_with_unix_pattern(final String pattern) {
assertDoesNotThrow(() -> new PatternProcessor(pattern));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ protected final void formatFileName(final StringBuilder buf, final Object... obj
}

private RolloverFrequency calculateFrequency(final String pattern) {
// The UNIX and UNIX_MILLIS converters do not have a pattern
if (pattern == null) {
return null;
}
if (patternContains(pattern, MILLIS_CHAR)) {
return RolloverFrequency.EVERY_MILLISECOND;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public void format(final StringBuilder toAppendTo, final Object... objects) {
/**
* Gets the pattern string describing this date format.
*
* @return the pattern string describing this date format.
* @return the pattern string describing this date format or {@code null} if the format does not have a pattern.
*/
public String getPattern() {
return formatter.toPattern();
Expand Down
8 changes: 8 additions & 0 deletions src/changelog/.2.x.x/2346_unix_millis_data_pattern.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://logging.apache.org/log4j/changelog"
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.3.xsd"
type="changed">
<issue id="2346" link="https://github.com/apache/logging-log4j2/pull/2346"/>
<description format="asciidoc">Fix `NullPointerException` in `PatternProcessor` for a `UNIX_MILLIS` pattern.</description>
</entry>

0 comments on commit 054a4f7

Please sign in to comment.