8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
authormsheppar
Thu, 20 Mar 2014 22:57:39 +0000
changeset 23560 80b1514e7eac
parent 23559 289e67ca41ee
child 23561 babc1414a7e4
8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c Summary: check JNI return values, check for pending exceptions, check malloc return value and raise OOME as needed Reviewed-by: alanb
jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
--- a/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c	Thu Mar 20 18:18:11 2014 -0400
+++ b/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c	Thu Mar 20 22:57:39 2014 +0000
@@ -53,7 +53,9 @@
 (JNIEnv *env, jclass authseq_clazz, jclass status_clazz)
 {
     ntlm_ctxHandleID = (*env)->GetFieldID(env, authseq_clazz, "ctxHandle", "J");
+    CHECK_NULL(ntlm_ctxHandleID);
     ntlm_crdHandleID = (*env)->GetFieldID(env, authseq_clazz, "crdHandle", "J");
+    CHECK_NULL(ntlm_crdHandleID);
     status_seqCompleteID = (*env)->GetFieldID(env, status_clazz, "sequenceComplete", "Z");
 }
 
@@ -100,6 +102,16 @@
         }
     }
     pCred = (CredHandle *)malloc(sizeof (CredHandle));
+    if (pCred == NULL) {
+        JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
+        if (pUser != NULL)
+            JNU_ReleaseStringPlatformChars(env, user, pUser);
+        if (pPassword != NULL)
+            JNU_ReleaseStringPlatformChars(env, password, pPassword);
+        if (pDomain != NULL)
+            JNU_ReleaseStringPlatformChars(env, domain, pDomain);
+        return NULL;
+    }
 
     if ( ((pUser != NULL) || (pPassword != NULL)) || (pDomain != NULL)) {
         pAuthId = &AuthId;
@@ -177,7 +189,12 @@
     pCtx = (CtxtHandle *) (*env)->GetLongField (env, this, ntlm_ctxHandleID);
     if (pCtx == 0) { /* first call */
         newContext = (CtxtHandle *)malloc(sizeof(CtxtHandle));
-        (*env)->SetLongField (env, this, ntlm_ctxHandleID, (jlong)newContext);
+        if (newContext != NULL) {
+            (*env)->SetLongField (env, this, ntlm_ctxHandleID, (jlong)newContext);
+        } else {
+            JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
+            return NULL;
+        }
     } else {
         newContext = pCtx;
     }
@@ -198,6 +215,7 @@
     if (lastToken != 0)
     {
         pInput = (VOID *)(*env)->GetByteArrayElements(env, lastToken, &isCopy);
+        CHECK_NULL_RETURN(pInput, NULL);
         inputLen = (*env)->GetArrayLength(env, lastToken);
 
         InBuffDesc.ulVersion = 0;
@@ -240,8 +258,10 @@
 
     if ( OutSecBuff.cbBuffer > 0 ) {
         jbyteArray ret = (*env)->NewByteArray(env, OutSecBuff.cbBuffer);
-        (*env)->SetByteArrayRegion(env, ret, 0, OutSecBuff.cbBuffer,
-                OutSecBuff.pvBuffer);
+        if (ret != NULL) {
+            (*env)->SetByteArrayRegion(env, ret, 0, OutSecBuff.cbBuffer,
+                    OutSecBuff.pvBuffer);
+        }
         if (lastToken != 0) // 2nd stage
             endSequence (pCred, pCtx, env, status);
         result = ret;