From 8b197967469deec88cfae5129a70f97e26cadbd2 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Thu, 4 Jul 2024 19:00:53 +0200 Subject: [PATCH] lib/, src/: Use consistent style using strchr(3) in conditionals While the return value is a pointer, it can be interpreted as a boolean value meaning "found". In general, we use explicit comparisons of pointers to NULL, but in this specific case, let's use that interpretation, and make an exception, using an implicit conversion to boolean. For negative matches, use if (!strchr(...)) For positive matches, use if (strchr(...)) For positive matches, when a variable is also set, use while (NULL != (p = strchr(...))) Signed-off-by: Alejandro Colomar --- lib/env.c | 4 ++-- lib/obscure.c | 4 ++-- lib/setupenv.c | 2 +- lib/tcbfuncs.c | 2 +- src/chfn.c | 4 ++-- src/chpasswd.c | 5 ++--- src/login_nopam.c | 3 +-- src/su.c | 2 +- 8 files changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/env.c b/lib/env.c index 00e89c9dd..3cc5b61ee 100644 --- a/lib/env.c +++ b/lib/env.c @@ -226,9 +226,9 @@ void sanitize_env (void) if (!strprefix(*cur, *bad)) { continue; } - if (strchr (*cur, '/') == NULL) { + if (!strchr(*cur, '/')) continue; /* OK */ - } + for (move = cur; NULL != *move; move++) { *move = *(move + 1); } diff --git a/lib/obscure.c b/lib/obscure.c index 8813f1c83..0b5d574d6 100644 --- a/lib/obscure.c +++ b/lib/obscure.c @@ -13,6 +13,7 @@ #include #include +#include #include "attr.h" #include "prototypes.h" @@ -66,9 +67,8 @@ static bool similar (/*@notnull@*/const char *old, /*@notnull@*/const char *new) } for (i = j = 0; ('\0' != new[i]) && ('\0' != old[i]); i++) { - if (strchr (new, old[i]) != NULL) { + if (strchr(new, old[i])) j++; - } } if (i >= j * 2) { diff --git a/lib/setupenv.c b/lib/setupenv.c index cb0b576a3..f5bf2cfad 100644 --- a/lib/setupenv.c +++ b/lib/setupenv.c @@ -235,7 +235,7 @@ void setup_env (struct passwd *info) if (NULL == cp) { /* not specified, use a minimal default */ addenv ((info->pw_uid == 0) ? "PATH=/sbin:/bin:/usr/sbin:/usr/bin" : "PATH=/bin:/usr/bin", NULL); - } else if (strchr (cp, '=')) { + } else if (strchr(cp, '=')) { /* specified as name=value (PATH=...) */ addenv (cp, NULL); } else { diff --git a/lib/tcbfuncs.c b/lib/tcbfuncs.c index f6082d7cd..2041d66a4 100644 --- a/lib/tcbfuncs.c +++ b/lib/tcbfuncs.c @@ -189,7 +189,7 @@ static shadowtcb_status mkdir_leading (const char *name, uid_t uid) shadow_progname, TCB_DIR, strerror (errno)); goto out_free_path; } - while ((ind = strchr (ptr, '/'))) { + while (NULL != (ind = strchr(ptr, '/'))) { stpcpy(ind, ""); dir = aprintf(TCB_DIR "/%s", path); if (dir == NULL) { diff --git a/src/chfn.c b/src/chfn.c index 7289c6a8a..d92c23a84 100644 --- a/src/chfn.c +++ b/src/chfn.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -159,9 +160,8 @@ static bool may_change_field (int field) cp = "frwh"; } - if (strchr (cp, field) != NULL) { + if (strchr(cp, field)) return true; - } return false; } diff --git a/src/chpasswd.c b/src/chpasswd.c index 6f8423632..fe3e6e9c2 100644 --- a/src/chpasswd.c +++ b/src/chpasswd.c @@ -17,6 +17,7 @@ #include #include #include +#include #ifdef USE_PAM #include "pam_defs.h" @@ -510,10 +511,8 @@ int main (int argc, char **argv) if (feof (stdin) == 0) { // Drop all remaining characters on this line. while (fgets (buf, sizeof buf, stdin) != NULL) { - cp = strchr (buf, '\n'); - if (cp != NULL) { + if (strchr(buf, '\n')) break; - } } fprintf (stderr, diff --git a/src/login_nopam.c b/src/login_nopam.c index 2a90f543e..d51977281 100644 --- a/src/login_nopam.c +++ b/src/login_nopam.c @@ -315,9 +315,8 @@ static bool from_match (char *tok, const char *string) return true; } } else if (strcaseeq(tok, "LOCAL")) { /* LOCAL: no dots */ - if (strchr (string, '.') == NULL) { + if (!strchr(string, '.')) return true; - } } else if ( (!streq(tok, "") && tok[strlen(tok) - 1] == '.') /* network */ && strprefix(resolve_hostname(string), tok)) { return true; diff --git a/src/su.c b/src/su.c index 84dc5dd76..ed4086b64 100644 --- a/src/su.c +++ b/src/su.c @@ -951,7 +951,7 @@ static void set_environment (struct passwd *pw) cp = getdef_str ((pw->pw_uid == 0) ? "ENV_SUPATH" : "ENV_PATH"); if (NULL == cp) { addenv ((pw->pw_uid == 0) ? "PATH=/sbin:/bin:/usr/sbin:/usr/bin" : "PATH=/bin:/usr/bin", NULL); - } else if (strchr (cp, '=') != NULL) { + } else if (strchr(cp, '=')) { addenv (cp, NULL); } else { addenv ("PATH", cp); -- 2.47.3