comparison src/solaris/native/com/sun/security/auth/module/Unix.c @ 6505:29b58cb8e4fc

8000204: Memory leak in com/sun/security/auth/module/Unix.c Reviewed-by: dsamersoff, wetmore, khazra, chegar Contributed-by: John Zavgren <john.zavgren@oracle.com>
author chegar
date Tue, 23 Oct 2012 11:57:47 +0100
parents 00cd9dc3c2b5
children cea72c2bf071
comparison
equal deleted inserted replaced
1:7abee7e82b38 2:356733b6cd99
45 struct passwd *pwd; 45 struct passwd *pwd;
46 struct passwd resbuf; 46 struct passwd resbuf;
47 jsize numSuppGroups = getgroups(0, NULL); 47 jsize numSuppGroups = getgroups(0, NULL);
48 gid_t *groups = (gid_t *)calloc(numSuppGroups, sizeof(gid_t)); 48 gid_t *groups = (gid_t *)calloc(numSuppGroups, sizeof(gid_t));
49 49
50 jfieldID fid; 50 jfieldID userNameID;
51 jfieldID userID;
52 jfieldID groupID;
53 jfieldID supplementaryGroupID;
54
51 jstring jstr; 55 jstring jstr;
52 jlongArray jgroups; 56 jlongArray jgroups;
53 jlong *jgroupsAsArray; 57 jlong *jgroupsAsArray;
54 jclass cls = (*env)->GetObjectClass(env, obj); 58 jclass cls = (*env)->GetObjectClass(env, obj);
55 59
56 memset(pwd_buf, 0, sizeof(pwd_buf)); 60 memset(pwd_buf, 0, sizeof(pwd_buf));
61
57 if (getpwuid_r(getuid(), &resbuf, pwd_buf, sizeof(pwd_buf), &pwd) == 0 && 62 if (getpwuid_r(getuid(), &resbuf, pwd_buf, sizeof(pwd_buf), &pwd) == 0 &&
58 pwd != NULL && 63 pwd != NULL &&
59 getgroups(numSuppGroups, groups) != -1) { 64 getgroups(numSuppGroups, groups) != -1) {
60 65
61 /* 66 userNameID = (*env)->GetFieldID(env, cls, "username", "Ljava/lang/String;");
62 * set username 67 if (userNameID == 0)
63 */ 68 goto cleanUpAndReturn;
64 fid = (*env)->GetFieldID(env, cls, "username", "Ljava/lang/String;"); 69
65 if (fid == 0) { 70 userID = (*env)->GetFieldID(env, cls, "uid", "J");
66 jclass newExcCls = 71 if (userID == 0)
67 (*env)->FindClass(env, "java/lang/IllegalArgumentException"); 72 goto cleanUpAndReturn;
68 if (newExcCls == 0) { 73
69 /* Unable to find the new exception class, give up. */ 74 groupID = (*env)->GetFieldID(env, cls, "gid", "J");
70 return; 75 if (groupID == 0)
71 } 76 goto cleanUpAndReturn;
72 (*env)->ThrowNew(env, newExcCls, "invalid field: username"); 77
73 } 78 supplementaryGroupID = (*env)->GetFieldID(env, cls, "groups", "[J");
79 if (supplementaryGroupID == 0)
80 goto cleanUpAndReturn;
81
74 jstr = (*env)->NewStringUTF(env, pwd->pw_name); 82 jstr = (*env)->NewStringUTF(env, pwd->pw_name);
75 (*env)->SetObjectField(env, obj, fid, jstr); 83 (*env)->SetObjectField(env, obj, userNameID, jstr);
76 84
77 /* 85 (*env)->SetLongField(env, obj, userID, pwd->pw_uid);
78 * set uid
79 */
80 fid = (*env)->GetFieldID(env, cls, "uid", "J");
81 if (fid == 0) {
82 jclass newExcCls =
83 (*env)->FindClass(env, "java/lang/IllegalArgumentException");
84 if (newExcCls == 0) {
85 /* Unable to find the new exception class, give up. */
86 return;
87 }
88 (*env)->ThrowNew(env, newExcCls, "invalid field: username");
89 }
90 (*env)->SetLongField(env, obj, fid, pwd->pw_uid);
91 86
92 /* 87 (*env)->SetLongField(env, obj, groupID, pwd->pw_gid);
93 * set gid
94 */
95 fid = (*env)->GetFieldID(env, cls, "gid", "J");
96 if (fid == 0) {
97 jclass newExcCls =
98 (*env)->FindClass(env, "java/lang/IllegalArgumentException");
99 if (newExcCls == 0) {
100 /* Unable to find the new exception class, give up. */
101 return;
102 }
103 (*env)->ThrowNew(env, newExcCls, "invalid field: username");
104 }
105 (*env)->SetLongField(env, obj, fid, pwd->pw_gid);
106
107 /*
108 * set supplementary groups
109 */
110 fid = (*env)->GetFieldID(env, cls, "groups", "[J");
111 if (fid == 0) {
112 jclass newExcCls =
113 (*env)->FindClass(env, "java/lang/IllegalArgumentException");
114 if (newExcCls == 0) {
115 /* Unable to find the new exception class, give up. */
116 return;
117 }
118 (*env)->ThrowNew(env, newExcCls, "invalid field: username");
119 }
120 88
121 jgroups = (*env)->NewLongArray(env, numSuppGroups); 89 jgroups = (*env)->NewLongArray(env, numSuppGroups);
122 jgroupsAsArray = (*env)->GetLongArrayElements(env, jgroups, 0); 90 jgroupsAsArray = (*env)->GetLongArrayElements(env, jgroups, 0);
123 for (i = 0; i < numSuppGroups; i++) 91 for (i = 0; i < numSuppGroups; i++)
124 jgroupsAsArray[i] = groups[i]; 92 jgroupsAsArray[i] = groups[i];
125 (*env)->ReleaseLongArrayElements(env, jgroups, jgroupsAsArray, 0); 93 (*env)->ReleaseLongArrayElements(env, jgroups, jgroupsAsArray, 0);
126 (*env)->SetObjectField(env, obj, fid, jgroups); 94 (*env)->SetObjectField(env, obj, supplementaryGroupID, jgroups);
127 } 95 }
96 cleanUpAndReturn:
97 free(groups);
128 return; 98 return;
129 } 99 }