8031588: warnings from b03 for jdk/src/share/native/sun/security/jgss/wrapper: JNI exception pending
authormichaelm
Thu, 06 Feb 2014 22:12:09 +0000 (2014-02-06)
changeset 22940 56b3ab8ec81c
parent 22939 fc08f20ea32f
child 22941 e39416d4eb9a
8031588: warnings from b03 for jdk/src/share/native/sun/security/jgss/wrapper: JNI exception pending Reviewed-by: chegar, mullan
jdk/src/share/native/sun/security/jgss/wrapper/GSSLibStub.c
jdk/src/share/native/sun/security/jgss/wrapper/NativeUtil.c
jdk/src/share/native/sun/security/jgss/wrapper/NativeUtil.h
--- a/jdk/src/share/native/sun/security/jgss/wrapper/GSSLibStub.c	Thu Feb 06 10:06:09 2014 -0800
+++ b/jdk/src/share/native/sun/security/jgss/wrapper/GSSLibStub.c	Thu Feb 06 22:12:09 2014 +0000
@@ -28,6 +28,7 @@
 #include "NativeFunc.h"
 #include "jlong.h"
 #include <jni.h>
+#include "jni_util.h"
 
 /* Throws a Java Exception by name */
 
@@ -36,6 +37,7 @@
 
     if (cls != 0) /* Otherwise an exception has already been thrown */
         (*env)->ThrowNew(env, cls, msg);
+    JNU_Equals(env, NULL, NULL);
 }
 
 void throwOutOfMemoryError(JNIEnv *env, const char *message) {
@@ -65,6 +67,12 @@
     }
 
     libName = (*env)->GetStringUTFChars(env, jlibName, NULL);
+    if (libName == NULL) {
+        if (!(*env)->ExceptionCheck(env)) {
+            throwOutOfMemoryError(env, NULL);
+        }
+        return JNI_FALSE;
+    }
     sprintf(debugBuf, "[GSSLibStub_init] libName=%s", libName);
     debug(env, debugBuf);
 
@@ -110,6 +118,8 @@
         }
       }
       (*env)->ReleaseByteArrayElements(env, jbytes, bytes, 0);
+    } else {
+      JNU_CHECK_EXCEPTION_RETURN(env, jlong_zero);
     }
     if (found != JNI_TRUE) {
       checkStatus(env, NULL, GSS_S_BAD_MECH, 0, "[GSSLibStub_getMechPtr]");
@@ -147,7 +157,9 @@
     cb->initiator_addrtype = GSS_C_AF_INET;
     value = (*env)->CallObjectMethod(env, jinetAddr,
                                      MID_InetAddress_getAddr);
-    initGSSBuffer(env, value, &(cb->initiator_address));
+    if (!initGSSBuffer(env, value, &(cb->initiator_address))) {
+        return NULL;
+    }
   } else {
     cb->initiator_addrtype = GSS_C_AF_NULLADDR;
     cb->initiator_address.length = 0;
@@ -161,7 +173,9 @@
     cb->acceptor_addrtype = GSS_C_AF_INET;
     value = (*env)->CallObjectMethod(env, jinetAddr,
                                      MID_InetAddress_getAddr);
-    initGSSBuffer(env, value, &(cb->acceptor_address));
+    if (!initGSSBuffer(env, value, &(cb->acceptor_address))) {
+        return NULL;
+    }
   } else {
     cb->acceptor_addrtype = GSS_C_AF_NULLADDR;
     cb->acceptor_address.length = 0;
@@ -171,7 +185,9 @@
   value = (*env)->CallObjectMethod(env, jcb,
                                    MID_ChannelBinding_getAppData);
   if (value != NULL) {
-    initGSSBuffer(env, value, &(cb->application_data));
+    if (!initGSSBuffer(env, value, &(cb->application_data))) {
+        return NULL;
+    }
   } else {
     cb->application_data.length = 0;
     cb->application_data.value = NULL;
@@ -230,6 +246,7 @@
     isUnseq = ((suppInfo & GSS_S_UNSEQ_TOKEN) != 0);
     hasGap = ((suppInfo & GSS_S_GAP_TOKEN) != 0);
     minorMsg = getMinorMessage(env, jstub, minor);
+    CHECK_NULL(minorMsg);
     (*env)->CallVoidMethod(env, jprop, MID_MessageProp_setSupplementaryStates,
                            isDuplicate, isOld, isUnseq, hasGap, minor,
                            minorMsg);
@@ -277,6 +294,7 @@
   /* release intermediate buffers */
   deleteGSSOIDSet(nameTypes);
 
+  CHECK_NULL_RETURN(result, NULL);
   checkStatus(env, jobj, major, minor, "[GSSLibStub_inquireNamesForMech]");
   return result;
   } else return NULL;
@@ -326,7 +344,9 @@
 
   debug(env, "[GSSLibStub_importName]");
 
-  initGSSBuffer(env, jnameVal, &nameVal);
+  if (!initGSSBuffer(env, jnameVal, &nameVal)) {
+    return jlong_zero;
+  }
   nameType = newGSSOID(env, jnameType);
   if ((*env)->ExceptionCheck(env)) {
     deleteGSSOID(nameType);
@@ -501,9 +521,14 @@
 
   /* release intermediate buffers */
   jname = getJavaString(env, &outNameBuf);
+  if (jname == NULL && !(*env)->ExceptionCheck(env)) {
+    throwOutOfMemoryError(env, NULL);
+    return NULL;
+  }
 
   jtype = getJavaOID(env, outNameType);
   jresult = (*env)->NewObjectArray(env, 2, CLS_Object, NULL);
+  CHECK_NULL_RETURN(jresult, NULL);
 
   /* return immediately if an exception has occurred */
   if ((*env)->ExceptionCheck(env)) {
@@ -738,7 +763,9 @@
   debug(env, "[GSSLibStub_importContext]");
 
   contextHdl = GSS_C_NO_CONTEXT;
-  initGSSBuffer(env, jctxtToken, &ctxtToken);
+  if (!initGSSBuffer(env, jctxtToken, &ctxtToken)) {
+    return NULL;
+  }
 
   /* gss_import_sec_context(...) => GSS_S_NO_CONTEXT, GSS_S_DEFECTIVE_TOKEN,
      GSS_S_UNAVAILABLE, GSS_S_UNAUTHORIZED */
@@ -829,7 +856,9 @@
     return NULL;
   }
 
-  initGSSBuffer(env, jinToken, &inToken);
+  if (!initGSSBuffer(env, jinToken, &inToken)) {
+    return NULL;
+  }
 
   sprintf(debugBuf,
           "[GSSLibStub_initContext] before: pCred=%ld, pContext=%ld",
@@ -927,7 +956,9 @@
   contextHdl = (gss_ctx_id_t)jlong_to_ptr(
     (*env)->GetLongField(env, jcontextSpi, FID_NativeGSSContext_pContext));
   credHdl = (gss_cred_id_t) jlong_to_ptr(pCred);
-  initGSSBuffer(env, jinToken, &inToken);
+  if (!initGSSBuffer(env, jinToken, &inToken)) {
+    return NULL;
+  }
   cb = getGSSCB(env, jcb);
   if ((*env)->ExceptionCheck(env)) {
     free(cb);
@@ -1102,6 +1133,7 @@
   result[5] = (jlong) getJavaTime(time);
 
   jresult = (*env)->NewLongArray(env, 6);
+  CHECK_NULL_RETURN(jresult, NULL);
   (*env)->SetLongArrayRegion(env, jresult, 0, 6, result);
 
   /* release intermediate buffers */
@@ -1335,7 +1367,9 @@
   }
   contextHdl = (gss_ctx_id_t) jlong_to_ptr(pContext);
   qop = (gss_qop_t) jqop;
-  initGSSBuffer(env, jmsg, &msg);
+  if (!initGSSBuffer(env, jmsg, &msg)) {
+    return NULL;
+  }
 
   /* gss_get_mic(...) => GSS_S_CONTEXT_EXPIRED, GSS_S_NO_CONTEXT(!),
      GSS_S_BAD_QOP */
@@ -1379,8 +1413,12 @@
         "[GSSLibStub_verifyMic]");
     return;
   }
-  initGSSBuffer(env, jmsg, &msg);
-  initGSSBuffer(env, jmsgToken, &msgToken);
+
+  if (!initGSSBuffer(env, jmsg, &msg) ||
+      !initGSSBuffer(env, jmsgToken, &msgToken)) {
+    return;
+  }
+
   qop = (gss_qop_t) (*env)->CallIntMethod(env, jprop, MID_MessageProp_getQOP);
   /* gss_verify_mic(...) => GSS_S_DEFECTIVE_TOKEN, GSS_S_BAD_MIC,
      GSS_S_CONTEXT_EXPIRED, GSS_S_DUPLICATE_TOKEN(!), GSS_S_OLD_TOKEN(!),
@@ -1433,7 +1471,9 @@
     (*env)->CallBooleanMethod(env, jprop, MID_MessageProp_getPrivacy);
   qop = (gss_qop_t)
     (*env)->CallIntMethod(env, jprop, MID_MessageProp_getQOP);
-  initGSSBuffer(env, jmsg, &msg);
+  if (!initGSSBuffer(env, jmsg, &msg)) {
+    return NULL;
+  }
   /* gss_wrap(...) => GSS_S_CONTEXT_EXPIRED, GSS_S_NO_CONTEXT(!),
      GSS_S_BAD_QOP */
   major = (*ftab->wrap)(&minor, contextHdl, confFlag, qop, &msg, &confState,
@@ -1479,7 +1519,9 @@
     checkStatus(env, jobj, GSS_S_CONTEXT_EXPIRED, 0, "[GSSLibStub_unwrap]");
     return NULL;
   }
-  initGSSBuffer(env, jmsgToken, &msgToken);
+  if (!initGSSBuffer(env, jmsgToken, &msgToken)) {
+    return NULL;
+  }
   confState = 0;
   qop = GSS_C_QOP_DEFAULT;
   /* gss_unwrap(...) => GSS_S_DEFECTIVE_TOKEN, GSS_S_BAD_MIC,
--- a/jdk/src/share/native/sun/security/jgss/wrapper/NativeUtil.c	Thu Feb 06 10:06:09 2014 -0800
+++ b/jdk/src/share/native/sun/security/jgss/wrapper/NativeUtil.c	Thu Feb 06 22:12:09 2014 +0000
@@ -27,6 +27,7 @@
 #include "NativeFunc.h"
 #include "jlong.h"
 #include <jni.h>
+#include "jni_util.h"
 
 extern void throwOutOfMemoryError(JNIEnv *env, const char *message);
 
@@ -495,6 +496,9 @@
                              &messageContext, &statusString);
   /* release intermediate buffers */
   msg = getJavaString(env, &statusString);
+  if (msg == NULL && !(*env)->ExceptionCheck(env)) {
+    throwOutOfMemoryError(env, NULL);
+  }
   (*ftab->releaseBuffer)(&minor, &statusString);
   return msg;
 }
@@ -529,6 +533,7 @@
     jmsg = NULL;
     if (minor != 0) {
       jmsg = getMinorMessage(env, jstub, minor);
+      CHECK_NULL(jmsg);
     }
     gssEx = (*env)->NewObject(env, CLS_GSSException,
                               MID_GSSException_ctor3,
@@ -545,28 +550,41 @@
     }
     jmajor = 13; /* use GSSException.FAILURE for now */
     jmsg = (*env)->NewStringUTF(env, msg);
+    CHECK_NULL(jmsg);
     gssEx = (*env)->NewObject(env, CLS_GSSException,
                               MID_GSSException_ctor3,
                               jmajor, jminor, jmsg);
+    CHECK_NULL(gssEx);
     (*env)->Throw(env, gssEx);
   }
 }
+
 /*
  * Utility routine for initializing gss_buffer_t structure
  * with the byte[] in the specified jbyteArray object.
  * NOTE: need to call resetGSSBuffer(...) to free up
  * the resources.
+ * Return JNI_TRUE if GetByteArrayElements() returns ok, JNI_FALSE otherwise
+ * If JNI_FALSE returned, then an exception has been thrown.
  */
-void initGSSBuffer(JNIEnv *env, jbyteArray jbytes,
+int initGSSBuffer(JNIEnv *env, jbyteArray jbytes,
                    gss_buffer_t cbytes) {
   if (jbytes != NULL) {
     cbytes->length = (*env)->GetArrayLength(env, jbytes);
     cbytes->value = (*env)->GetByteArrayElements(env, jbytes, NULL);
+    if (cbytes->value == NULL) {
+        if (!(*env)->ExceptionCheck(env)) {
+            throwOutOfMemoryError(env, NULL);
+        }
+        return JNI_FALSE;
+    }
   } else {
     cbytes->length = 0;
     cbytes->value = NULL;
   }
+  return JNI_TRUE;
 }
+
 /*
  * Utility routine for unpinning/releasing the byte[]
  * associated with the specified jbyteArray object.
@@ -593,7 +611,8 @@
   if ((cbytes != NULL) && (cbytes != GSS_C_NO_BUFFER) &&
       (cbytes->length != 0)) {
     result = (*env)->NewByteArray(env, cbytes->length);
-    (*env)->SetByteArrayRegion(env, result, 0, cbytes->length,
+    if (result != NULL)
+        (*env)->SetByteArrayRegion(env, result, 0, cbytes->length,
                                cbytes->value);
     (*ftab->releaseBuffer)(&minor, cbytes);
     return result;
@@ -616,6 +635,7 @@
     if ((*env)->ExceptionCheck(env)) {
       gssEx = (*env)->ExceptionOccurred(env);
       (*env)->Throw(env, gssEx);
+      return GSS_C_NO_OID;
     }
     cOid = malloc(sizeof(struct gss_OID_desc_struct));
     if (cOid == NULL) {
@@ -665,13 +685,12 @@
   oidHdr[0] = 6;
   oidHdr[1] = cLen;
   jbytes = (*env)->NewByteArray(env, cLen+2);
+  CHECK_NULL_RETURN(jbytes, NULL);
   (*env)->SetByteArrayRegion(env, jbytes, 0, 2, (jbyte *) oidHdr);
   (*env)->SetByteArrayRegion(env, jbytes, 2, cLen, (jbyte *) cOid->elements);
 
   result = (*env)->NewObject(env, CLS_Oid, MID_Oid_ctor1, jbytes);
-  if ((*env)->ExceptionCheck(env)) {
-    (*env)->Throw(env, (*env)->ExceptionOccurred(env));
-  }
+  JNU_CHECK_EXCEPTION_RETURN(env, NULL);
   (*env)->DeleteLocalRef(env, jbytes);
   return result;
 }
@@ -722,6 +741,9 @@
   if (cOidSet != NULL && cOidSet != GSS_C_NO_OID_SET) {
     numOfOids = cOidSet->count;
     jOidSet = (*env)->NewObjectArray(env, numOfOids, CLS_Oid, NULL);
+    if (jOidSet == NULL) {
+      return NULL;
+    }
     if (jOidSet != NULL) {
       for (i = 0; i < numOfOids; i++) {
         jOid = getJavaOID(env, &(cOidSet->elements[i]));
@@ -736,6 +758,7 @@
 
 void debug(JNIEnv *env, char *msg) {
   jstring jmsg = (*env)->NewStringUTF(env, msg);
+  CHECK_NULL(jmsg);
   (*env)->CallStaticVoidMethod(env, CLS_SunNativeProvider,
                                MID_SunNativeProvider_debug, jmsg);
   (*env)->DeleteLocalRef(env, jmsg);
--- a/jdk/src/share/native/sun/security/jgss/wrapper/NativeUtil.h	Thu Feb 06 10:06:09 2014 -0800
+++ b/jdk/src/share/native/sun/security/jgss/wrapper/NativeUtil.h	Thu Feb 06 22:12:09 2014 +0000
@@ -37,7 +37,7 @@
   extern OM_uint32 getGSSTime(jint);
   extern void checkStatus(JNIEnv *, jobject, OM_uint32, OM_uint32, char*);
   extern jint checkTime(OM_uint32);
-  extern void initGSSBuffer(JNIEnv *, jbyteArray, gss_buffer_t);
+  extern jint initGSSBuffer(JNIEnv *, jbyteArray, gss_buffer_t);
   extern void resetGSSBuffer(JNIEnv *, jbyteArray, gss_buffer_t);
 
   extern gss_OID newGSSOID(JNIEnv *, jobject);