Skip to content

Commit 6864441

Browse files
author
Jim Laskey
committed
8313809: String template fails with java.lang.StringIndexOutOfBoundsException if last fragment is UTF16
Reviewed-by: redestad
1 parent 509f80b commit 6864441

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

src/java.base/share/classes/java/lang/invoke/StringConcatFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,25 +1107,25 @@ public static MethodHandle makeConcatWithTemplate(
11071107
MethodHandle mh = MethodHandles.dropArguments(newString(), 2, ttypes);
11081108

11091109
long initialLengthCoder = INITIAL_CODER;
1110-
String lastFragment = "";
11111110
pos = 0;
11121111
for (String fragment : fragments) {
1113-
lastFragment = fragment;
1112+
initialLengthCoder = JLA.stringConcatMix(initialLengthCoder, fragment);
11141113

11151114
if (ttypes.length <= pos) {
11161115
break;
11171116
}
11181117

11191118
Class<?> ttype = ttypes[pos];
11201119
// (long,byte[],ttype) -> long
1121-
MethodHandle prepender = prepender(lastFragment.isEmpty() ? null : fragment, ttype);
1122-
initialLengthCoder = JLA.stringConcatMix(initialLengthCoder, fragment);
1120+
MethodHandle prepender = prepender(fragment.isEmpty() ? null : fragment, ttype);
11231121
// (byte[],long,ttypes...) -> String (unchanged)
11241122
mh = MethodHandles.filterArgumentsWithCombiner(mh, 1, prepender,1, 0, 2 + pos);
11251123

11261124
pos++;
11271125
}
11281126

1127+
String lastFragment = fragments.getLast();
1128+
initialLengthCoder -= lastFragment.length();
11291129
MethodHandle newArrayCombinator = lastFragment.isEmpty() ? newArray() :
11301130
newArrayWithSuffix(lastFragment);
11311131
// (long,ttypes...) -> String
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8313809
27+
* @summary String template fails with java.lang.StringIndexOutOfBoundsException if last fragment is UTF16
28+
.
29+
* @enablePreview true
30+
*/
31+
32+
import static java.util.FormatProcessor.FMT;
33+
34+
public class T8313809 {
35+
public static void main(final String[] args) throws Exception {
36+
double sum = 12.34;
37+
final String message = FMT."The sum is : %f\{sum} €"; // this fails
38+
if (!message.equals("The sum is : 12.340000 €")) {
39+
throw new RuntimeException("Incorrect result");
40+
}
41+
}
42+
}
43+

0 commit comments

Comments
 (0)