Skip to content

Commit a043bd0

Browse files
committedJun 8, 2020
8246632: StringConcatFactory::makeConcatWithConstants no longer throws NullPointerException when an unexpected constant is null
Reviewed-by: rriggs, mchung
1 parent 5805cbe commit a043bd0

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed
 

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

+23-15
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ public static CallSite makeConcatWithConstants(MethodHandles.Lookup lookup,
329329
Objects.requireNonNull(concatType, "Concat type is null");
330330
Objects.requireNonNull(constants, "Constants are null");
331331

332+
for (Object o : constants) {
333+
Objects.requireNonNull(o, "Cannot accept null constants");
334+
}
335+
332336
if ((lookup.lookupModes() & MethodHandles.Lookup.PRIVATE) == 0) {
333337
throw new StringConcatException("Invalid caller: " +
334338
lookup.lookupClass().getName());
@@ -382,11 +386,11 @@ private static List<String> parseRecipe(MethodType concatType,
382386
if (c == TAG_CONST) {
383387
if (cCount == constants.length) {
384388
// Not enough constants
385-
throw constantMismatch(concatType, oCount);
389+
throw constantMismatch(constants, cCount);
386390
}
387391
// Accumulate constant args along with any constants encoded
388392
// into the recipe
389-
acc.append(Objects.requireNonNull(constants[cCount++], "Cannot accept null constants"));
393+
acc.append(constants[cCount++]);
390394
} else if (c == TAG_ARG) {
391395
// Flush any accumulated characters into a constant
392396
if (acc.length() > 0) {
@@ -406,28 +410,32 @@ private static List<String> parseRecipe(MethodType concatType,
406410
if (acc.length() > 0) {
407411
elements.add(acc.toString());
408412
}
409-
410413
if (oCount != concatType.parameterCount()) {
411-
throw constantMismatch(concatType, oCount);
414+
throw argumentMismatch(concatType, oCount);
412415
}
413-
if (cCount != constants.length) {
414-
throw new StringConcatException(
415-
"Mismatched number of concat constants: recipe wants " +
416-
cCount +
417-
" constants, but only " +
418-
constants.length +
419-
" are passed");
416+
if (cCount < constants.length) {
417+
throw constantMismatch(constants, cCount);
420418
}
421419
return elements;
422420
}
423421

424-
private static StringConcatException constantMismatch(MethodType concatType,
422+
private static StringConcatException argumentMismatch(MethodType concatType,
425423
int oCount) {
426424
return new StringConcatException(
427425
"Mismatched number of concat arguments: recipe wants " +
428-
oCount +
429-
" arguments, but signature provides " +
430-
concatType.parameterCount());
426+
oCount +
427+
" arguments, but signature provides " +
428+
concatType.parameterCount());
429+
}
430+
431+
private static StringConcatException constantMismatch(Object[] constants,
432+
int cCount) {
433+
return new StringConcatException(
434+
"Mismatched number of concat constants: recipe wants " +
435+
cCount +
436+
" constants, but only " +
437+
constants.length +
438+
" are passed");
431439
}
432440

433441
/**

‎test/jdk/java/lang/String/concat/StringConcatFactoryInvariants.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public static void main(String[] args) throws Throwable {
220220
fail("Static arguments and recipe mismatch: too many",
221221
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, mtThreshold, recipeThreshold, "bar", "baz"));
222222

223-
fail("Static arguments and recipe mismatch, too many, overflowing constant is null",
223+
failNPE("Static arguments and recipe mismatch, too many, overflowing constant is null",
224224
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, mtThreshold, recipeThreshold, "bar", null));
225225

226226
// Advanced factory: check for mismatched recipe and dynamic arguments

0 commit comments

Comments
 (0)
Please sign in to comment.