From 9fc9cf684a7097829591a7e36dcb3e9146562f64 Mon Sep 17 00:00:00 2001 From: Volker Simonis Date: Tue, 15 Dec 2015 00:16:09 -0500 Subject: [PATCH] 8145015: jni_GetStringCritical asserts for empty strings Reviewed-by: thartmann, dholmes --- hotspot/src/share/vm/prims/jni.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/hotspot/src/share/vm/prims/jni.cpp b/hotspot/src/share/vm/prims/jni.cpp index 724d75b8495..a0f5665d276 100644 --- a/hotspot/src/share/vm/prims/jni.cpp +++ b/hotspot/src/share/vm/prims/jni.cpp @@ -3194,17 +3194,20 @@ JNI_ENTRY(const jchar*, jni_GetStringCritical(JNIEnv *env, jstring string, jbool if (isCopy != NULL) { *isCopy = is_latin1 ? JNI_TRUE : JNI_FALSE; } - const jchar* ret; + jchar* ret; if (!is_latin1) { - ret = s_value->char_at_addr(0); + ret = (jchar*) s_value->base(T_CHAR); } else { // Inflate latin1 encoded string to UTF16 int s_len = java_lang_String::length(s); - jchar* buf = NEW_C_HEAP_ARRAY(jchar, s_len, mtInternal); - for (int i = 0; i < s_len; i++) { - buf[i] = ((jchar) s_value->byte_at(i)) & 0xff; + ret = NEW_C_HEAP_ARRAY_RETURN_NULL(jchar, s_len + 1, mtInternal); // add one for zero termination + /* JNI Specification states return NULL on OOM */ + if (ret != NULL) { + for (int i = 0; i < s_len; i++) { + ret[i] = ((jchar) s_value->byte_at(i)) & 0xff; + } + ret[s_len] = 0; } - ret = &buf[0]; } HOTSPOT_JNI_GETSTRINGCRITICAL_RETURN((uint16_t *) ret); return ret;