8297371: Improve UTF8 representation redux

Reviewed-by: rhalade, bchristi
This commit is contained in:
Naoto Sato 2022-12-13 00:10:06 +00:00 committed by Henry Jen
parent 3656939a6a
commit 5ec0120152

View File

@ -35,8 +35,13 @@
* such as "z:" need to be appended with a "." so we
* must allocate at least 4 bytes to allow room for
* this expansion. See 4235353 for details.
* This macro returns NULL if the requested size is
* negative, or the size is INT_MAX as the macro adds 1
* that overflows into negative value.
*/
#define MALLOC_MIN4(len) ((char *)malloc((len) + 1 < 4 ? 4 : (len) + 1))
#define MALLOC_MIN4(len) ((unsigned)(len) >= INT_MAX ? \
NULL : \
((char *)malloc((len) + 1 < 4 ? 4 : (len) + 1)))
/**
* Throw a Java exception by name. Similar to SignalError.
@ -861,17 +866,10 @@ 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);
JNU_ThrowOutOfMemoryError(env, 0);
JNU_ThrowOutOfMemoryError(env, "requested array size exceeds VM limit");
return NULL;
}