Skip to content

Commit

Permalink
lib/, src/: Use !streq() instead of its pattern
Browse files Browse the repository at this point in the history
Except for the added (and sorted) includes, and the removal of redundant
parentheses, and one special case, this patch can be approximated with
the following semantic patch:

	$ cat ~/tmp/spatch/strneq.sp;
	@@
	expression a, b;
	@@

	- strcmp(a, b) != 0
	+ !streq(a, b)

	@@
	expression a, b;
	@@

	- 0 != strcmp(a, b)
	+ !streq(a, b)

	$ find contrib/ lib* src/ -type f \
	| xargs spatch --sp-file ~/tmp/spatch/strneq.sp --in-place;

Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar authored and hallyn committed Dec 2, 2024
1 parent 5581e74 commit 9f12914
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 57 deletions.
4 changes: 2 additions & 2 deletions lib/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ del_list(/*@returned@*/ /*@only@*/char **list, const char *member)
*/

for (i = j = 0; list[i] != NULL; i++) {
if (strcmp (list[i], member) != 0) {
if (!streq(list[i], member)) {
j++;
}
}
Expand All @@ -116,7 +116,7 @@ del_list(/*@returned@*/ /*@only@*/char **list, const char *member)
*/

for (i = j = 0; list[i] != NULL; i++) {
if (strcmp (list[i], member) != 0) {
if (!streq(list[i], member)) {
tmp[j] = list[i];
j++;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/salt.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ static /*@observer@*/const char *gensalt (size_t salt_size)
rounds = SHA_get_salt_rounds (arg);
SHA_salt_rounds_to_buf (result, rounds);
#endif /* USE_SHA_CRYPT */
} else if (0 != strcmp (method, "DES")) {
} else if (!streq(method, "DES")) {
fprintf (log_get_logfd(),
_("Invalid ENCRYPT_METHOD value: '%s'.\n"
"Defaulting to DES.\n"),
Expand Down
8 changes: 4 additions & 4 deletions lib/subordinateio.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ static const struct subordinate_range *find_range(struct commonio_db *db,
unsigned long first = range->start;
unsigned long last = first + range->count - 1;

if (0 != strcmp(range->owner, owner))
if (!streq(range->owner, owner))
continue;

if ((val >= first) && (val <= last))
Expand All @@ -208,7 +208,7 @@ static const struct subordinate_range *find_range(struct commonio_db *db,
/*
* We only do special handling for these two files
*/
if ((0 != strcmp(db->filename, SUBUID_FILE)) && (0 != strcmp(db->filename, SUBGID_FILE)))
if (!streq(db->filename, SUBUID_FILE) && !streq(db->filename, SUBGID_FILE))
return NULL;

/*
Expand Down Expand Up @@ -465,7 +465,7 @@ static int remove_range (struct commonio_db *db,
last = first + range->count - 1;

/* Skip entries with a different owner */
if (0 != strcmp (range->owner, owner)) {
if (!streq(range->owner, owner)) {
continue;
}

Expand Down Expand Up @@ -1062,7 +1062,7 @@ bool new_subid_range(struct subordinate_range *range, enum subid_type id_type, b
if (reuse) {
while ((r = commonio_next(db)) != NULL) {
// TODO account for username vs uid_t
if (0 != strcmp(r->owner, range->owner))
if (!streq(r->owner, range->owner))
continue;
if (r->count >= range->count) {
range->count = r->count;
Expand Down
2 changes: 1 addition & 1 deletion lib/tcbfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ static shadowtcb_status move_dir (const char *user_newname, uid_t user_newid)
if (NULL == real_new_dir_rel) {
goto out_free;
}
if ( (strcmp (real_new_dir, newdir) != 0)
if ( !streq(real_new_dir, newdir)
&& (symlink (real_new_dir_rel, newdir) != 0)) {
fprintf (shadow_logfd,
_("%s: Cannot create symbolic link %s: %s\n"),
Expand Down
20 changes: 10 additions & 10 deletions src/chgpasswd.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,18 +260,18 @@ static void check_flags (void)
}

if (cflg) {
if ( (0 != strcmp (crypt_method, "DES"))
&& (0 != strcmp (crypt_method, "MD5"))
&& (0 != strcmp (crypt_method, "NONE"))
if ( !streq(crypt_method, "DES")
&& !streq(crypt_method, "MD5")
&& !streq(crypt_method, "NONE")
#ifdef USE_SHA_CRYPT
&& (0 != strcmp (crypt_method, "SHA256"))
&& (0 != strcmp (crypt_method, "SHA512"))
&& !streq(crypt_method, "SHA256")
&& !streq(crypt_method, "SHA512")
#endif /* USE_SHA_CRYPT */
#ifdef USE_BCRYPT
&& (0 != strcmp (crypt_method, "BCRYPT"))
&& !streq(crypt_method, "BCRYPT")
#endif /* USE_BCRYPT */
#ifdef USE_YESCRYPT
&& (0 != strcmp (crypt_method, "YESCRYPT"))
&& !streq(crypt_method, "YESCRYPT")
#endif /* USE_YESCRYPT */
) {
fprintf (stderr,
Expand Down Expand Up @@ -490,7 +490,7 @@ int main (int argc, char **argv)
newpwd = cp;
if ( (!eflg)
&& ( (NULL == crypt_method)
|| (0 != strcmp (crypt_method, "NONE")))) {
|| !streq(crypt_method, "NONE"))) {
void *arg = NULL;
const char *salt;
if (md5flg) {
Expand Down Expand Up @@ -577,7 +577,7 @@ int main (int argc, char **argv)
newsg.sg_passwd = cp;
}
if ( (NULL == sg)
|| (strcmp (gr->gr_passwd, SHADOW_PASSWD_STRING) != 0))
|| !streq(gr->gr_passwd, SHADOW_PASSWD_STRING))
#endif
{
newgr = *gr;
Expand All @@ -600,7 +600,7 @@ int main (int argc, char **argv)
}
}
if ( (NULL == sg)
|| (strcmp (gr->gr_passwd, SHADOW_PASSWD_STRING) != 0))
|| !streq(gr->gr_passwd, SHADOW_PASSWD_STRING))
#endif
{
if (gr_update (&newgr) == 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/chpasswd.c
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ int main (int argc, char **argv)
}

if ( (NULL == sp)
|| (strcmp (pw->pw_passwd, SHADOW_PASSWD_STRING) != 0)) {
|| !streq(pw->pw_passwd, SHADOW_PASSWD_STRING)) {
newpw = *pw;
newpw.pw_passwd = cp;
}
Expand All @@ -647,7 +647,7 @@ int main (int argc, char **argv)
}
}
if ( (NULL == sp)
|| (strcmp (pw->pw_passwd, SHADOW_PASSWD_STRING) != 0)) {
|| !streq(pw->pw_passwd, SHADOW_PASSWD_STRING)) {
if (pw_update (&newpw) == 0) {
fprintf (stderr,
_("%s: line %d: failed to prepare the new %s entry '%s'\n"),
Expand Down
2 changes: 1 addition & 1 deletion src/groupmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ static void new_grent (struct group *grent)
if ( pflg
#ifdef SHADOWGRP
&& ( (!is_shadow_grp)
|| (strcmp (grent->gr_passwd, SHADOW_PASSWD_STRING) != 0))
|| !streq(grent->gr_passwd, SHADOW_PASSWD_STRING))
#endif
) {
/* Update the password in group if there is no gshadow
Expand Down
6 changes: 3 additions & 3 deletions src/grpck.c
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ static void check_grp_file (int *errors, bool *changed)
continue;
}

if (strcmp (grp->gr_name, ent->gr_name) != 0) {
if (!streq(grp->gr_name, ent->gr_name)) {
continue;
}

Expand Down Expand Up @@ -649,7 +649,7 @@ static void check_grp_file (int *errors, bool *changed)
/* The group entry has a gshadow counterpart.
* Make sure no passwords are in group.
*/
if (strcmp (grp->gr_passwd, SHADOW_PASSWD_STRING) != 0) {
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;
Expand Down Expand Up @@ -739,7 +739,7 @@ static void check_sgr_file (int *errors, bool *changed)
continue;
}

if (strcmp (sgr->sg_name, ent->sg_name) != 0) {
if (!streq(sgr->sg_name, ent->sg_name)) {
continue;
}

Expand Down
14 changes: 9 additions & 5 deletions src/grpconv.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,29 @@

#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <grp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <unistd.h>
#include <getopt.h>

#include "attr.h"
#include "nscd.h"
#include "sssd.h"
#include "prototypes.h"
/*@-exitarg@*/
#include "exitcodes.h"
#include "nscd.h"
#include "prototypes.h"
#include "string/strcmp/streq.h"

#ifdef SHADOWGRP
#include "groupio.h"
#include "sgroupio.h"
#include "shadowlog.h"
#include "sssd.h"


/*
* Global variables
*/
Expand Down Expand Up @@ -194,7 +198,7 @@ int main (int argc, char **argv)
if (NULL != sg) {
/* update existing shadow group entry */
sgent = *sg;
if (strcmp (gr->gr_passwd, SHADOW_PASSWD_STRING) != 0)
if (!streq(gr->gr_passwd, SHADOW_PASSWD_STRING))
sgent.sg_passwd = gr->gr_passwd;
} else {
static char *empty = NULL;
Expand Down
4 changes: 2 additions & 2 deletions src/newgrp.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static bool ingroup(const char *name, struct group *gr)

look = gr->gr_mem;
while (*look && notfound)
notfound = strcmp (*look++, name);
notfound = !streq(*look++, name);

return !notfound;
}
Expand Down Expand Up @@ -189,7 +189,7 @@ static void check_perms (const struct group *grp,
}

if (grp->gr_passwd[0] == '\0' ||
strcmp (cpasswd, grp->gr_passwd) != 0) {
!streq(cpasswd, grp->gr_passwd)) {
#ifdef WITH_AUDIT
SNPRINTF(audit_buf, "authentication new-gid=%lu",
(unsigned long) grp->gr_gid);
Expand Down
18 changes: 9 additions & 9 deletions src/newusers.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ static int add_passwd (struct passwd *pwd, const char *password)
* when the entry was created, so this user would have to have had
* the password set someplace else.
*/
if (strcmp (pwd->pw_passwd, "x") != 0) {
if (!streq(pwd->pw_passwd, "x")) {
return update_passwd (pwd, password);
}
#else /* USE_PAM */
Expand All @@ -568,7 +568,7 @@ static int add_passwd (struct passwd *pwd, const char *password)
* The password will be updated later for all users using PAM.
*/
if ( (NULL != sp)
|| (strcmp (pwd->pw_passwd, "x") != 0)) {
|| !streq(pwd->pw_passwd, "x")) {
return 0;
}
#endif /* USE_PAM */
Expand Down Expand Up @@ -754,18 +754,18 @@ static void check_flags (void)
#endif /* USE_SHA_CRYPT || USE_BCRYPT || USE_YESCRYPT */

if (cflg) {
if ( (0 != strcmp (crypt_method, "DES"))
&& (0 != strcmp (crypt_method, "MD5"))
&& (0 != strcmp (crypt_method, "NONE"))
if ( !streq(crypt_method, "DES")
&& !streq(crypt_method, "MD5")
&& !streq(crypt_method, "NONE")
#ifdef USE_SHA_CRYPT
&& (0 != strcmp (crypt_method, "SHA256"))
&& (0 != strcmp (crypt_method, "SHA512"))
&& !streq(crypt_method, "SHA256")
&& !streq(crypt_method, "SHA512")
#endif /* USE_SHA_CRYPT */
#ifdef USE_BCRYPT
&& (0 != strcmp (crypt_method, "BCRYPT"))
&& !streq(crypt_method, "BCRYPT")
#endif /* USE_BCRYPT */
#ifdef USE_YESCRYPT
&& (0 != strcmp (crypt_method, "YESCRYPT"))
&& !streq(crypt_method, "YESCRYPT")
#endif /* USE_YESCRYPT */
) {
fprintf (stderr,
Expand Down
8 changes: 4 additions & 4 deletions src/passwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ static int new_password (const struct passwd *pw)
return -1;
}

if (strcmp (cipher, crypt_passwd) != 0) {
if (!streq(cipher, crypt_passwd)) {
erase_pass (clear);
strzero (cipher);
SYSLOG ((LOG_WARN, "incorrect password for %s",
Expand Down Expand Up @@ -299,7 +299,7 @@ static int new_password (const struct passwd *pw)
MEMZERO(pass);
return -1;
}
if (warned && (strcmp (pass, cp) != 0)) {
if (warned && !streq(pass, cp)) {
warned = false;
}
ret = STRTCPY (pass, cp);
Expand Down Expand Up @@ -333,7 +333,7 @@ static int new_password (const struct passwd *pw)
MEMZERO(pass);
return -1;
}
if (strcmp (cp, pass) != 0) {
if (!streq(cp, pass)) {
erase_pass (cp);
(void) fputs (_("They don't match; try again.\n"), stderr);
} else {
Expand Down Expand Up @@ -839,7 +839,7 @@ main(int argc, char **argv)
case 'r':
/* -r repository (files|nis|nisplus) */
/* only "files" supported for now */
if (strcmp (optarg, "files") != 0) {
if (!streq(optarg, "files")) {
fprintf (stderr,
_("%s: repository %s not supported\n"),
Prog, optarg);
Expand Down
20 changes: 11 additions & 9 deletions src/pwck.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,26 @@
#ident "$Id$"

#include <fcntl.h>
#include <getopt.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <getopt.h>

#include "chkname.h"
#include "commonio.h"
#include "defines.h"
#include "getdef.h"
#include "nscd.h"
#include "prototypes.h"
#include "pwio.h"
#include "shadowio.h"
#include "getdef.h"
#include "nscd.h"
#include "shadowlog.h"
#include "sssd.h"
#include "string/strcmp/streq.h"
#ifdef WITH_TCB
#include "tcbfuncs.h"
#endif /* WITH_TCB */
#include "shadowlog.h"


/*
* Exit codes
Expand Down Expand Up @@ -447,7 +450,7 @@ static void check_pw_file (int *errors, bool *changed)
continue;
}

if (strcmp (pwd->pw_name, ent->pw_name) != 0) {
if (!streq(pwd->pw_name, ent->pw_name)) {
continue;
}

Expand Down Expand Up @@ -518,7 +521,7 @@ static void check_pw_file (int *errors, bool *changed)
/*
* Home directory does not exist, give a warning (unless intentional)
*/
if (NULL == nonexistent || strcmp (pwd->pw_dir, nonexistent) != 0) {
if (NULL == nonexistent || !streq(pwd->pw_dir, nonexistent)) {
printf (_("user '%s': directory '%s' does not exist\n"),
pwd->pw_name, pwd->pw_dir);
*errors += 1;
Expand Down Expand Up @@ -644,8 +647,7 @@ static void check_pw_file (int *errors, bool *changed)
* Make sure no passwords are in passwd.
*/
if ( !quiet
&& (strcmp (pwd->pw_passwd,
SHADOW_PASSWD_STRING) != 0)) {
&& !streq(pwd->pw_passwd, SHADOW_PASSWD_STRING)) {
printf (_("user %s has an entry in %s, but its password field in %s is not set to 'x'\n"),
pwd->pw_name, spw_dbname (), pw_dbname ());
*errors += 1;
Expand Down Expand Up @@ -773,7 +775,7 @@ static void check_spw_file (int *errors, bool *changed)
continue;
}

if (strcmp (spw->sp_namp, ent->sp_namp) != 0) {
if (!streq(spw->sp_namp, ent->sp_namp)) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/su.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ static /*@only@*/struct passwd * do_check_perms (void)
su_failure (caller_tty, 0 == pw->pw_uid);
}
tmp_name = item;
if (strcmp (name, tmp_name) != 0) {
if (!streq(name, tmp_name)) {
SYSLOG ((LOG_INFO,
"Change user from '%s' to '%s' as requested by PAM",
name, tmp_name));
Expand Down
Loading

0 comments on commit 9f12914

Please sign in to comment.