Skip to content

Commit

Permalink
Throw OOM if array size exceeds int maximum
Browse files Browse the repository at this point in the history
Signed-off-by: Theresa Mammarella <Theresa.T.Mammarella@ibm.com>
  • Loading branch information
theresa-m committed May 16, 2024
1 parent d9d4bd4 commit e446918
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 e446918

Please sign in to comment.