Skip to content

Commit

Permalink
Merge pull request #352 from theresa-m/arrayoom
Browse files Browse the repository at this point in the history
Throw OOM if array size exceeds int maximum
  • Loading branch information
keithc-ca authored May 16, 2024
2 parents 9be897c + e446918 commit e583cf9
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/java.base/share/classes/java/lang/String.java
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,11 @@ private static byte[] safeTrim(byte[] ba, int len, boolean isTrusted) {
private static int scale(int len, float expansionFactor) {
// We need to perform double, not float, arithmetic; otherwise
// we lose low order bits when len is larger than 2**24.
return (int)(len * (double)expansionFactor);
double result = len * (double)expansionFactor;
if (result > (double)Integer.MAX_VALUE) {
throw new OutOfMemoryError("Requested array size exceeds limit");
}
return (int)result;
}

private static Charset lookupCharset(String csn) throws UnsupportedEncodingException {
Expand Down

0 comments on commit e583cf9

Please sign in to comment.