8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
authoralanb
Wed, 05 Feb 2014 10:20:30 +0000
changeset 22648 7bcdb55f70ff
parent 22647 7e45e0a1088d
child 22649 f70b08c1593c
8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions Reviewed-by: vinnie, chegar
jdk/src/solaris/native/com/sun/security/auth/module/Solaris.c
jdk/src/solaris/native/com/sun/security/auth/module/Unix.c
jdk/src/windows/native/com/sun/security/auth/module/nt.c
--- a/jdk/src/solaris/native/com/sun/security/auth/module/Solaris.c	Wed Feb 05 11:05:24 2014 +0100
+++ b/jdk/src/solaris/native/com/sun/security/auth/module/Solaris.c	Wed Feb 05 10:20:30 2014 +0000
@@ -32,6 +32,12 @@
 #include <string.h>
 #include <pwd.h>
 
+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 void JNICALL
 Java_com_sun_security_auth_module_SolarisSystem_getSolarisInfo
                                                 (JNIEnv *env, jobject obj) {
@@ -51,7 +57,7 @@
 
     if (groups == NULL) {
         jclass cls = (*env)->FindClass(env,"java/lang/OutOfMemoryError");
-        if(cls != 0)
+        if (cls != NULL)
             (*env)->ThrowNew(env, cls, NULL);
         return;
     }
@@ -67,15 +73,13 @@
          */
         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 the new exception class, give up. */
-                return;
-            }
-            (*env)->ThrowNew(env, newExcCls, "invalid field: username");
+            (*env)->ExceptionClear(env);
+            throwIllegalArgumentException(env, "invalid field: username");
+            return;
         }
         jstr = (*env)->NewStringUTF(env, pwd.pw_name);
+        if (jstr == NULL)
+            return;
         (*env)->SetObjectField(env, obj, fid, jstr);
 
         /*
@@ -83,13 +87,9 @@
          */
         fid = (*env)->GetFieldID(env, cls, "uid", "J");
         if (fid == 0) {
-            jclass newExcCls =
-                (*env)->FindClass(env, "java/lang/IllegalArgumentException");
-            if (newExcCls == 0) {
-                /* Unable to find the new exception class, give up. */
-                return;
-            }
-            (*env)->ThrowNew(env, newExcCls, "invalid field: username");
+            (*env)->ExceptionClear(env);
+            throwIllegalArgumentException(env, "invalid field: uid");
+            return;
         }
         (*env)->SetLongField(env, obj, fid, pwd.pw_uid);
 
@@ -98,13 +98,9 @@
          */
         fid = (*env)->GetFieldID(env, cls, "gid", "J");
         if (fid == 0) {
-            jclass newExcCls =
-                (*env)->FindClass(env, "java/lang/IllegalArgumentException");
-            if (newExcCls == 0) {
-                /* Unable to find the new exception class, give up. */
-                return;
-            }
-            (*env)->ThrowNew(env, newExcCls, "invalid field: username");
+            (*env)->ExceptionClear(env);
+            throwIllegalArgumentException(env, "invalid field: gid");
+            return;
         }
         (*env)->SetLongField(env, obj, fid, pwd.pw_gid);
 
@@ -113,17 +109,17 @@
          */
         fid = (*env)->GetFieldID(env, cls, "groups", "[J");
         if (fid == 0) {
-            jclass newExcCls =
-                (*env)->FindClass(env, "java/lang/IllegalArgumentException");
-            if (newExcCls == 0) {
-                /* Unable to find the new exception class, give up. */
-                return;
-            }
-            (*env)->ThrowNew(env, newExcCls, "invalid field: username");
+            (*env)->ExceptionClear(env);
+            throwIllegalArgumentException(env, "invalid field: groups");
+            return;
         }
 
         jgroups = (*env)->NewLongArray(env, numSuppGroups);
+        if (jgroups == NULL)
+            return;
         jgroupsAsArray = (*env)->GetLongArrayElements(env, jgroups, 0);
+        if (jgroupsAsArray == NULL)
+            return;
         for (i = 0; i < numSuppGroups; i++)
             jgroupsAsArray[i] = groups[i];
         (*env)->ReleaseLongArrayElements(env, jgroups, jgroupsAsArray, 0);
--- a/jdk/src/solaris/native/com/sun/security/auth/module/Unix.c	Wed Feb 05 11:05:24 2014 +0100
+++ b/jdk/src/solaris/native/com/sun/security/auth/module/Unix.c	Wed Feb 05 10:20:30 2014 +0000
@@ -60,7 +60,7 @@
     groups = (gid_t *)calloc(numSuppGroups, sizeof(gid_t));
     if (groups == NULL) {
         jclass cls = (*env)->FindClass(env,"java/lang/OutOfMemoryError");
-        if(cls != 0)
+        if (cls != NULL)
             (*env)->ThrowNew(env, cls, NULL);
         return;
     }
@@ -90,6 +90,8 @@
             goto cleanUpAndReturn;
 
         jstr = (*env)->NewStringUTF(env, pwd->pw_name);
+        if (jstr == NULL)
+            goto cleanUpAndReturn;
         (*env)->SetObjectField(env, obj, userNameID, jstr);
 
         (*env)->SetLongField(env, obj, userID, pwd->pw_uid);
@@ -97,7 +99,11 @@
         (*env)->SetLongField(env, obj, groupID, pwd->pw_gid);
 
         jgroups = (*env)->NewLongArray(env, numSuppGroups);
+        if (jgroups == NULL)
+            goto cleanUpAndReturn;
         jgroupsAsArray = (*env)->GetLongArrayElements(env, jgroups, 0);
+        if (jgroupsAsArray == NULL)
+            goto cleanUpAndReturn;
         for (i = 0; i < numSuppGroups; i++)
             jgroupsAsArray[i] = groups[i];
         (*env)->ReleaseLongArrayElements(env, jgroups, jgroupsAsArray, 0);
--- a/jdk/src/windows/native/com/sun/security/auth/module/nt.c	Wed Feb 05 11:05:24 2014 +0100
+++ b/jdk/src/windows/native/com/sun/security/auth/module/nt.c	Wed Feb 05 10:20:30 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);
     }