From e44691896bdac2ee48570d1ddec6251c264a0b5f Mon Sep 17 00:00:00 2001 From: Theresa Mammarella Date: Thu, 16 May 2024 10:39:31 -0400 Subject: [PATCH] Throw OOM if array size exceeds int maximum Signed-off-by: Theresa Mammarella --- src/java.base/share/classes/java/lang/String.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/java.base/share/classes/java/lang/String.java b/src/java.base/share/classes/java/lang/String.java index bb415af8125..52621e6e46e 100644 --- a/src/java.base/share/classes/java/lang/String.java +++ b/src/java.base/share/classes/java/lang/String.java @@ -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 {