diff --git a/src/java.base/share/native/libjava/jni_util.c b/src/java.base/share/native/libjava/jni_util.c index 839141a4533..a4ecbbc6cdf 100644 --- a/src/java.base/share/native/libjava/jni_util.c +++ b/src/java.base/share/native/libjava/jni_util.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -869,6 +869,13 @@ getStringUTF8(JNIEnv *env, jstring jstr) } } + // Check `jint` overflow + if (rlen < 0) { + (*env)->ReleasePrimitiveArrayCritical(env, value, str, 0); + JNU_ThrowOutOfMemoryError(env, "requested array size exceeds VM limit"); + return NULL; + } + result = MALLOC_MIN4(rlen); if (result == NULL) { (*env)->ReleasePrimitiveArrayCritical(env, value, str, 0);