Skip to content

Commit 33fff89

Browse files
authored
Fix warning when last argument is null (#4014)
1 parent 8abfdfc commit 33fff89

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

log4j-api-test/src/test/java/org/apache/logging/log4j/message/ParameterFormatterTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,19 @@ void format_should_warn_on_insufficient_args(
9898
placeholderCount, argCount, pattern);
9999
}
100100

101+
@Test
102+
void format_should_not_warn_on_insufficient_args() {
103+
final String expectedMessage = "pan a";
104+
final String pattern = "pan {}";
105+
final String[] args = new String[] {"a", null};
106+
final int argCount = args.length;
107+
108+
String actualMessage = ParameterFormatter.format(pattern, args, argCount);
109+
assertThat(actualMessage).isEqualTo(expectedMessage);
110+
final List<StatusData> statusDataList = statusListener.getStatusData().collect(Collectors.toList());
111+
assertThat(statusDataList).isEmpty();
112+
}
113+
101114
@ParameterizedTest
102115
@MethodSource("messageFormattingTestCases")
103116
void format_should_work(

log4j-api/src/main/java/org/apache/logging/log4j/message/ParameterFormatter.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ static void formatMessage(
247247
// #2380: check if the count of placeholder is not equal to the count of arguments
248248
if (analysis.placeholderCount != argCount) {
249249
final int noThrowableArgCount =
250-
argCount < 1 ? 0 : argCount - ((args[argCount - 1] instanceof Throwable) ? 1 : 0);
250+
argCount < 1 ? 0 : argCount - (isLastArgumentThrowable(args, argCount) ? 1 : 0);
251251
if (analysis.placeholderCount != noThrowableArgCount) {
252252
STATUS_LOGGER.warn(
253253
"found {} argument placeholders, but provided {} for pattern `{}`",
@@ -268,6 +268,12 @@ static void formatMessage(
268268
}
269269
}
270270

271+
private static boolean isLastArgumentThrowable(final Object[] args, final int argCount) {
272+
final Object lastArgument = args[argCount - 1];
273+
// #3975: tolerate null in the last argument since it could have been Throwable parameter
274+
return (lastArgument == null) || (lastArgument instanceof Throwable);
275+
}
276+
271277
private static void formatMessageContainingNoEscapes(
272278
final StringBuilder buffer,
273279
final String pattern,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns="https://logging.apache.org/xml/ns"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="
5+
https://logging.apache.org/xml/ns
6+
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
7+
type="changed">
8+
<issue id="3975" link="https://github.com/apache/logging-log4j2/issues/3975"/>
9+
<issue id="4014" link="https://github.com/apache/logging-log4j2/pull/4014"/>
10+
<description format="asciidoc">
11+
Prevent ParameterFormatter issuing a warning in case there is an extra null argument.
12+
Needed to support cases with Throwable parameter that may be null.
13+
</description>
14+
</entry>

0 commit comments

Comments
 (0)