8033757: Redo JDK-8011983 (due to bad merge with JDK-8031586)
authorvinnie
Thu, 06 Feb 2014 12:24:42 +0000
changeset 22935 4322bab78de4
parent 22934 705df2581571
child 22936 1263cd9144d7
8033757: Redo JDK-8011983 (due to bad merge with JDK-8031586) Reviewed-by: alanb
jdk/src/windows/native/com/sun/security/auth/module/nt.c
--- a/jdk/src/windows/native/com/sun/security/auth/module/nt.c	Thu Feb 06 10:43:09 2014 +0000
+++ b/jdk/src/windows/native/com/sun/security/auth/module/nt.c	Thu Feb 06 12:24:42 2014 +0000
@@ -43,6 +43,12 @@
 BOOL getTextualSid(PSID pSid, LPTSTR TextualSid, LPDWORD lpdwBufferLen);
 void DisplayErrorText(DWORD dwLastError);
 
+static void throwIllegalArgumentException(JNIEnv *env, const char *msg) {
+    jclass clazz = (*env)->FindClass(env, "java/lang/IllegalArgumentException");
+    if (clazz != NULL)
+        (*env)->ThrowNew(env, clazz, msg);
+}
+
 JNIEXPORT jlong JNICALL
 Java_com_sun_security_auth_module_NTSystem_getImpersonationToken0
         (JNIEnv *env, jobject obj) {
@@ -62,7 +68,6 @@
 
     long i, j = 0;
     HANDLE tokenHandle = INVALID_HANDLE_VALUE;
-    BOOL systemError = FALSE;
 
     LPTSTR userName = NULL;             // user name
     LPTSTR userSid = NULL;              // user sid
@@ -116,70 +121,59 @@
 
     fid = (*env)->GetFieldID(env, cls, "userName", "Ljava/lang/String;");
     if (fid == 0) {
-        jclass newExcCls =
-            (*env)->FindClass(env, "java/lang/IllegalArgumentException");
-        if (newExcCls == 0) {
-            // Unable to find exception class
-            systemError = TRUE;
-            goto out;
-        }
-        (*env)->ThrowNew(env, newExcCls, "invalid field: userName");
+        (*env)->ExceptionClear(env);
+        throwIllegalArgumentException(env, "invalid field: userName");
+        goto cleanup;
     }
     jstr = (*env)->NewStringUTF(env, userName);
+    if (jstr == NULL)
+        goto cleanup;
     (*env)->SetObjectField(env, obj, fid, jstr);
 
     fid = (*env)->GetFieldID(env, cls, "userSID", "Ljava/lang/String;");
     if (fid == 0) {
-        jclass newExcCls =
-            (*env)->FindClass(env, "java/lang/IllegalArgumentException");
-        if (newExcCls == 0) {
-            systemError = TRUE;
-            goto out;
-        }
-        (*env)->ThrowNew(env, newExcCls, "invalid field: userSID");
+        (*env)->ExceptionClear(env);
+        throwIllegalArgumentException(env, "invalid field: userSID");
+        goto cleanup;
     }
     jstr = (*env)->NewStringUTF(env, userSid);
+    if (jstr == NULL)
+        goto cleanup;
     (*env)->SetObjectField(env, obj, fid, jstr);
 
     fid = (*env)->GetFieldID(env, cls, "domain", "Ljava/lang/String;");
     if (fid == 0) {
-        jclass newExcCls =
-            (*env)->FindClass(env, "java/lang/IllegalArgumentException");
-        if (newExcCls == 0) {
-            systemError = TRUE;
-            goto out;
-        }
-        (*env)->ThrowNew(env, newExcCls, "invalid field: domain");
+        (*env)->ExceptionClear(env);
+        throwIllegalArgumentException(env, "invalid field: domain");
+        goto cleanup;
     }
     jstr = (*env)->NewStringUTF(env, domainName);
+    if (jstr == NULL)
+        goto cleanup;
     (*env)->SetObjectField(env, obj, fid, jstr);
 
     if (domainSid != NULL) {
         fid = (*env)->GetFieldID(env, cls, "domainSID", "Ljava/lang/String;");
         if (fid == 0) {
-            jclass newExcCls =
-                (*env)->FindClass(env, "java/lang/IllegalArgumentException");
-            if (newExcCls == 0) {
-                systemError = TRUE;
-                goto out;
-            }
-            (*env)->ThrowNew(env, newExcCls, "invalid field: domainSID");
+            (*env)->ExceptionClear(env);
+            throwIllegalArgumentException(env, "invalid field: domainSID");
+            goto cleanup;
         }
         jstr = (*env)->NewStringUTF(env, domainSid);
+        if (jstr == NULL)
+            goto cleanup;
         (*env)->SetObjectField(env, obj, fid, jstr);
     }
 
     fid = (*env)->GetFieldID(env, cls, "primaryGroupID", "Ljava/lang/String;");
     if (fid == 0) {
-        jclass newExcCls =
-            (*env)->FindClass(env, "java/lang/IllegalArgumentException");
-        if (newExcCls == 0) {
-            systemError = TRUE;
-            goto out;
-        }
-        (*env)->ThrowNew(env, newExcCls, "invalid field: PrimaryGroupID");
+        (*env)->ExceptionClear(env);
+        throwIllegalArgumentException(env, "invalid field: PrimaryGroupID");
+        goto cleanup;
     }
     jstr = (*env)->NewStringUTF(env, primaryGroup);
+    if (jstr == NULL)
+        goto cleanup;
     (*env)->SetObjectField(env, obj, fid, jstr);
 
     // primary group may or may not be part of supplementary groups
@@ -204,19 +198,14 @@
 
         fid = (*env)->GetFieldID(env, cls, "groupIDs", "[Ljava/lang/String;");
         if (fid == 0) {
-            jclass newExcCls =
-                (*env)->FindClass(env, "java/lang/IllegalArgumentException");
-            if (newExcCls == 0) {
-                systemError = TRUE;
-                goto out;
-            }
-            (*env)->ThrowNew(env, newExcCls, "invalid field: groupIDs");
+            (*env)->ExceptionClear(env);
+            throwIllegalArgumentException(env, "groupIDs");
+            goto cleanup;
         }
 
         stringClass = (*env)->FindClass(env, "java/lang/String");
-        if (stringClass == 0) {
-            goto out;
-        }
+        if (stringClass == NULL)
+            goto cleanup;
 
         if (pIndex == -1) {
             // primary group not in groups array
@@ -226,6 +215,8 @@
             // allocate one less array entry and do not add into new array
             jgroups = (*env)->NewObjectArray(env, numGroups-1, stringClass, 0);
         }
+        if (jgroups == NULL)
+            goto cleanup;
 
         for (i = 0, j = 0; i < (long)numGroups; i++) {
             if (pIndex == i) {
@@ -233,12 +224,14 @@
                 continue;
             }
             jstr = (*env)->NewStringUTF(env, groups[i]);
+            if (jstr == NULL)
+                goto cleanup;
             (*env)->SetObjectArrayElement(env, jgroups, j++, jstr);
         }
         (*env)->SetObjectField(env, obj, fid, jgroups);
     }
 
-out:
+cleanup:
     if (userName != NULL) {
         HeapFree(GetProcessHeap(), 0, userName);
     }
@@ -264,11 +257,6 @@
     }
     CloseHandle(tokenHandle);
 
-    if (systemError && debug) {
-        printf("  [getCurrent] System Error: ");
-        printf("unable to find IllegalArgumentException class\n");
-    }
-
     return;
 }
 
@@ -336,7 +324,7 @@
             DisplayErrorText(GetLastError());
         }
         error = TRUE;
-        goto out;
+        goto cleanup;
     }
 
     if (debug) {
@@ -369,7 +357,7 @@
             DisplayErrorText(GetLastError());
         }
         error = TRUE;
-        goto out;
+        goto cleanup;
     }
 
     if (debug) {
@@ -411,7 +399,7 @@
             DisplayErrorText(GetLastError());
         }
         // ok not to have a domain SID (no error)
-        goto out;
+        goto cleanup;
     }
 
     bufSize = 0;
@@ -422,7 +410,7 @@
         printf("  [getUser] domainSid: %s\n", *domainSid);
     }
 
-out:
+cleanup:
     if (tokenUserInfo != NULL) {
         HeapFree(GetProcessHeap(), 0, tokenUserInfo);
     }
@@ -466,7 +454,7 @@
             DisplayErrorText(GetLastError());
         }
         error = TRUE;
-        goto out;
+        goto cleanup;
     }
 
     if (debug) {
@@ -481,7 +469,7 @@
         printf("  [getPrimaryGroup] primaryGroup: %s\n", *primaryGroup);
     }
 
-out:
+cleanup:
     if (tokenGroupInfo != NULL) {
         HeapFree(GetProcessHeap(), 0, tokenGroupInfo);
     }
@@ -519,7 +507,7 @@
             DisplayErrorText(GetLastError());
         }
         error = TRUE;
-        goto out;
+        goto cleanup;
     }
 
     if (debug) {
@@ -528,7 +516,7 @@
 
     if (tokenGroupInfo->GroupCount == 0) {
         // no groups
-        goto out;
+        goto cleanup;
     }
 
     // return group info
@@ -545,7 +533,7 @@
         }
     }
 
-out:
+cleanup:
     if (tokenGroupInfo != NULL) {
         HeapFree(GetProcessHeap(), 0, tokenGroupInfo);
     }