Skip to content

Commit

Permalink
8246632: StringConcatFactory::makeConcatWithConstants no longer throw…
Browse files Browse the repository at this point in the history
…s NullPointerException when an unexpected constant is null

Reviewed-by: rriggs, mchung
  • Loading branch information
cl4es committed Jun 8, 2020
1 parent 5805cbe commit a043bd0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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) {
Expand All @@ -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");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a043bd0

Please sign in to comment.