Skip to content

Commit af5c303

Browse files
martinuyRealCLanger
authored andcommitted
8297371: Improve UTF8 representation redux
Reviewed-by: yan Backport-of: a44eb133d3cdb190abb0210c201e315d94d09dc7
1 parent 9846193 commit af5c303

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/java.base/share/native/libjava/jni_util.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* questions.
2424
*/
2525

26+
#include <limits.h>
2627
#include <stdlib.h>
2728
#include <string.h>
2829

@@ -35,8 +36,13 @@
3536
* such as "z:" need to be appended with a "." so we
3637
* must allocate at least 4 bytes to allow room for
3738
* this expansion. See 4235353 for details.
39+
* This macro returns NULL if the requested size is
40+
* negative, or the size is INT_MAX as the macro adds 1
41+
* that overflows into negative value.
3842
*/
39-
#define MALLOC_MIN4(len) ((char *)malloc((len) + 1 < 4 ? 4 : (len) + 1))
43+
#define MALLOC_MIN4(len) ((unsigned)(len) >= INT_MAX ? \
44+
NULL : \
45+
((char *)malloc((len) + 1 < 4 ? 4 : (len) + 1)))
4046

4147
/**
4248
* Throw a Java exception by name. Similar to SignalError.
@@ -885,17 +891,10 @@ getStringUTF8(JNIEnv *env, jstring jstr)
885891
}
886892
}
887893

888-
// Check `jint` overflow
889-
if (rlen < 0) {
890-
(*env)->ReleasePrimitiveArrayCritical(env, value, str, 0);
891-
JNU_ThrowOutOfMemoryError(env, "requested array size exceeds VM limit");
892-
return NULL;
893-
}
894-
895894
result = MALLOC_MIN4(rlen);
896895
if (result == NULL) {
897896
(*env)->ReleasePrimitiveArrayCritical(env, value, str, 0);
898-
JNU_ThrowOutOfMemoryError(env, 0);
897+
JNU_ThrowOutOfMemoryError(env, "requested array size exceeds VM limit");
899898
return NULL;
900899
}
901900

0 commit comments

Comments
 (0)