]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/, src/: Use consistent style using strchr(3) in conditionals
authorAlejandro Colomar <alx@kernel.org>
Thu, 4 Jul 2024 17:00:53 +0000 (19:00 +0200)
committerSerge Hallyn <serge@hallyn.com>
Sun, 21 Sep 2025 23:55:20 +0000 (18:55 -0500)
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 <alx@kernel.org>
lib/env.c
lib/obscure.c
lib/setupenv.c
lib/tcbfuncs.c
src/chfn.c
src/chpasswd.c
src/login_nopam.c
src/su.c

index 00e89c9dd05591f9fd5aaeab923a93b04f1e2ed1..3cc5b61eec12b1adf4d41cb4ba5ef58fe006a828 100644 (file)
--- 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);
                        }
index 8813f1c834ea5f0f6be851025eb04d602bc465c3..0b5d574d63a3ef9a1fd87315875cb0f97e8c6047 100644 (file)
@@ -13,6 +13,7 @@
 
 #include <ctype.h>
 #include <stdio.h>
+#include <string.h>
 
 #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) {
index cb0b576a34fef7ab9c5c333a0ead5fd9bdbc614f..f5bf2cfadbb2656c6331f72acb68209faa9b0606 100644 (file)
@@ -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 {
index f6082d7cde7cd782f6151065cb7e7d101f1e23e7..2041d66a4da4ea5916be43d33d49915f081317e6 100644 (file)
@@ -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) {
index 7289c6a8a11bf37bbd9b38b334dacde1116ae937..d92c23a840bc9b3c7b814189fa6e78e7c457d618 100644 (file)
@@ -15,6 +15,7 @@
 #include <pwd.h>
 #include <signal.h>
 #include <stdio.h>
+#include <string.h>
 #include <sys/types.h>
 #include <getopt.h>
 
@@ -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;
 }
index 6f8423632cbc060b2e420aed9b74261eee05ffcf..fe3e6e9c23eb2fb365777a14498fb5140962bc5b 100644 (file)
@@ -17,6 +17,7 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #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,
index 2a90f543e361b35f04d2ea2468e65d8c3d7a5a79..d519772819fc3a8992e6bfec2f70e3358737e6aa 100644 (file)
@@ -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;
index 84dc5dd7644d87a28f836d4bc097eefb788b0007..ed4086b64e8a23d0e666962bd4b1cf14cc1e0ece 100644 (file)
--- 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);