mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8230743: StringJoiner does not handle too large strings correctly
Reviewed-by: rriggs, psandoz, martin
This commit is contained in:
parent
cb960ee7b5
commit
4de4200652
3 changed files with 77 additions and 2 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue