Skip to content

Commit

Permalink
Merge pull request #782 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 committed May 16, 2024
2 parents c3757b4 + e5ee806 commit f8e6ecb
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/java.base/share/classes/java/lang/StringCoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
* questions.
*/

/*
* ===========================================================================
* (c) Copyright IBM Corp. 2024, 2024 All Rights Reserved
* ===========================================================================
*/

package java.lang;

import java.io.UnsupportedEncodingException;
Expand Down Expand Up @@ -94,7 +100,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) {
Expand Down

0 comments on commit f8e6ecb

Please sign in to comment.