Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
8246632: StringConcatFactory::makeConcatWithConstants no longer throw…
…s NullPointerException when an unexpected constant is null

Reviewed-by: rriggs, mchung
  • Loading branch information
cl4es committed Jun 8, 2020
1 parent 5805cbe commit a043bd0ff8b1f558ff74125206ae2d4279751be9
Showing with 24 additions and 16 deletions.
  1. +23 −15 src/java.base/share/classes/java/lang/invoke/StringConcatFactory.java
  2. +1 −1 test/jdk/java/lang/String/concat/StringConcatFactoryInvariants.java
@@ -329,6 +329,10 @@ public static CallSite makeConcatWithConstants(MethodHandles.Lookup lookup,
Objects.requireNonNull(concatType, "Concat type is null");
Objects.requireNonNull(constants, "Constants are null");

for (Object o : constants) {
Objects.requireNonNull(o, "Cannot accept null constants");
}

if ((lookup.lookupModes() & MethodHandles.Lookup.PRIVATE) == 0) {
throw new StringConcatException("Invalid caller: " +
lookup.lookupClass().getName());
@@ -382,11 +386,11 @@ private static List<String> parseRecipe(MethodType concatType,
if (c == TAG_CONST) {
if (cCount == constants.length) {
// Not enough constants
throw constantMismatch(concatType, oCount);
throw constantMismatch(constants, cCount);
}
// Accumulate constant args along with any constants encoded
// into the recipe
acc.append(Objects.requireNonNull(constants[cCount++], "Cannot accept null constants"));
acc.append(constants[cCount++]);
} else if (c == TAG_ARG) {
// Flush any accumulated characters into a constant
if (acc.length() > 0) {
@@ -406,28 +410,32 @@ private static List<String> parseRecipe(MethodType concatType,
if (acc.length() > 0) {
elements.add(acc.toString());
}

if (oCount != concatType.parameterCount()) {
throw constantMismatch(concatType, oCount);
throw argumentMismatch(concatType, oCount);
}
if (cCount != constants.length) {
throw new StringConcatException(
"Mismatched number of concat constants: recipe wants " +
cCount +
" constants, but only " +
constants.length +
" are passed");
if (cCount < constants.length) {
throw constantMismatch(constants, cCount);
}
return elements;
}

private static StringConcatException constantMismatch(MethodType concatType,
private static StringConcatException argumentMismatch(MethodType concatType,
int oCount) {
return new StringConcatException(
"Mismatched number of concat arguments: recipe wants " +
oCount +
" arguments, but signature provides " +
concatType.parameterCount());
oCount +
" arguments, but signature provides " +
concatType.parameterCount());
}

private static StringConcatException constantMismatch(Object[] constants,
int cCount) {
return new StringConcatException(
"Mismatched number of concat constants: recipe wants " +
cCount +
" constants, but only " +
constants.length +
" are passed");
}

/**
@@ -220,7 +220,7 @@ public static void main(String[] args) throws Throwable {
fail("Static arguments and recipe mismatch: too many",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, mtThreshold, recipeThreshold, "bar", "baz"));

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

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

0 comments on commit a043bd0

Please sign in to comment.