Skip to content

Specific message pattern causes ArrayIndexOutOfBoundsException throwed by ParameterizedMessage.init() #1626

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

Closed
hucsmn opened this issue Jul 27, 2023 · 1 comment · Fixed by #1639
Assignees

Comments

@hucsmn
Copy link

hucsmn commented Jul 27, 2023

Description

Message pattern "\\\\{}" causes ArrayIndexOutOfBoundsException during ParameterizedMessage.init()

Configuration

Default spring boot configuration.

Version: [Log4j version]

Known reproducable versions: 2.12.0 - 2.20.0

Operating system: [OS and version]

Windows 10

JDK: [JDK distribution and version]

Oracle JDK 1.8u362

Logs

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
	at org.apache.logging.log4j.message.ParameterFormatter.countArgumentPlaceholders2(ParameterFormatter.java:121)
	at org.apache.logging.log4j.message.ParameterizedMessage.init(ParameterizedMessage.java:149)
	at org.apache.logging.log4j.message.ParameterizedMessage.<init>(ParameterizedMessage.java:123)
	at org.apache.logging.log4j.message.ParameterizedMessage.<init>(ParameterizedMessage.java:132)
	at org.apache.logging.log4j.message.ParameterizedMessageFactory.newMessage(ParameterizedMessageFactory.java:70)
	at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:2033)
	at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1899)
	at org.apache.logging.slf4j.Log4jLogger.error(Log4jLogger.java:304)
        ......

Reproduction

package org.apache.logging.log4j.message;

public class Test {
    private static final String testMsg1 = "\\\\{}";
    private static final String testMsg2 = "\\\\{} blabla";
    private static final String[] testArgs = {"a"};

    public static void main(String[] args) {
        System.out.println(formatUsingReusableParameterizedMessage(testMsg2));  // \a blabla
        System.out.println(formatUsingParameterizedMessage(testMsg2));          // \a blabla
        System.out.println(formatUsingReusableParameterizedMessage(testMsg1));  // \a
        System.out.println(formatUsingParameterizedMessage(testMsg1));          // throws ArrayIndexOutOfBoundsException
    }

    public static String formatUsingParameterizedMessage(final String msg) {
        return new ParameterizedMessage(msg, testArgs).getFormattedMessage();
    }

    public static String formatUsingReusableParameterizedMessage(final String msg) {
        return new ReusableParameterizedMessage().set(msg, testArgs).getFormattedMessage();
    }
}
@vy vy self-assigned this Jul 28, 2023
@vy
Copy link
Member

vy commented Jul 28, 2023

@hucsmn, thanks for the report! This is indeed a bug. I will work on it.

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

Successfully merging a pull request may close this issue.

2 participants