You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by "Rogério Lecarião Leite (JIRA)" <ji...@apache.org> on 2016/08/23 14:20:21 UTC

[jira] [Comment Edited] (LOG4J2-1542) java.lang.ArrayIndexOutOfBoundsException

    [ https://issues.apache.org/jira/browse/LOG4J2-1542?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15432870#comment-15432870 ] 

Rogério Lecarião Leite edited comment on LOG4J2-1542 at 8/23/16 2:20 PM:
-------------------------------------------------------------------------

Hi Ralph,
I am newbie on Jira, but the correction is like as Math.max(-1,1) or Math.max(0,1).

{code:title=ParameterizedMessage.java|borderStyle=solid}
private void init(final String messagePattern) {
	this.messagePattern = messagePattern;
	this.indices = new int[messagePattern == null ? 0 : messagePattern.length() >> 1]; // divide by 2
	final int placeholders = ParameterFormatter.countArgumentPlaceholders2(messagePattern, indices);
	initThrowable(argArray, placeholders);
	this.usedCount = Math.min(placeholders, argArray == null ? 0 : argArray.length);
}

private void init(final String messagePattern) {
	int arraySize = messagePattern == null ? 0 : Math.max(messagePattern.length() >> 1, 1);
	this.messagePattern = messagePattern;
	this.indices = new int[arraySize]; 
	final int placeholders = ParameterFormatter.countArgumentPlaceholders2(messagePattern, indices);
	initThrowable(argArray, placeholders);
	this.usedCount = Math.min(placeholders, argArray == null ? 0 : argArray.length);
}
{code}

Some Tests:
- Test with empty string
- Test with one character string


This would be enough? :D


was (Author: rogeriolleite):
Hi Ralph,
I am newbie on Jira, but the correction is like as Math.max(-1,1) or Math.max(0,1).

private void init(final String messagePattern) {
	this.messagePattern = messagePattern;
	this.indices = new int[messagePattern == null ? 0 : messagePattern.length() >> 1]; // divide by 2
	final int placeholders = ParameterFormatter.countArgumentPlaceholders2(messagePattern, indices);
	initThrowable(argArray, placeholders);
	this.usedCount = Math.min(placeholders, argArray == null ? 0 : argArray.length);
}

private void init(final String messagePattern) {
	int arraySize = messagePattern == null ? 0 : Math.max(messagePattern.length() >> 1, 1);
	this.messagePattern = messagePattern;
	this.indices = new int[arraySize]; 
	final int placeholders = ParameterFormatter.countArgumentPlaceholders2(messagePattern, indices);
	initThrowable(argArray, placeholders);
	this.usedCount = Math.min(placeholders, argArray == null ? 0 : argArray.length);
}

Some Tests:
- Test with empty string
- Test with one character string


This would be enough? :D

> java.lang.ArrayIndexOutOfBoundsException
> ----------------------------------------
>
>                 Key: LOG4J2-1542
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1542
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: API
>    Affects Versions: 2.6.2
>         Environment: Array length logic causes throw java.lang.ArrayIndexOutOfBoundsException
>            Reporter: Rogério Lecarião Leite
>            Priority: Critical
>              Labels: easyfix
>   Original Estimate: 1h
>  Remaining Estimate: 1h
>
> This error is reproducible when method fomarTo of the class ParameterizedMessage is invoked.
> This root cause is the method init of same class where is invoked, in the this.indices is initializaded with messagePattern.length() >> 1 run to equals zero because that messagePattern.length() is 1 (string with one character).
> I used File Appender in my XML config and I wrote my code with logger.trace("a").
> My PatternLayout used is pattern="%m" (simplest).



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org