From e5ee8063812a4304833628545010e09ccae8360c Mon Sep 17 00:00:00 2001 From: Theresa Mammarella Date: Thu, 16 May 2024 10:31:49 -0400 Subject: [PATCH] Throw OOM if array size exceeds int maximum Signed-off-by: Theresa Mammarella --- .../share/classes/java/lang/StringCoding.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/java.base/share/classes/java/lang/StringCoding.java b/src/java.base/share/classes/java/lang/StringCoding.java index 097c1566542..a2aa7c77876 100644 --- a/src/java.base/share/classes/java/lang/StringCoding.java +++ b/src/java.base/share/classes/java/lang/StringCoding.java @@ -23,6 +23,12 @@ * questions. */ +/* +* =========================================================================== +* (c) Copyright IBM Corp. 2024, 2024 All Rights Reserved +* =========================================================================== +*/ + package java.lang; import java.io.UnsupportedEncodingException; @@ -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) {