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 795d069 commit e5ee806
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 e5ee806

Please sign in to comment.