jdk/src/share/native/sun/security/jgss/wrapper/NativeUtil.c
changeset 22940 56b3ab8ec81c
parent 16488 816b9df68a9f
child 23010 6dadb192ad81
--- 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);