8030993: Check jdk/src/share/native/common/jni_util.c for JNI pending exceptions
authorrriggs
Wed, 05 Feb 2014 10:59:53 -0500
changeset 22653 7a464b795af5
parent 22652 141565f2aafc
child 22654 da81e0be094a
8030993: Check jdk/src/share/native/common/jni_util.c for JNI pending exceptions Summary: Check for pending exceptions Reviewed-by: alanb, chegar
jdk/src/share/native/common/jni_util.c
--- a/jdk/src/share/native/common/jni_util.c	Wed Feb 05 15:58:27 2014 +0000
+++ b/jdk/src/share/native/common/jni_util.c	Wed Feb 05 10:59:53 2014 -0500
@@ -626,10 +626,14 @@
 {
     jstring propname = 0;
     jstring enc = 0;
+    jclass strClazz = NULL;
 
     if ((*env)->EnsureLocalCapacity(env, 3) < 0)
         return;
 
+    strClazz = JNU_ClassString(env);
+    CHECK_NULL(strClazz);
+
     propname = (*env)->NewStringUTF(env, "sun.jnu.encoding");
     if (propname) {
         jboolean exc;
@@ -683,9 +687,10 @@
     (*env)->DeleteLocalRef(env, enc);
 
     /* Initialize method-id cache */
-    String_getBytes_ID = (*env)->GetMethodID(env, JNU_ClassString(env),
+    String_getBytes_ID = (*env)->GetMethodID(env, strClazz,
                                              "getBytes", "(Ljava/lang/String;)[B");
-    String_init_ID = (*env)->GetMethodID(env, JNU_ClassString(env),
+    CHECK_NULL(String_getBytes_ID);
+    String_init_ID = (*env)->GetMethodID(env, strClazz,
                                          "<init>", "([BLjava/lang/String;)V");
 }
 
@@ -720,8 +725,10 @@
         jbyteArray hab = 0;
         int len;
 
-        if (fastEncoding == NO_ENCODING_YET)
+        if (fastEncoding == NO_ENCODING_YET) {
             initializeEncoding(env);
+            JNU_CHECK_EXCEPTION_RETURN(env, NULL);
+        }
 
         if ((fastEncoding == FAST_8859_1) || (fastEncoding == NO_ENCODING_YET))
             return newString8859_1(env, str);
@@ -736,9 +743,11 @@
         len = (int)strlen(str);
         hab = (*env)->NewByteArray(env, len);
         if (hab != 0) {
+            jclass strClazz = JNU_ClassString(env);
+            CHECK_NULL_RETURN(strClazz, 0);
             (*env)->SetByteArrayRegion(env, hab, 0, len, (jbyte *)str);
             if (jnuEncodingSupported(env)) {
-                result = (*env)->NewObject(env, JNU_ClassString(env),
+                result = (*env)->NewObject(env, strClazz,
                                            String_init_ID, hab, jnuEncoding);
             } else {
                 /*If the encoding specified in sun.jnu.encoding is not endorsed
@@ -747,9 +756,11 @@
                   StringCoding class will pickup the iso-8859-1 as the fallback
                   converter for us.
                  */
-                jmethodID mid = (*env)->GetMethodID(env, JNU_ClassString(env),
+                jmethodID mid = (*env)->GetMethodID(env, strClazz,
                                                     "<init>", "([B)V");
-                result = (*env)->NewObject(env, JNU_ClassString(env), mid, hab);
+                if (mid != NULL) {
+                    result = (*env)->NewObject(env, strClazz, mid, hab);
+                }
             }
             (*env)->DeleteLocalRef(env, hab);
             return result;
@@ -775,8 +786,10 @@
         if (isCopy)
             *isCopy = JNI_TRUE;
 
-        if (fastEncoding == NO_ENCODING_YET)
+        if (fastEncoding == NO_ENCODING_YET) {
             initializeEncoding(env);
+            JNU_CHECK_EXCEPTION_RETURN(env, 0);
+        }
 
         if ((fastEncoding == FAST_8859_1) || (fastEncoding == NO_ENCODING_YET))
             return getString8859_1Chars(env, jstr);
@@ -791,9 +804,14 @@
         if (jnuEncodingSupported(env)) {
             hab = (*env)->CallObjectMethod(env, jstr, String_getBytes_ID, jnuEncoding);
         } else {
-            jmethodID mid = (*env)->GetMethodID(env, JNU_ClassString(env),
-                                                "getBytes", "()[B");
-            hab = (*env)->CallObjectMethod(env, jstr, mid);
+            jmethodID mid;
+            jclass strClazz = JNU_ClassString(env);
+            CHECK_NULL_RETURN(strClazz, 0);
+            mid = (*env)->GetMethodID(env, strClazz,
+                                           "getBytes", "()[B");
+            if (mid != NULL) {
+                hab = (*env)->CallObjectMethod(env, jstr, mid);
+            }
         }
 
         if (!(*env)->ExceptionCheck(env)) {
@@ -842,6 +860,7 @@
         if ((*env)->EnsureLocalCapacity(env, 1) < 0)
             return 0;
         c = (*env)->FindClass(env, "java/lang/String");
+        CHECK_NULL_RETURN(c, NULL);
         cls = (*env)->NewGlobalRef(env, c);
         (*env)->DeleteLocalRef(env, c);
     }
@@ -857,6 +876,7 @@
         if ((*env)->EnsureLocalCapacity(env, 1) < 0)
             return 0;
         c = (*env)->FindClass(env, "java/lang/Class");
+        CHECK_NULL_RETURN(c, NULL);
         cls = (*env)->NewGlobalRef(env, c);
         (*env)->DeleteLocalRef(env, c);
     }
@@ -872,6 +892,7 @@
         if ((*env)->EnsureLocalCapacity(env, 1) < 0)
             return 0;
         c = (*env)->FindClass(env, "java/lang/Object");
+        CHECK_NULL_RETURN(c, NULL);
         cls = (*env)->NewGlobalRef(env, c);
         (*env)->DeleteLocalRef(env, c);
     }
@@ -887,6 +908,7 @@
         if ((*env)->EnsureLocalCapacity(env, 1) < 0)
             return 0;
         c = (*env)->FindClass(env, "java/lang/Throwable");
+        CHECK_NULL_RETURN(c, NULL);
         cls = (*env)->NewGlobalRef(env, c);
         (*env)->DeleteLocalRef(env, c);
     }
@@ -936,8 +958,11 @@
 {
     static jmethodID mid = NULL;
     if (mid == NULL) {
-        mid = (*env)->GetMethodID(env, JNU_ClassObject(env), "equals",
+        jclass objClazz = JNU_ClassObject(env);
+        CHECK_NULL_RETURN(objClazz, JNI_FALSE);
+        mid = (*env)->GetMethodID(env, objClazz, "equals",
                                   "(Ljava/lang/Object;)Z");
+        CHECK_NULL_RETURN(mid, JNI_FALSE);
     }
     return (*env)->CallBooleanMethod(env, object1, mid, object2);
 }
@@ -1039,7 +1064,9 @@
     } else {
         jclass cls = (*env)->GetObjectClass(env, object);
         jstring clsName = JNU_ToString(env, cls);
-        JNU_PrintString(env, hdr, clsName);
+        if (clsName == NULL) {
+            JNU_PrintString(env, hdr, clsName);
+        }
         (*env)->DeleteLocalRef(env, cls);
         (*env)->DeleteLocalRef(env, clsName);
     }