Skip to content

Commit 148cea0

Browse files
eamonnmcmanusGoogle Java Core Libraries
authored andcommitted
In Strings.lenientFormat, don't assign into the varargs array.
When the method is called with an actual array argument, rather than as varargs, assigning into that array is at best surprising. It may also cause an `ArrayStoreException` if the component type of the array is not compatible with `String`. RELNOTES=`Strings.lenientFormat` no longer assigns into its varargs array argument. PiperOrigin-RevId: 800096793
1 parent 47b7e9e commit 148cea0

File tree

4 files changed

+18
-22
lines changed

4 files changed

+18
-22
lines changed

android/guava-tests/test/com/google/common/base/StringsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ public void testLenientFormat() {
213213
assertEquals("5 + 6 = 11", Strings.lenientFormat("5 + %s = 11", 6));
214214
assertEquals("5 + 6 = 11", Strings.lenientFormat("5 + 6 = %s", 11));
215215
assertEquals("5 + 6 = 11", Strings.lenientFormat("%s + %s = %s", 5, 6, 11));
216+
assertEquals(
217+
"5 + 6 = 11", Strings.lenientFormat("%s + %s = %s", (Object[]) new Integer[] {5, 6, 11}));
216218
assertEquals("null [null, null]", Strings.lenientFormat("%s", null, null, null));
217219
assertEquals("null [5, 6]", Strings.lenientFormat(null, 5, 6));
218220
assertEquals("null", Strings.lenientFormat("%s", (Object) null));

android/guava/src/com/google/common/base/Strings.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,6 @@ public static String lenientFormat(
263263

264264
if (args == null) {
265265
args = new Object[] {"(Object[])null"};
266-
} else {
267-
for (int i = 0; i < args.length; i++) {
268-
args[i] = lenientToString(args[i]);
269-
}
270266
}
271267

272268
// start substituting the arguments into the '%s' placeholders
@@ -279,18 +275,18 @@ public static String lenientFormat(
279275
break;
280276
}
281277
builder.append(template, templateStart, placeholderStart);
282-
builder.append(args[i++]);
278+
builder.append(lenientToString(args[i++]));
283279
templateStart = placeholderStart + 2;
284280
}
285281
builder.append(template, templateStart, template.length());
286282

287-
// if we run out of placeholders, append the extra args in square braces
283+
// if we run out of placeholders, append the extra args in square brackets
288284
if (i < args.length) {
289-
builder.append(" [");
290-
builder.append(args[i++]);
291-
while (i < args.length) {
292-
builder.append(", ");
293-
builder.append(args[i++]);
285+
String prefix = " [";
286+
for (; i < args.length; i++) {
287+
builder.append(prefix);
288+
builder.append(lenientToString(args[i]));
289+
prefix = ", ";
294290
}
295291
builder.append(']');
296292
}

guava-tests/test/com/google/common/base/StringsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ public void testLenientFormat() {
213213
assertEquals("5 + 6 = 11", Strings.lenientFormat("5 + %s = 11", 6));
214214
assertEquals("5 + 6 = 11", Strings.lenientFormat("5 + 6 = %s", 11));
215215
assertEquals("5 + 6 = 11", Strings.lenientFormat("%s + %s = %s", 5, 6, 11));
216+
assertEquals(
217+
"5 + 6 = 11", Strings.lenientFormat("%s + %s = %s", (Object[]) new Integer[] {5, 6, 11}));
216218
assertEquals("null [null, null]", Strings.lenientFormat("%s", null, null, null));
217219
assertEquals("null [5, 6]", Strings.lenientFormat(null, 5, 6));
218220
assertEquals("null", Strings.lenientFormat("%s", (Object) null));

guava/src/com/google/common/base/Strings.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,6 @@ public static String lenientFormat(
269269

270270
if (args == null) {
271271
args = new Object[] {"(Object[])null"};
272-
} else {
273-
for (int i = 0; i < args.length; i++) {
274-
args[i] = lenientToString(args[i]);
275-
}
276272
}
277273

278274
// start substituting the arguments into the '%s' placeholders
@@ -285,18 +281,18 @@ public static String lenientFormat(
285281
break;
286282
}
287283
builder.append(template, templateStart, placeholderStart);
288-
builder.append(args[i++]);
284+
builder.append(lenientToString(args[i++]));
289285
templateStart = placeholderStart + 2;
290286
}
291287
builder.append(template, templateStart, template.length());
292288

293-
// if we run out of placeholders, append the extra args in square braces
289+
// if we run out of placeholders, append the extra args in square brackets
294290
if (i < args.length) {
295-
builder.append(" [");
296-
builder.append(args[i++]);
297-
while (i < args.length) {
298-
builder.append(", ");
299-
builder.append(args[i++]);
291+
String prefix = " [";
292+
for (; i < args.length; i++) {
293+
builder.append(prefix);
294+
builder.append(lenientToString(args[i]));
295+
prefix = ", ";
300296
}
301297
builder.append(']');
302298
}

0 commit comments

Comments
 (0)