]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
treewide: use err() instead of errx() where ul_get{userpw,grp}_str() fails
authorChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Wed, 8 Apr 2026 05:20:53 +0000 (01:20 -0400)
committerChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Thu, 9 Apr 2026 14:11:09 +0000 (10:11 -0400)
Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
login-utils/chfn.c
login-utils/newgrp.c
login-utils/su-common.c
sys-utils/unshare.c

index 76895887a270e2be785b67130d6bd69777043db2..efc58159e56fda63d9df8d49b3b7dce8f6049974 100644 (file)
@@ -448,9 +448,10 @@ int main(int argc, char **argv)
                        errx(EXIT_FAILURE, _("your user %u does not exist"),
                             uid);
        } else {
+               errno = 0;
                ctl.pw = ul_getuserpw_str(ctl.username, NULL);
                if (!ctl.pw)
-                       errx(EXIT_FAILURE, _("user \"%s\" does not exist"),
+                       err(EXIT_FAILURE, _("user \"%s\" does not exist"),
                             ctl.username);
        }
        ctl.username = ctl.pw->pw_name;
index 17c58e55eb64a9070a5654d55ab3eb4ef532a831..fa352d8140ba0c4f840903c339bf165550838c6a 100644 (file)
@@ -218,20 +218,22 @@ int main(int argc, char *argv[])
                        errtryhelp(EXIT_FAILURE);
                }
 
-       if (!(pw_entry = getpwuid(getuid())))
-               err(EXIT_FAILURE, _("who are you?"));
+       errno = 0;
+       pw_entry = getpwuid(getuid());
+       if (!pw_entry) {
+               if (!errno)
+                       errno = EINVAL;
+               err(EXIT_FAILURE, _("failed to look up current user"));
+       }
 
        if (argc <= optind) {
                if (setgid(pw_entry->pw_gid) < 0)
                        err(EXIT_FAILURE, _("setgid() failed"));
        } else {
                errno = 0;
-               if (!(gr_entry = ul_getgrp_str(argv[optind++], NULL))) {
-                       if (errno)
-                               err(EXIT_FAILURE, _("no such group"));
-                       else
-                               errx(EXIT_FAILURE, _("no such group"));
-               }
+               gr_entry = ul_getgrp_str(argv[optind++], NULL);
+               if (!gr_entry)
+                       err(EXIT_FAILURE, _("no such group"));
                if (!allow_setgid(pw_entry, gr_entry))
                        errx(EXIT_FAILURE, _("permission denied"));
                if (setgid(gr_entry->gr_gid) < 0)
index 57651ea9ee33e4fc74fd8ba1c48e4fc24c7d727d..6f361e65a478da4f41715719d8a80491bd03661f 100644 (file)
@@ -978,9 +978,10 @@ static gid_t add_supp_group(const char *name, gid_t **groups, size_t *ngroups)
                        "specifying more than %d supplemental groups is not possible",
                        NGROUPS_MAX - 1), NGROUPS_MAX - 1);
 
+       errno = 0;
        gr = ul_getgrp_str(name, NULL);
        if (!gr)
-               errx(EXIT_FAILURE, _("group %s does not exist"), name);
+               err(EXIT_FAILURE, _("group %s does not exist"), name);
 
        DBG(MISC, ul_debug("add %s group [name=%s, GID=%d]", name, gr->gr_name, (int) gr->gr_gid));
 
index fd2768064bf620d23ac6933602017a80714e715a..8f0380bf4ddf101d624230380164b3c530835d60 100644 (file)
@@ -327,8 +327,9 @@ static uid_t get_user(const char *s)
 {
        uid_t uid;
 
+       errno = 0;
        if (!ul_getuserpw_str(s, &uid) && uid == (uid_t) -1)
-               errx(EXIT_FAILURE, _("failed to parse uid '%s'"), s);
+               err(EXIT_FAILURE, _("failed to parse uid '%s'"), s);
        return uid;
 }
 
@@ -336,8 +337,9 @@ static gid_t get_group(const char *s)
 {
        gid_t gid;
 
+       errno = 0;
        if (!ul_getgrp_str(s, &gid) && gid == (gid_t) -1)
-               errx(EXIT_FAILURE, _("failed to parse gid '%s'"), s);
+               err(EXIT_FAILURE, _("failed to parse gid '%s'"), s);
        return gid;
 }