]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/, src/: Unconditionally call setgroups(2)
authorAlejandro Colomar <alx@kernel.org>
Thu, 14 Nov 2024 18:05:31 +0000 (19:05 +0100)
committerSerge Hallyn <serge@hallyn.com>
Fri, 24 Jan 2025 13:58:13 +0000 (07:58 -0600)
Call it regardless of having added any groups.  If the group list is the
same that getgroups(3) gave us, setgroups(3) will be a no-op, and it
simplifies the surrounding code, by removing the 'added' variable, and
allowing to call lsearch(3) instead of lfind(3).

Reviewed-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/addgrps.c
src/newgrp.c

index e65b06d2786adca8ab6eba1afc8d9b835afc7b87..c45c5299644aaa28bb4bf68d187e84635a541c40 100644 (file)
@@ -36,7 +36,6 @@ add_groups(const char *list)
 {
        GETGROUPS_T *grouplist;
        int ngroups;
-       bool added;
        char *g, *p;
        char buf[1024];
        FILE *shadow_logfd = log_get_logfd();
@@ -59,7 +58,6 @@ add_groups(const char *list)
        if (ngroups == -1)
                goto free_gids;
 
-       added = false;
        p = buf;
        while (NULL != (g = strsep(&p, ",:"))) {
                struct group  *grp;
@@ -84,13 +82,10 @@ add_groups(const char *list)
 
                grouplist[ngroups] = grp->gr_gid;
                ngroups++;
-               added = true;
        }
 
-       if (added) {
-               if (setgroups(ngroups, grouplist) == -1)
-                       goto free_gids;
-       }
+       if (setgroups(ngroups, grouplist) == -1)
+               goto free_gids;
 
        free (grouplist);
        return 0;
index 7a5fecabec6cc73e35d8a9fd53e6565f577ed8f3..4c5795931da6f618e72d9290c73cde4cb90fbdea 100644 (file)
@@ -687,12 +687,12 @@ fail_gg:
                        (void) fputs (_("too many groups\n"), stderr);
                } else {
                        grouplist[ngroups++] = gid;
-                       if (setgroups (ngroups, grouplist) != 0) {
-                               perror ("setgroups");
-                       }
                }
        }
 
+       if (setgroups(ngroups, grouplist) == -1)
+               perror("setgroups");
+
        /*
         * Close all files before changing the user/group IDs.
         *