]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/, src/: Reorder while() conditions for safety
authorAlejandro Colomar <alx@kernel.org>
Fri, 18 Jul 2025 10:23:42 +0000 (12:23 +0200)
committerSerge Hallyn <serge@hallyn.com>
Sat, 9 Aug 2025 22:03:22 +0000 (17:03 -0500)
In conditions that perform simple assignment (=) before comparison,
it's safer to put the comparison first, as a mistake would result in a
compiler error, as opposed to assigning something incorrect.
It's also more readable, IMO.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
19 files changed:
lib/copydir.c
lib/find_new_gid.c
lib/find_new_uid.c
lib/gshadow.c
lib/port.c
lib/prefix_flag.c
lib/subordinateio.c
lib/user_busy.c
lib/utmp.c
src/groupmod.c
src/grpconv.c
src/grpunconv.c
src/logoutd.c
src/newgrp.c
src/pwconv.c
src/pwunconv.c
src/su.c
src/userdel.c
src/usermod.c

index d7addb1d5ca47d871eed1f741189249a9208a53b..736e230464d86971c7dffd9020c4bf63f9565451 100644 (file)
@@ -310,7 +310,7 @@ static int copy_tree_impl (const struct path_info *src, const struct path_info *
                dst_orig = dst->full_path;
                set_orig = true;
        }
-       while ((0 == err) && (ent = readdir (dir)) != NULL) {
+       while (0 == err && NULL != (ent = readdir(dir))) {
                char              *src_name = NULL;
                char              *dst_name;
                struct path_info  src_entry, dst_entry;
index 3d1fc560418727980381862512b7e43ebf88f637..5ee077abde073969f4ef48c9cdda72a97cd8d9b6 100644 (file)
@@ -245,7 +245,7 @@ int find_new_gid (bool sys_group,
        (void) gr_rewind ();
        highest_found = gid_min;
        lowest_found = gid_max;
-       while ((grp = gr_next ()) != NULL) {
+       while (NULL != (grp = gr_next())) {
                /*
                 * Does this entry have a lower GID than the lowest we've found
                 * so far?
index 0229ea849b1c6e6eb864737c6790867acd47f5f6..c9a460b028b32797b2992c44d6034e6bb2617d20 100644 (file)
@@ -245,7 +245,7 @@ int find_new_uid(bool sys_user,
        (void) pw_rewind ();
        highest_found = uid_min;
        lowest_found = uid_max;
-       while ((pwd = pw_next ()) != NULL) {
+       while (NULL != (pwd = pw_next())) {
                /*
                 * Does this entry have a lower UID than the lowest we've found
                 * so far?
index cb62c61a9d2a57bb49f4a6e797793838dc7d3e37..2bde386954d7c676dcf527a84b3dff93da9a40ef 100644 (file)
@@ -166,7 +166,7 @@ sgetsgent(const char *s)
 
        setsgent ();
 
-       while ((sgrp = getsgent ()) != NULL) {
+       while (NULL != (sgrp = getsgent())) {
                if (streq(name, sgrp->sg_namp)) {
                        break;
                }
index 948c277e4deba67aae43b6943dfed7d5a9d6f855..cbb94c3c97b2e214a4111125f15e22adc98ef672 100644 (file)
@@ -310,7 +310,7 @@ getttyuser(const char *tty, const char *user)
 
        setportent();
 
-       while ((port = getportent()) != NULL) {
+       while (NULL != (port = getportent())) {
                char  **ptn;
                char  **ptu;
 
index 0efddd45ae046503e83a01e39a6ae2bc87f263d1..f69aa106065e0ee2c3d3a79a3f067902c2819a1c 100644 (file)
@@ -153,7 +153,7 @@ extern struct group *prefix_getgrnam(const char *name)
                fg = fopen(group_db_file, "rt");
                if (!fg)
                        return NULL;
-               while ((grp = fgetgrent(fg)) != NULL) {
+               while (NULL != (grp = fgetgrent(fg))) {
                        if (streq(name, grp->gr_name))
                                break;
                }
@@ -173,7 +173,7 @@ extern struct group *prefix_getgrgid(gid_t gid)
                fg = fopen(group_db_file, "rt");
                if (!fg)
                        return NULL;
-               while ((grp = fgetgrent(fg)) != NULL) {
+               while (NULL != (grp = fgetgrent(fg))) {
                        if (gid == grp->gr_gid)
                                break;
                }
@@ -193,7 +193,7 @@ extern struct passwd *prefix_getpwuid(uid_t uid)
                fg = fopen(passwd_db_file, "rt");
                if (!fg)
                        return NULL;
-               while ((pwd = fgetpwent(fg)) != NULL) {
+               while (NULL != (pwd = fgetpwent(fg))) {
                        if (uid == pwd->pw_uid)
                                break;
                }
@@ -213,7 +213,7 @@ extern struct passwd *prefix_getpwnam(const char* name)
                fg = fopen(passwd_db_file, "rt");
                if (!fg)
                        return NULL;
-               while ((pwd = fgetpwent(fg)) != NULL) {
+               while (NULL != (pwd = fgetpwent(fg))) {
                        if (streq(name, pwd->pw_name))
                                break;
                }
@@ -256,7 +256,7 @@ extern struct spwd *prefix_getspnam(const char* name)
                fg = fopen(spw_db_file, "rt");
                if (!fg)
                        return NULL;
-               while ((sp = fgetspent(fg)) != NULL) {
+               while (NULL != (sp = fgetspent(fg))) {
                        if (streq(name, sp->sp_namp))
                                break;
                }
index 504977ed180bd344ebb1155d71a09d45cd26ca0f..058a097058d68525e8fe826455e2158c8a2fddd8 100644 (file)
@@ -156,7 +156,7 @@ static bool range_exists(struct commonio_db *db, const char *owner)
 {
        const struct subordinate_range *range;
        commonio_rewind(db);
-       while ((range = commonio_next(db)) != NULL) {
+       while (NULL != (range = commonio_next(db))) {
                if (streq(range->owner, owner))
                        return true;
        }
@@ -188,7 +188,7 @@ static const struct subordinate_range *find_range(struct commonio_db *db,
         * before.
         */
        commonio_rewind(db);
-       while ((range = commonio_next(db)) != NULL) {
+       while (NULL != (range = commonio_next(db))) {
                unsigned long first = range->start;
                unsigned long last = first + range->count - 1;
 
@@ -229,7 +229,7 @@ static const struct subordinate_range *find_range(struct commonio_db *db,
                 return NULL;
 
         commonio_rewind(db);
-        while ((range = commonio_next(db)) != NULL) {
+        while (NULL != (range = commonio_next(db))) {
                 unsigned long first = range->start;
                 unsigned long last = first + range->count - 1;
 
@@ -366,7 +366,7 @@ static unsigned long find_free_range(struct commonio_db *db,
        commonio_rewind(db);
 
        low = min;
-       while ((range = commonio_next(db)) != NULL) {
+       while (NULL != (range = commonio_next(db))) {
                unsigned long first = range->start;
                unsigned long last = first + range->count - 1;
 
@@ -884,7 +884,7 @@ int list_owner_ranges(const char *owner, enum subid_type id_type, struct subid_r
        have_owner_id = get_owner_id(owner, id_type, id);
 
        commonio_rewind(db);
-       while ((range = commonio_next(db)) != NULL) {
+       while (NULL != (range = commonio_next(db))) {
                if (streq(range->owner, owner)) {
                        if (!append_range(&ranges, range, count++)) {
                                free(ranges);
@@ -989,7 +989,7 @@ int find_subid_owners(unsigned long id, enum subid_type id_type, uid_t **uids)
        *uids = NULL;
 
        commonio_rewind(db);
-       while ((range = commonio_next(db)) != NULL) {
+       while (NULL != (range = commonio_next(db))) {
                if (id >= range->start && id < range->start + range-> count) {
                        n = append_uids(uids, range->owner, n);
                        if (n < 0)
@@ -1045,7 +1045,7 @@ bool new_subid_range(struct subordinate_range *range, enum subid_type id_type, b
 
        commonio_rewind(db);
        if (reuse) {
-               while ((r = commonio_next(db)) != NULL) {
+               while (NULL != (r = commonio_next(db))) {
                        // TODO account for username vs uid_t
                        if (!streq(r->owner, range->owner))
                                continue;
index 5f6173729e4e0dc6c84305255f75b310774120ad..630249399c2451713a7494793e66b0748725adbd 100644 (file)
@@ -65,7 +65,7 @@ user_busy_utmp(const char *name)
        struct utmpx  *utent;
 
        setutxent();
-       while ((utent = getutxent()) != NULL)
+       while (NULL != (utent = getutxent()))
        {
                if (utent->ut_type != USER_PROCESS) {
                        continue;
@@ -193,7 +193,7 @@ static int user_busy_processes (const char *name, uid_t uid)
                return 0;
        }
 
-       while ((ent = readdir (proc)) != NULL) {
+       while (NULL != (ent = readdir(proc))) {
                tmp_d_name = ent->d_name;
                /*
                 * Ingo Molnar's patch introducing NPTL for 2.4 hides
@@ -236,7 +236,7 @@ static int user_busy_processes (const char *name, uid_t uid)
                SNPRINTF(task_path, "/proc/%lu/task", (unsigned long) pid);
                task_dir = opendir (task_path);
                if (task_dir != NULL) {
-                       while ((ent = readdir (task_dir)) != NULL) {
+                       while (NULL != (ent = readdir(task_dir))) {
                                pid_t tid;
                                if (get_pid(ent->d_name, &tid) == -1) {
                                        continue;
index 157fc5737eee3e41b0a165236cdc21e8015e8450..7159e0a3bc536052c7ec8d9a5186b996b3efdf4d 100644 (file)
@@ -156,7 +156,7 @@ get_current_utmp(pid_t main_pid)
        setutxent();
 
        /* First, try to find a valid utmp entry for this process.  */
-       while ((ut = getutxent()) != NULL) {
+       while (NULL != (ut = getutxent())) {
                if (   (LOGIN_PROCESS != ut->ut_type)
                    && (USER_PROCESS  != ut->ut_type))
                        continue;
index 2cdb20676c679b2885529d3cd012ea52927367df..5b42135ba323b9c8dfe50b2f7d51f0efa0d3125a 100644 (file)
@@ -735,7 +735,7 @@ void update_primary_groups (gid_t ogid, gid_t ngid)
        struct passwd *pwd;
 
        prefix_setpwent ();
-       while ((pwd = prefix_getpwent ()) != NULL) {
+       while (NULL != (pwd = prefix_getpwent())) {
                if (pwd->pw_gid == ogid) {
                        const struct passwd *lpwd;
                        struct passwd npwd;
index f28109d85065fb8a9dd67e2a0fe8e4e2c1f48698..9b198a4c4931aa9bcf33c3ff91e91228c02d4cf9 100644 (file)
@@ -171,7 +171,7 @@ int main (int argc, char **argv)
         * Remove /etc/gshadow entries for groups not in /etc/group.
         */
        (void) sgr_rewind ();
-       while ((sg = sgr_next ()) != NULL) {
+       while (NULL != (sg = sgr_next())) {
                if (gr_locate (sg->sg_namp) != NULL) {
                        continue;
                }
index afdfe6e36c2687354176c7348a3ce01ac560b6e0..f9b24d8127405d47b6729253f73dcc7bb8b7fda6 100644 (file)
@@ -175,7 +175,7 @@ int main (int argc, char **argv)
         * Update group passwords if non-shadow password is "x".
         */
        (void) gr_rewind ();
-       while ((gr = gr_next ()) != NULL) {
+       while (NULL != (gr = gr_next())) {
                sg = sgr_locate (gr->gr_name);
                if (   (NULL != sg)
                    && streq(gr->gr_passwd, SHADOW_PASSWD_STRING)) {
index 219604fbfad1224d0b9f287648e57ff73d98beae..3efcd8468c815b345ff47ad03957abf052569265 100644 (file)
@@ -171,7 +171,7 @@ main(int argc, char **argv)
                 * for login sessions will be checked to see if the user
                 * is permitted to be signed on at this time.
                 */
-               while ((ut = getutxent()) != NULL) {
+               while (NULL != (ut = getutxent())) {
                        int   tty_fd;
                        char  tty_name[sizeof(ut->ut_line) + 6];  // /dev/ + NUL
 
index 937fd4356a2a99650a6dfbc940854cd0628af0af..0d9cb3395998a8851e303104483efbc6fea5e1ac 100644 (file)
@@ -96,7 +96,7 @@ static /*@null@*/struct group *find_matching_group (const char *name, struct gro
                return gr;
 
        setgrent ();
-       while ((gr = getgrent ()) != NULL) {
+       while (NULL != (gr = getgrent())) {
                if (gr->gr_gid != gid) {
                        continue;
                }
index c58b45f28ed7c739f6305c8a5715152ba3f470e9..69f5634913902182485b6da2333f27831cb3dadc 100644 (file)
@@ -207,7 +207,7 @@ int main (int argc, char **argv)
         * Remove /etc/shadow entries for users not in /etc/passwd.
         */
        (void) spw_rewind ();
-       while ((sp = spw_next ()) != NULL) {
+       while (NULL != (sp = spw_next())) {
                if (pw_locate (sp->sp_namp) != NULL) {
                        continue;
                }
@@ -229,7 +229,7 @@ int main (int argc, char **argv)
         * missing shadow entries.
         */
        (void) pw_rewind ();
-       while ((pw = pw_next ()) != NULL) {
+       while (NULL != (pw = pw_next())) {
                sp = spw_locate (pw->pw_name);
                if (NULL != sp) {
                        /* do we need to update this entry? */
index f83d3517fc76d65faf1c1d32590499f2cc34009a..b0516cbe6a34a7899fb430fdc1d5ca6bc26568d1 100644 (file)
@@ -172,7 +172,7 @@ int main (int argc, char **argv)
        }
 
        (void) pw_rewind ();
-       while ((pw = pw_next ()) != NULL) {
+       while (NULL != (pw = pw_next())) {
                spwd = spw_locate (pw->pw_name);
                if (NULL == spwd) {
                        continue;
index 4051682c2a8a922ab63497e828dd35411cbc990e..2c958a7a6361115edc0e625bcdf1146383d53c99 100644 (file)
--- a/src/su.c
+++ b/src/su.c
@@ -187,7 +187,7 @@ static bool restricted_shell (const char *shellname)
        /*@observer@*/const char *line;
 
        setusershell ();
-       while ((line = getusershell ()) != NULL) {
+       while (NULL != (line = getusershell())) {
                if (('#' != *line) && streq(line, shellname)) {
                        endusershell ();
                        return false;
index 522a108bb7c9df5e0546afa30699f4216124b5b4..2f8d59fa7e560e7571cac6d4634ce48d72481042 100644 (file)
@@ -323,7 +323,7 @@ static void remove_usergroup (void)
                 * used as a primary group.
                 */
                prefix_setpwent ();
-               while ((pwd = prefix_getpwent ()) != NULL) {
+               while (NULL != (pwd = prefix_getpwent())) {
                        if (streq(pwd->pw_name, user_name)) {
                                continue;
                        }
index 29b7426d0e544a734a5df040d89054ffa633f602..02414da9d6ef2e073b5c90ef3070ebd1a55a6c9b 100644 (file)
@@ -696,7 +696,7 @@ update_group_file(void)
         * Scan through the entire group file looking for the groups that
         * the user is a member of.
         */
-       while ((grp = gr_next()) != NULL)
+       while (NULL != (grp = gr_next()))
                update_group(grp);
 }
 
@@ -822,7 +822,7 @@ update_gshadow_file(void)
         * Scan through the entire shadow group file looking for the groups
         * that the user is a member of.
         */
-       while ((sgrp = sgr_next()) != NULL)
+       while (NULL != (sgrp = sgr_next()))
                update_gshadow(sgrp);
 }
 #endif                         /* SHADOWGRP */