Skip to content

Commit 2659b9d

Browse files
committed
SONARJAVA-1369 Fix out of bounds when refering previous parameter
1 parent 90a7b9f commit 2659b9d

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

java-checks/src/main/java/org/sonar/java/checks/PrintfCheck.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ private void verifyParameters(MethodInvocationTree mit, List<ExpressionTree> arg
167167
return;
168168
}
169169
param = param.substring(param.indexOf("$") + 1);
170-
} else if (!param.startsWith("<")) {
170+
} else if (param.startsWith("<")) {
171+
//refers to previous argument
172+
argIndex = Math.max(0, argIndex - 1);
173+
}else {
171174
index++;
172175
}
173176
ExpressionTree argExpressionTree = args.get(argIndex);

java-checks/src/test/files/checks/PrintfCheck.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,6 @@ void foo(Calendar c){
7878

7979
String.format("Dude's Birthday: %1$tm %<te,%<tY", c); // Compliant
8080
String.format("Dude's Birthday: %1$tm %1$te,%1$tY", c); // Compliant
81+
String.format("log/protocol_%tY_%<tm_%<td_%<tH_%<tM_%<tS.zip", new java.util.Date());
8182
}
8283
}

0 commit comments

Comments
 (0)