8230743: StringJoiner does not handle too large strings correctly

Reviewed-by: rriggs, psandoz, martin
This commit is contained in:
Jim Laskey 2020-06-05 09:37:14 -03:00
parent cb960ee7b5
commit 4de4200652
3 changed files with 77 additions and 2 deletions

View file

@ -125,6 +125,7 @@ public final class StringJoiner {
this.prefix = prefix.toString();
this.delimiter = delimiter.toString();
this.suffix = suffix.toString();
checkAddLength(0, 0);
}
/**
@ -202,13 +203,22 @@ public final class StringJoiner {
} else {
if (size == elts.length)
elts = Arrays.copyOf(elts, 2 * size);
len += delimiter.length();
len = checkAddLength(len, delimiter.length());
}
len += elt.length();
len = checkAddLength(len, elt.length());
elts[size++] = elt;
return this;
}
private int checkAddLength(int oldLen, int inc) {
long newLen = (long)oldLen + (long)inc;
long tmpLen = newLen + (long)prefix.length() + (long)suffix.length();
if (tmpLen != (int)tmpLen) {
throw new OutOfMemoryError("Requested array size exceeds VM limit");
}
return (int)newLen;
}
/**
* Adds the contents of the given {@code StringJoiner} without prefix and
* suffix as the next element if it is non-empty. If the given {@code