Skip to content

Commit

Permalink
lib/, src/: Turn error counters into flags
Browse files Browse the repository at this point in the history
If we are not interested in the amount of errors but only if errors
exist, use a flag instead of a counter. This eliminates the chance of
signed integer overflows and better reflects the meaning of variable.

Keeping variable name and basically copied from src/faillog.c.

Reviewed-by: Alejandro Colomar <[email protected]>
Signed-off-by: Tobias Stoeckmann <[email protected]>
  • Loading branch information
stoeckmann authored and alejandro-colomar committed Jan 22, 2025
1 parent 6ef84cc commit 3e4b3d3
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 94 deletions.
28 changes: 14 additions & 14 deletions lib/commonio.c
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ static int write_all (const struct commonio_db *db)

int commonio_close (struct commonio_db *db)
{
int errors = 0;
bool errors = false;
char buf[1024];
struct stat sb;

Expand Down Expand Up @@ -932,25 +932,25 @@ int commonio_close (struct commonio_db *db)

#ifdef WITH_SELINUX
if (set_selinux_file_context (db->filename, S_IFREG) != 0) {
errors++;
errors = true;
}
#endif
if (create_backup (buf, db->fp) != 0) {
errors++;
errors = true;
}

if (fclose (db->fp) != 0) {
errors++;
errors = true;
}

db->fp = NULL;

#ifdef WITH_SELINUX
if (reset_selinux_file_context () != 0) {
errors++;
errors = true;
}
#endif
if (errors != 0)
if (errors)
goto fail;
} else {
/*
Expand All @@ -966,7 +966,7 @@ int commonio_close (struct commonio_db *db)

#ifdef WITH_SELINUX
if (set_selinux_file_context (db->filename, S_IFREG) != 0) {
errors++;
errors = true;
}
#endif

Expand All @@ -976,24 +976,24 @@ int commonio_close (struct commonio_db *db)
}

if (write_all (db) != 0) {
errors++;
errors = true;
}

if (fflush (db->fp) != 0) {
errors++;
errors = true;
}

if (fsync (fileno (db->fp)) != 0) {
errors++;
errors = true;
}

if (fclose (db->fp) != 0) {
errors++;
errors = true;
}

db->fp = NULL;

if (errors != 0) {
if (errors) {
unlink (buf);
goto fail;
}
Expand All @@ -1011,11 +1011,11 @@ int commonio_close (struct commonio_db *db)
nscd_need_reload = true;
goto success;
fail:
errors++;
errors = true;
success:

free_linked_list (db);
return errors == 0;
return !errors;
}

static /*@dependent@*/ /*@null@*/struct commonio_entry *next_entry_by_name (
Expand Down
14 changes: 7 additions & 7 deletions src/chgpasswd.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ int main (int argc, char **argv)

const struct group *gr;
struct group newgr;
int errors = 0;
bool errors = false;
intmax_t line = 0;

log_set_progname(Prog);
Expand Down Expand Up @@ -466,7 +466,7 @@ int main (int argc, char **argv)
if (stpsep(buf, "\n") == NULL) {
fprintf (stderr, _("%s: line %jd: line too long\n"),
Prog, line);
errors++;
errors = true;
continue;
}

Expand All @@ -485,7 +485,7 @@ int main (int argc, char **argv)
fprintf (stderr,
_("%s: line %jd: missing new password\n"),
Prog, line);
errors++;
errors = true;
continue;
}
newpwd = cp;
Expand Down Expand Up @@ -536,7 +536,7 @@ int main (int argc, char **argv)
fprintf (stderr,
_("%s: line %jd: group '%s' does not exist\n"), Prog,
line, name);
errors++;
errors = true;
continue;
}
#ifdef SHADOWGRP
Expand Down Expand Up @@ -596,7 +596,7 @@ int main (int argc, char **argv)
fprintf (stderr,
_("%s: line %jd: failed to prepare the new %s entry '%s'\n"),
Prog, line, sgr_dbname (), newsg.sg_name);
errors++;
errors = true;
continue;
}
}
Expand All @@ -608,7 +608,7 @@ int main (int argc, char **argv)
fprintf (stderr,
_("%s: line %jd: failed to prepare the new %s entry '%s'\n"),
Prog, line, gr_dbname (), newgr.gr_name);
errors++;
errors = true;
continue;
}
}
Expand All @@ -621,7 +621,7 @@ int main (int argc, char **argv)
* changes to be written out all at once, and then unlocked
* afterwards.
*/
if (0 != errors) {
if (errors) {
fprintf (stderr,
_("%s: error detected, changes ignored\n"), Prog);
fail_exit (1);
Expand Down
16 changes: 8 additions & 8 deletions src/chpasswd.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ int main (int argc, char **argv)
bool use_pam = true;
#endif /* USE_PAM */

int errors = 0;
bool errors = false;
intmax_t line = 0;

log_set_progname(Prog);
Expand Down Expand Up @@ -517,7 +517,7 @@ int main (int argc, char **argv)
fprintf (stderr,
_("%s: line %jd: line too long\n"),
Prog, line);
errors++;
errors = true;
continue;
}
}
Expand All @@ -537,7 +537,7 @@ int main (int argc, char **argv)
fprintf (stderr,
_("%s: line %jd: missing new password\n"),
Prog, line);
errors++;
errors = true;
continue;
}
newpwd = cp;
Expand All @@ -548,7 +548,7 @@ int main (int argc, char **argv)
fprintf (stderr,
_("%s: (line %jd, user %s) password not changed\n"),
Prog, line, name);
errors++;
errors = true;
}
} else
#endif /* USE_PAM */
Expand Down Expand Up @@ -577,7 +577,7 @@ int main (int argc, char **argv)
fprintf (stderr,
_("%s: line %jd: user '%s' does not exist\n"), Prog,
line, name);
errors++;
errors = true;
continue;
}
if (is_shadow_pwd) {
Expand Down Expand Up @@ -643,7 +643,7 @@ int main (int argc, char **argv)
fprintf (stderr,
_("%s: line %jd: failed to prepare the new %s entry '%s'\n"),
Prog, line, spw_dbname (), newsp.sp_namp);
errors++;
errors = true;
continue;
}
}
Expand All @@ -653,7 +653,7 @@ int main (int argc, char **argv)
fprintf (stderr,
_("%s: line %jd: failed to prepare the new %s entry '%s'\n"),
Prog, line, pw_dbname (), newpw.pw_name);
errors++;
errors = true;
continue;
}
}
Expand All @@ -670,7 +670,7 @@ int main (int argc, char **argv)
* With PAM, it is not possible to delay the update of the
* password database.
*/
if (0 != errors) {
if (errors) {
#ifdef USE_PAM
if (!use_pam)
#endif /* USE_PAM */
Expand Down
40 changes: 20 additions & 20 deletions src/grpck.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ static int check_members (const char *groupname,
const char *fmt_info,
const char *fmt_prompt,
const char *fmt_syslog,
int *errors);
static void check_grp_file (int *errors, bool *changed);
bool *errors);
static void check_grp_file (bool *errors, bool *changed);
#ifdef SHADOWGRP
static void compare_members_lists (const char *groupname,
char **members,
char **other_members,
const char *file,
const char *other_file);
static void check_sgr_file (int *errors, bool *changed);
static void check_sgr_file (bool *errors, bool *changed);
#endif

/*
Expand Down Expand Up @@ -360,7 +360,7 @@ static void close_files (bool changed)
/*
* check_members - check that every members of a group exist
*
* If an error is detected, *errors is incremented.
* If an error is detected, *errors is set to true.
*
* The user will be prompted for the removal of the non-existent
* user.
Expand All @@ -381,7 +381,7 @@ static int check_members (const char *groupname,
const char *fmt_info,
const char *fmt_prompt,
const char *fmt_syslog,
int *errors)
bool *errors)
{
int i;
int members_changed = 0;
Expand All @@ -398,7 +398,7 @@ static int check_members (const char *groupname,
* Can't find this user. Remove them
* from the list.
*/
*errors += 1;
*errors = true;
printf (fmt_info, groupname, members[i]);
printf (fmt_prompt, members[i]);

Expand Down Expand Up @@ -454,7 +454,7 @@ static void compare_members_lists (const char *groupname,
/*
* check_grp_file - check the content of the group file
*/
static void check_grp_file (int *errors, bool *changed)
static void check_grp_file (bool *errors, bool *changed)
{
struct commonio_entry *gre, *tgre;
struct group *grp;
Expand Down Expand Up @@ -487,7 +487,7 @@ static void check_grp_file (int *errors, bool *changed)
*/
(void) puts (_("invalid group file entry"));
printf (_("delete line '%s'? "), gre->line);
*errors += 1;
*errors = true;

/*
* prompt the user to delete the entry or not
Expand Down Expand Up @@ -547,7 +547,7 @@ static void check_grp_file (int *errors, bool *changed)
*/
(void) puts (_("duplicate group entry"));
printf (_("delete line '%s'? "), gre->line);
*errors += 1;
*errors = true;

/*
* prompt the user to delete the entry or not
Expand All @@ -561,7 +561,7 @@ static void check_grp_file (int *errors, bool *changed)
* Check for invalid group names. --marekm
*/
if (!is_valid_group_name (grp->gr_name)) {
*errors += 1;
*errors = true;
printf (_("invalid group name '%s'\n"), grp->gr_name);
}

Expand All @@ -570,7 +570,7 @@ static void check_grp_file (int *errors, bool *changed)
*/
if (grp->gr_gid == (gid_t)-1) {
printf (_("invalid group ID '%lu'\n"), (long unsigned int)grp->gr_gid);
*errors += 1;
*errors = true;
}

/*
Expand Down Expand Up @@ -607,7 +607,7 @@ static void check_grp_file (int *errors, bool *changed)
sgr_file);
printf (_("add group '%s' in %s? "),
grp->gr_name, sgr_file);
*errors += 1;
*errors = true;
if (yes_or_no (read_only)) {
struct sgrp sg;
struct group gr;
Expand Down Expand Up @@ -653,7 +653,7 @@ static void check_grp_file (int *errors, bool *changed)
if (!streq(grp->gr_passwd, SHADOW_PASSWD_STRING)) {
printf (_("group %s has an entry in %s, but its password field in %s is not set to 'x'\n"),
grp->gr_name, sgr_file, grp_file);
*errors += 1;
*errors = true;
}
}
}
Expand All @@ -666,7 +666,7 @@ static void check_grp_file (int *errors, bool *changed)
/*
* check_sgr_file - check the content of the shadowed group file (gshadow)
*/
static void check_sgr_file (int *errors, bool *changed)
static void check_sgr_file (bool *errors, bool *changed)
{
const struct group *grp;
struct commonio_entry *sge, *tsge;
Expand All @@ -690,7 +690,7 @@ static void check_sgr_file (int *errors, bool *changed)
*/
(void) puts (_("invalid shadow group file entry"));
printf (_("delete line '%s'? "), sge->line);
*errors += 1;
*errors = true;

/*
* prompt the user to delete the entry or not
Expand Down Expand Up @@ -750,7 +750,7 @@ static void check_sgr_file (int *errors, bool *changed)
*/
(void) puts (_("duplicate shadow group entry"));
printf (_("delete line '%s'? "), sge->line);
*errors += 1;
*errors = true;

/*
* prompt the user to delete the entry or not
Expand All @@ -768,7 +768,7 @@ static void check_sgr_file (int *errors, bool *changed)
printf (_("no matching group file entry in %s\n"),
grp_file);
printf (_("delete line '%s'? "), sge->line);
*errors += 1;
*errors = true;
if (yes_or_no (read_only)) {
goto delete_sg;
}
Expand Down Expand Up @@ -816,7 +816,7 @@ static void check_sgr_file (int *errors, bool *changed)
*/
int main (int argc, char **argv)
{
int errors = 0;
bool errors = false;
bool changed = false;

log_set_progname(Prog);
Expand Down Expand Up @@ -863,14 +863,14 @@ int main (int argc, char **argv)
/*
* Tell the user what we did and exit.
*/
if (0 != errors) {
if (errors) {
if (changed) {
printf (_("%s: the files have been updated\n"), Prog);
} else {
printf (_("%s: no changes\n"), Prog);
}
}

return ((0 != errors) ? E_BAD_ENTRY : E_OKAY);
return (errors ? E_BAD_ENTRY : E_OKAY);
}

Loading

0 comments on commit 3e4b3d3

Please sign in to comment.