8145015: jni_GetStringCritical asserts for empty strings
authorsimonis
Tue, 15 Dec 2015 00:16:09 -0500
changeset 35162 65d0237246b1
parent 35080 b9dd459157f0
child 35163 448e610805bb
child 35164 80ecc1fdfbd6
8145015: jni_GetStringCritical asserts for empty strings Reviewed-by: thartmann, dholmes
hotspot/src/share/vm/prims/jni.cpp
--- a/hotspot/src/share/vm/prims/jni.cpp	Mon Dec 14 19:49:15 2015 +0000
+++ b/hotspot/src/share/vm/prims/jni.cpp	Tue Dec 15 00:16:09 2015 -0500
@@ -3194,17 +3194,20 @@
   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;