}
strcpy (buf, list);
- for (size_t i = 16; /* void */; i *= 2) {
- grouplist = MALLOC(i, GETGROUPS_T);
- if (NULL == grouplist) {
- return -1;
- }
- ngroups = getgroups (i, grouplist);
- if (ngroups == -1 && errno != EINVAL) {
- free(grouplist);
- return -1;
- }
- if (i > (size_t)ngroups) {
- break;
- }
- free (grouplist);
- }
+ ngroups = getgroups(0, NULL);
+ if (ngroups == -1)
+ return -1;
+
+ grouplist = MALLOC(ngroups, GETGROUPS_T);
+ if (grouplist == NULL)
+ return -1;
+
+ ngroups = getgroups(ngroups, grouplist);
+ if (ngroups == -1)
+ goto free_gids;
added = false;
p = buf;
free (grouplist);
return 0;
+
+free_gids:
+ free(grouplist);
+ return -1;
}
#else /* !USE_PAM */
extern int ISO_C_forbids_an_empty_translation_unit;
* nasty message but at least your real and effective group ids are
* set.
*/
- /* don't use getgroups(0, 0) - it doesn't work on some systems */
- for (int i = 16; /* void */; i *= 2) {
- grouplist = XMALLOC(i, GETGROUPS_T);
- ngroups = getgroups (i, grouplist);
- if (ngroups == -1 && errno != EINVAL) {
- perror("getgroups");
+
+ ngroups = getgroups(0, NULL);
+ if (ngroups == -1)
+ goto fail_gg;
+
+ grouplist = XMALLOC(ngroups, GETGROUPS_T);
+
+ ngroups = getgroups(ngroups, grouplist);
+ if (ngroups == -1) {
+fail_gg:
+ perror("getgroups");
#ifdef WITH_AUDIT
- if (group) {
- SNPRINTF(audit_buf, "changing new-group=%s", group);
- audit_logger(AUDIT_CHGRP_ID, Prog,
- audit_buf, NULL, getuid(), 0);
- } else {
- audit_logger(AUDIT_CHGRP_ID, Prog,
- "changing", NULL, getuid(), 0);
- }
-#endif
- exit(EXIT_FAILURE);
- }
- if (i > (size_t)ngroups) {
- break;
+ if (group) {
+ SNPRINTF(audit_buf, "changing new-group=%s", group);
+ audit_logger(AUDIT_CHGRP_ID, Prog,
+ audit_buf, NULL, getuid(), 0);
+ } else {
+ audit_logger(AUDIT_CHGRP_ID, Prog,
+ "changing", NULL, getuid(), 0);
}
- free (grouplist);
+#endif
+ exit(EXIT_FAILURE);
}
/*
* If the group doesn't fit, I'll complain loudly and skip this
* part.
*/
+ grouplist = XREALLOC(grouplist, ngroups + 1, GETGROUPS_T);
+
int i;
for (i = 0; i < ngroups; i++) {
if (gid == grouplist[i]) {