This is an automated email from the ASF dual-hosted git repository.
vy pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/release-2.x by this push:
new df45a46110 LOG4J2-3614 Harden InstantFormatter against delegate
failures.
df45a46110 is described below
commit df45a4611099860254c68540480fc40ea0a89ac4
Author: Volkan Yazıcı <[email protected]>
AuthorDate: Mon Oct 10 11:13:12 2022 +0200
LOG4J2-3614 Harden InstantFormatter against delegate failures.
---
.../template/json/util/InstantFormatterTest.java | 44 ++++++++++++++++++++++
.../template/json/util/InstantFormatter.java | 18 +++++++--
src/changes/changes.xml | 7 +++-
3 files changed, 63 insertions(+), 6 deletions(-)
diff --git
a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/util/InstantFormatterTest.java
b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/util/InstantFormatterTest.java
index bc1fd42269..65bbc3b081 100644
---
a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/util/InstantFormatterTest.java
+++
b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/util/InstantFormatterTest.java
@@ -17,11 +17,14 @@
package org.apache.logging.log4j.layout.template.json.util;
import org.apache.logging.log4j.core.time.MutableInstant;
+import org.apache.logging.log4j.core.util.datetime.FastDateFormat;
+import org.apache.logging.log4j.core.util.datetime.FixedDateFormat;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
+import java.util.Locale;
import java.util.TimeZone;
class InstantFormatterTest {
@@ -60,4 +63,45 @@ class InstantFormatterTest {
.isEqualTo("1970-01-01T00:00:00.123456789Z");
}
+ /**
+ * @see <a
href="https://issues.apache.org/jira/browse/LOG4J2-3614">LOG4J2-3614</a>
+ */
+ @Test
+ void FastDateFormat_failures_should_be_handled() {
+
+ // Define a pattern causing `FastDateFormat` to fail.
+ final String pattern = "ss.nnnnnnnnn";
+ final TimeZone timeZone = TimeZone.getTimeZone("UTC");
+ final Locale locale = Locale.US;
+
+ // Assert that the pattern is not supported by `FixedDateFormat`.
+ final FixedDateFormat fixedDateFormat =
FixedDateFormat.createIfSupported(pattern, timeZone.getID());
+ Assertions.assertThat(fixedDateFormat).isNull();
+
+ // Assert that the pattern indeed causes a `FastDateFormat` failure.
+ Assertions
+ .assertThatThrownBy(() -> FastDateFormat.getInstance(pattern,
timeZone, locale))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Illegal pattern component: nnnnnnnnn");
+
+ // Assert that `InstantFormatter` falls back to `DateTimeFormatter`.
+ final InstantFormatter formatter = InstantFormatter
+ .newBuilder()
+ .setPattern(pattern)
+ .setTimeZone(timeZone)
+ .build();
+ Assertions
+ .assertThat(formatter.getInternalImplementationClass())
+ .asString()
+ .endsWith(".DateTimeFormatter");
+
+ // Assert that formatting works.
+ MutableInstant instant = new MutableInstant();
+ instant.initFromEpochSecond(0, 123_456_789);
+ Assertions
+ .assertThat(formatter.format(instant))
+ .isEqualTo("00.123456789");
+
+ }
+
}
diff --git
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/util/InstantFormatter.java
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/util/InstantFormatter.java
index 2dfbafe7cc..c8de2cd137 100644
---
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/util/InstantFormatter.java
+++
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/util/InstantFormatter.java
@@ -20,6 +20,7 @@ import org.apache.logging.log4j.core.time.Instant;
import org.apache.logging.log4j.core.time.MutableInstant;
import org.apache.logging.log4j.core.util.datetime.FastDateFormat;
import org.apache.logging.log4j.core.util.datetime.FixedDateFormat;
+import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.util.Strings;
import java.time.format.DateTimeFormatter;
@@ -40,6 +41,8 @@ import java.util.TimeZone;
*/
public final class InstantFormatter {
+ private static final StatusLogger LOGGER = StatusLogger.getLogger();
+
/**
* The list of formatter factories in decreasing efficiency order.
*/
@@ -54,10 +57,17 @@ public final class InstantFormatter {
private InstantFormatter(final Builder builder) {
this.formatter = Arrays
.stream(FORMATTER_FACTORIES)
- .map(formatterFactory -> formatterFactory.createIfSupported(
- builder.getPattern(),
- builder.getLocale(),
- builder.getTimeZone()))
+ .map(formatterFactory -> {
+ try {
+ return formatterFactory.createIfSupported(
+ builder.getPattern(),
+ builder.getLocale(),
+ builder.getTimeZone());
+ } catch (final Exception error) {
+ LOGGER.warn("skipping the failed formatter factory
\"{}\"", formatterFactory, error);
+ return null;
+ }
+ })
.filter(Objects::nonNull)
.findFirst()
.orElseThrow(() -> new AssertionError("could not find a
matching formatter"));
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 377fbc8862..49c2090af1 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -30,6 +30,9 @@
- "remove" - Removed
-->
<release version="2.19.0" date="2022-09-09" description="GA Release
2.19.0">
+ <action issue="LOG4J2-3614" dev="vy" type="fix" due-to="strainu">
+ Harden InstantFormatter against delegate failures.
+ </action>
<action issue="LOG4J2-3572" dev="rgeors" type="update">
Add getExplicitLevel method to LoggerConfig.
</action>
@@ -42,10 +45,10 @@
<action issue="LOG4J2-3578" dev="rgoers" type="fix">
Generate new SSL certs for testing.
</action>
- <action issue="LOG4J2-3556" dev="vy" type="fix" due-to=" Arthur
Gavlyukovskiy">
+ <action issue="LOG4J2-3556" dev="vy" type="fix" due-to="Arthur
Gavlyukovskiy">
Make JsonTemplateLayout stack trace truncation operate for each label
block.
</action>
- <action issue="LOG4J2-3573" dev="vy" type="remove" due-to=" Wolff Bock
von Wuelfingen">
+ <action issue="LOG4J2-3573" dev="vy" type="remove" due-to="Wolff Bock
von Wuelfingen">
Removed build page in favor of a single build instructions file.
</action>
<action issue="LOG4J2-3550" dev="rgoers" type="fix"
due-to="DongjianPeng">