lib/, src/: Use !streq() instead of its pattern

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 <alx@kernel.org>
This commit is contained in:
Alejandro Colomar
2024-11-24 18:40:48 +01:00
committed by Serge Hallyn
parent 5581e74188
commit 9f129146ff
16 changed files with 63 additions and 57 deletions

View File

@@ -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++;
}
}
@@ -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++;
}

View File

@@ -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"),

View File

@@ -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))
@@ -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;
/*
@@ -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;
}
@@ -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;

View File

@@ -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"),

View File

@@ -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,
@@ -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) {
@@ -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;
@@ -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) {

View File

@@ -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;
}
@@ -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"),

View File

@@ -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

View File

@@ -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;
}
@@ -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;
@@ -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;
}

View File

@@ -17,6 +17,7 @@
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <grp.h>
#include <stdio.h>
#include <stdlib.h>
@@ -24,18 +25,21 @@
#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
*/
@@ -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;

View File

@@ -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;
}
@@ -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);

View File

@@ -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 */
@@ -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 */
@@ -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,

View File

@@ -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",
@@ -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);
@@ -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 {
@@ -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);

View File

@@ -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
@@ -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;
}
@@ -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;
@@ -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;
@@ -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;
}

View File

@@ -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));

View File

@@ -1413,7 +1413,7 @@ static void process_flags (int argc, char **argv)
}
if ( '\0' != optarg[0]
&& '*' != optarg[0]
&& strcmp(optarg, "/sbin/nologin") != 0
&& !streq(optarg, "/sbin/nologin")
&& ( stat(optarg, &st) != 0
|| S_ISDIR(st.st_mode)
|| access(optarg, X_OK) != 0)) {

View File

@@ -496,7 +496,7 @@ static void new_pwent (struct passwd *pwent)
* used for this account.
*/
if ( (!is_shadow_pwd)
|| (strcmp (pwent->pw_passwd, SHADOW_PASSWD_STRING) != 0)) {
|| !streq(pwent->pw_passwd, SHADOW_PASSWD_STRING)) {
pwent->pw_passwd = new_pw_passwd (pwent->pw_passwd);
}
@@ -1164,7 +1164,7 @@ process_flags(int argc, char **argv)
}
if ( '\0' != optarg[0]
&& '*' != optarg[0]
&& strcmp(optarg, "/sbin/nologin") != 0
&& !streq(optarg, "/sbin/nologin")
&& ( stat(optarg, &st) != 0
|| S_ISDIR(st.st_mode)
|| access(optarg, X_OK) != 0)) {