Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 682e6ae

Browse files
lib/, src/: Reorder while() conditions for safety
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 <[email protected]>
1 parent 90042ad commit 682e6ae

19 files changed

Lines changed: 33 additions & 33 deletions

lib/copydir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ static int copy_tree_impl (const struct path_info *src, const struct path_info *
310310
dst_orig = dst->full_path;
311311
set_orig = true;
312312
}
313-
while ((0 == err) && (ent = readdir (dir)) != NULL) {
313+
while (0 == err && NULL != (ent = readdir(dir))) {
314314
char *src_name = NULL;
315315
char *dst_name;
316316
struct path_info src_entry, dst_entry;

lib/find_new_gid.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ int find_new_gid (bool sys_group,
245245
(void) gr_rewind ();
246246
highest_found = gid_min;
247247
lowest_found = gid_max;
248-
while ((grp = gr_next ()) != NULL) {
248+
while (NULL != (grp = gr_next())) {
249249
/*
250250
* Does this entry have a lower GID than the lowest we've found
251251
* so far?

lib/find_new_uid.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ int find_new_uid(bool sys_user,
245245
(void) pw_rewind ();
246246
highest_found = uid_min;
247247
lowest_found = uid_max;
248-
while ((pwd = pw_next ()) != NULL) {
248+
while (NULL != (pwd = pw_next())) {
249249
/*
250250
* Does this entry have a lower UID than the lowest we've found
251251
* so far?

lib/gshadow.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ sgetsgent(const char *s)
166166

167167
setsgent ();
168168

169-
while ((sgrp = getsgent ()) != NULL) {
169+
while (NULL != (sgrp = getsgent())) {
170170
if (streq(name, sgrp->sg_namp)) {
171171
break;
172172
}

lib/port.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ getttyuser(const char *tty, const char *user)
310310

311311
setportent();
312312

313-
while ((port = getportent()) != NULL) {
313+
while (NULL != (port = getportent())) {
314314
char **ptn;
315315
char **ptu;
316316

lib/prefix_flag.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ extern struct group *prefix_getgrnam(const char *name)
153153
fg = fopen(group_db_file, "rt");
154154
if (!fg)
155155
return NULL;
156-
while ((grp = fgetgrent(fg)) != NULL) {
156+
while (NULL != (grp = fgetgrent(fg))) {
157157
if (streq(name, grp->gr_name))
158158
break;
159159
}
@@ -173,7 +173,7 @@ extern struct group *prefix_getgrgid(gid_t gid)
173173
fg = fopen(group_db_file, "rt");
174174
if (!fg)
175175
return NULL;
176-
while ((grp = fgetgrent(fg)) != NULL) {
176+
while (NULL != (grp = fgetgrent(fg))) {
177177
if (gid == grp->gr_gid)
178178
break;
179179
}
@@ -193,7 +193,7 @@ extern struct passwd *prefix_getpwuid(uid_t uid)
193193
fg = fopen(passwd_db_file, "rt");
194194
if (!fg)
195195
return NULL;
196-
while ((pwd = fgetpwent(fg)) != NULL) {
196+
while (NULL != (pwd = fgetpwent(fg))) {
197197
if (uid == pwd->pw_uid)
198198
break;
199199
}
@@ -213,7 +213,7 @@ extern struct passwd *prefix_getpwnam(const char* name)
213213
fg = fopen(passwd_db_file, "rt");
214214
if (!fg)
215215
return NULL;
216-
while ((pwd = fgetpwent(fg)) != NULL) {
216+
while (NULL != (pwd = fgetpwent(fg))) {
217217
if (streq(name, pwd->pw_name))
218218
break;
219219
}
@@ -256,7 +256,7 @@ extern struct spwd *prefix_getspnam(const char* name)
256256
fg = fopen(spw_db_file, "rt");
257257
if (!fg)
258258
return NULL;
259-
while ((sp = fgetspent(fg)) != NULL) {
259+
while (NULL != (sp = fgetspent(fg))) {
260260
if (streq(name, sp->sp_namp))
261261
break;
262262
}

lib/subordinateio.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ static bool range_exists(struct commonio_db *db, const char *owner)
156156
{
157157
const struct subordinate_range *range;
158158
commonio_rewind(db);
159-
while ((range = commonio_next(db)) != NULL) {
159+
while (NULL != (range = commonio_next(db))) {
160160
if (streq(range->owner, owner))
161161
return true;
162162
}
@@ -188,7 +188,7 @@ static const struct subordinate_range *find_range(struct commonio_db *db,
188188
* before.
189189
*/
190190
commonio_rewind(db);
191-
while ((range = commonio_next(db)) != NULL) {
191+
while (NULL != (range = commonio_next(db))) {
192192
unsigned long first = range->start;
193193
unsigned long last = first + range->count - 1;
194194

@@ -229,7 +229,7 @@ static const struct subordinate_range *find_range(struct commonio_db *db,
229229
return NULL;
230230

231231
commonio_rewind(db);
232-
while ((range = commonio_next(db)) != NULL) {
232+
while (NULL != (range = commonio_next(db))) {
233233
unsigned long first = range->start;
234234
unsigned long last = first + range->count - 1;
235235

@@ -366,7 +366,7 @@ static unsigned long find_free_range(struct commonio_db *db,
366366
commonio_rewind(db);
367367

368368
low = min;
369-
while ((range = commonio_next(db)) != NULL) {
369+
while (NULL != (range = commonio_next(db))) {
370370
unsigned long first = range->start;
371371
unsigned long last = first + range->count - 1;
372372

@@ -884,7 +884,7 @@ int list_owner_ranges(const char *owner, enum subid_type id_type, struct subid_r
884884
have_owner_id = get_owner_id(owner, id_type, id);
885885

886886
commonio_rewind(db);
887-
while ((range = commonio_next(db)) != NULL) {
887+
while (NULL != (range = commonio_next(db))) {
888888
if (streq(range->owner, owner)) {
889889
if (!append_range(&ranges, range, count++)) {
890890
free(ranges);
@@ -989,7 +989,7 @@ int find_subid_owners(unsigned long id, enum subid_type id_type, uid_t **uids)
989989
*uids = NULL;
990990

991991
commonio_rewind(db);
992-
while ((range = commonio_next(db)) != NULL) {
992+
while (NULL != (range = commonio_next(db))) {
993993
if (id >= range->start && id < range->start + range-> count) {
994994
n = append_uids(uids, range->owner, n);
995995
if (n < 0)
@@ -1045,7 +1045,7 @@ bool new_subid_range(struct subordinate_range *range, enum subid_type id_type, b
10451045

10461046
commonio_rewind(db);
10471047
if (reuse) {
1048-
while ((r = commonio_next(db)) != NULL) {
1048+
while (NULL != (r = commonio_next(db))) {
10491049
// TODO account for username vs uid_t
10501050
if (!streq(r->owner, range->owner))
10511051
continue;

lib/user_busy.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ user_busy_utmp(const char *name)
6565
struct utmpx *utent;
6666

6767
setutxent();
68-
while ((utent = getutxent()) != NULL)
68+
while (NULL != (utent = getutxent()))
6969
{
7070
if (utent->ut_type != USER_PROCESS) {
7171
continue;
@@ -193,7 +193,7 @@ static int user_busy_processes (const char *name, uid_t uid)
193193
return 0;
194194
}
195195

196-
while ((ent = readdir (proc)) != NULL) {
196+
while (NULL != (ent = readdir(proc))) {
197197
tmp_d_name = ent->d_name;
198198
/*
199199
* 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)
236236
SNPRINTF(task_path, "/proc/%lu/task", (unsigned long) pid);
237237
task_dir = opendir (task_path);
238238
if (task_dir != NULL) {
239-
while ((ent = readdir (task_dir)) != NULL) {
239+
while (NULL != (ent = readdir(task_dir))) {
240240
pid_t tid;
241241
if (get_pid(ent->d_name, &tid) == -1) {
242242
continue;

lib/utmp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ get_current_utmp(pid_t main_pid)
156156
setutxent();
157157

158158
/* First, try to find a valid utmp entry for this process. */
159-
while ((ut = getutxent()) != NULL) {
159+
while (NULL != (ut = getutxent())) {
160160
if ( (LOGIN_PROCESS != ut->ut_type)
161161
&& (USER_PROCESS != ut->ut_type))
162162
continue;

src/groupmod.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ void update_primary_groups (gid_t ogid, gid_t ngid)
734734
struct passwd *pwd;
735735

736736
prefix_setpwent ();
737-
while ((pwd = prefix_getpwent ()) != NULL) {
737+
while (NULL != (pwd = prefix_getpwent())) {
738738
if (pwd->pw_gid == ogid) {
739739
const struct passwd *lpwd;
740740
struct passwd npwd;

0 commit comments

Comments
 (0)