From d706cb2ab6ae9718791fdd6186e16e998bb0eba7 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sun, 12 Oct 2025 19:37:53 +0200 Subject: [PATCH 1/3] lib/, src/: Use strspn(3) instead of its pattern Signed-off-by: Alejandro Colomar --- lib/commonio.c | 6 ++---- lib/console.c | 2 +- lib/encrypt.c | 4 ++-- lib/getrange.c | 3 ++- lib/hushed.c | 2 +- lib/limits.c | 4 ++-- lib/port.c | 11 ++++++----- lib/prefix_flag.c | 3 ++- lib/root_flag.c | 3 ++- lib/sub.c | 7 ++++--- lib/ttytype.c | 3 +-- lib/utmp.c | 2 +- lib/yesno.c | 5 ++--- src/check_subid_range.c | 4 ++-- src/chsh.c | 3 ++- src/grpck.c | 8 +++----- src/login.c | 12 +++++------- src/login_nopam.c | 10 +++++----- src/newgrp.c | 4 ++-- src/newusers.c | 2 +- src/passwd.c | 11 +++++------ src/pwck.c | 8 +++----- src/su.c | 8 ++++---- src/sulogin.c | 4 ++-- src/useradd.c | 9 +++++---- src/usermod.c | 7 +++---- 26 files changed, 70 insertions(+), 75 deletions(-) diff --git a/lib/commonio.c b/lib/commonio.c index da530ef222..427081369b 100644 --- a/lib/commonio.c +++ b/lib/commonio.c @@ -37,7 +37,6 @@ #include "string/sprintf/aprintf.h" #include "string/sprintf/stprintf.h" #include "string/strcmp/streq.h" -#include "string/strcmp/strprefix.h" #include "string/strtok/stpsep.h" #undef NDEBUG @@ -488,7 +487,7 @@ static void add_one_entry (struct commonio_db *db, static bool name_is_nis (const char *name) { - return strprefix(name, "+") || strprefix(name, "-"); + return !!strspn(name, "+-"); } @@ -684,8 +683,7 @@ commonio_sort (struct commonio_db *db, int (*cmp) (const void *, const void *)) (NULL != ptr) #if KEEP_NIS_AT_END && ((NULL == ptr->line) - || (('+' != ptr->line[0]) - && ('-' != ptr->line[0]))) + || !strspn(ptr->line, "+-")) #endif ; ptr = ptr->next) { diff --git a/lib/console.c b/lib/console.c index 9df3e2a49e..0bf2f473aa 100644 --- a/lib/console.c +++ b/lib/console.c @@ -50,7 +50,7 @@ is_listed(const char *cfgin, const char *tty, bool def) * console devices upon which root logins are allowed. */ - if (*cons != '/') { + if (!strspn(cons, "/")) { char *pbuf; strtcpy_a(buf, cons); diff --git a/lib/encrypt.c b/lib/encrypt.c index d21f962f36..9b97d951f9 100644 --- a/lib/encrypt.c +++ b/lib/encrypt.c @@ -13,11 +13,11 @@ #include #include +#include #include "prototypes.h" #include "defines.h" #include "shadowlog.h" -#include "string/strcmp/strprefix.h" /*@exposed@*//*@null@*/char *pw_encrypt (const char *clear, const char *salt) @@ -37,7 +37,7 @@ /* Some crypt() do not return NULL if the algorithm is not * supported, and return a DES encrypted password. */ - if ((NULL != salt) && strprefix(salt, "$") && (strlen (cp) <= 13)) + if ((NULL != salt) && strspn(salt, "$") && (strlen (cp) <= 13)) { /*@observer@*/const char *method; switch (salt[1]) diff --git a/lib/getrange.c b/lib/getrange.c index a7dd844f67..b107776d0a 100644 --- a/lib/getrange.c +++ b/lib/getrange.c @@ -9,6 +9,7 @@ #include #include +#include #include "atoi/a2i.h" #include "defines.h" @@ -39,7 +40,7 @@ getrange(const char *range, *has_min = false; *has_max = false; - if ('-' == range[0]) { + if (strspn(range, "-")) { end = range + 1; goto parse_max; } diff --git a/lib/hushed.c b/lib/hushed.c index d88549a647..1af747a2f7 100644 --- a/lib/hushed.c +++ b/lib/hushed.c @@ -61,7 +61,7 @@ bool hushed (const char *username) * file exists in the user's home directory. */ - if (hushfile[0] != '/') { + if (!strspn(hushfile, "/")) { stprintf_a(buf, "%s/%s", pw->pw_dir, hushfile); return (access (buf, F_OK) == 0); } diff --git a/lib/limits.c b/lib/limits.c index 3fabe54d3a..160fadde91 100644 --- a/lib/limits.c +++ b/lib/limits.c @@ -60,7 +60,7 @@ static int setrlimit_value (unsigned int resource, /* The "-" is special, not belonging to a strange negative limit. * It is infinity, in a controlled way. */ - if (strprefix(value, "-")) { + if (strspn(value, "-")) { limit = RLIM_INFINITY; } else { @@ -394,7 +394,7 @@ static int setup_user_limits (const char *uname) break; } else if (streq(name, "*")) { strcpy (deflimits, tempbuf); - } else if (strprefix(name, "@")) { + } else if (strspn(name, "@")) { /* If the user is in the group, the group * limits apply unless later a line for * the specific user is found. diff --git a/lib/port.c b/lib/port.c index cde50b27e2..0529514265 100644 --- a/lib/port.c +++ b/lib/port.c @@ -22,7 +22,6 @@ #include "prototypes.h" #include "string/ctype/isascii.h" #include "string/strcmp/streq.h" -#include "string/strcmp/strprefix.h" #include "string/strtok/stpsep.h" #include "string/strtok/strsep2ls.h" @@ -54,7 +53,7 @@ static int portcmp (const char *pattern, const char *port) if (streq(orig, "SU")) return 1; - return !strprefix(pattern, "*"); + return !strspn(pattern, "*"); } /* @@ -145,7 +144,7 @@ getportent(void) errno = saveerr; return NULL; } - if (strprefix(buf, "#")) + if (strspn(buf, "#")) goto next; stpsep(buf, "\n"); @@ -266,9 +265,11 @@ getportent(void) dtime = dtime * 10 + cp[i] - '0'; } - if (('-' != cp[i]) || (dtime > 2400) || ((dtime % 100) > 59)) { + if (!strspn(cp + i, "-")) goto next; - } + if (dtime > 2400 || (dtime % 100) > 59) + goto next; + port.pt_times[j].t_start = dtime; cp = cp + i + 1; diff --git a/lib/prefix_flag.c b/lib/prefix_flag.c index db8c1dc4ba..325844a001 100644 --- a/lib/prefix_flag.c +++ b/lib/prefix_flag.c @@ -9,6 +9,7 @@ #include #include +#include #include "atoi/getnum.h" #include "defines.h" @@ -100,7 +101,7 @@ extern const char* process_prefix_flag (const char* short_opt, int argc, char ** return ""; /* if prefix is "/" then we ignore the flag option */ /* should we prevent symbolic link from being used as a prefix? */ - if ( prefix[0] != '/') { + if (!strspn(prefix, "/")) { fprintf (log_get_logfd(), _("%s: prefix must be an absolute path\n"), log_get_progname()); diff --git a/lib/root_flag.c b/lib/root_flag.c index 064289afa9..a153598c59 100644 --- a/lib/root_flag.c +++ b/lib/root_flag.c @@ -10,6 +10,7 @@ #ident "$Id$" #include +#include #include "defines.h" /*@-exitarg@*/ @@ -80,7 +81,7 @@ static void change_root (const char* newroot) exit (EXIT_FAILURE); } - if ('/' != newroot[0]) { + if (!strspn(newroot, "/")) { fprintf (log_get_logfd(), _("%s: invalid chroot path '%s', only absolute paths are supported.\n"), log_get_progname(), newroot); diff --git a/lib/sub.c b/lib/sub.c index 14d220eb21..69c49f357e 100644 --- a/lib/sub.c +++ b/lib/sub.c @@ -8,13 +8,14 @@ #include "config.h" -#ident "$Id$" - #include #include +#include #include + #include "prototypes.h" #include "defines.h" + #define MAX_SUBROOT2 "maximum subsystem depth reached\n" #define BAD_SUBROOT2 "invalid root `%s' for user `%s'\n" #define NO_SUBROOT2 "no subsystem root `%s' for user `%s'\n" @@ -45,7 +46,7 @@ void subsystem (const struct passwd *pw) * The new root directory must begin with a "/" character. */ - if (pw->pw_dir[0] != '/') { + if (!strspn(pw->pw_dir, "/")) { printf (_("Invalid root directory '%s'\n"), pw->pw_dir); SYSLOG(LOG_WARN, BAD_SUBROOT2, pw->pw_dir, pw->pw_name); closelog (); diff --git a/lib/ttytype.c b/lib/ttytype.c index e2a9c98f7d..d23516d0c8 100644 --- a/lib/ttytype.c +++ b/lib/ttytype.c @@ -50,9 +50,8 @@ void ttytype (const char *line) return; } while (fgets_a(buf, fp) != NULL) { - if (strprefix(buf, "#")) { + if (strspn(buf, "#")) continue; - } stpsep(buf, "\n"); diff --git a/lib/utmp.c b/lib/utmp.c index 62002768e6..e6e0be47a1 100644 --- a/lib/utmp.c +++ b/lib/utmp.c @@ -60,7 +60,7 @@ is_my_tty(const char tty[UTX_LINESIZE]) char my_tty[countof(full_tty)]; stpcpy(full_tty, ""); - if (tty[0] != '/') + if (!strspn(tty, "/")) strcpy (full_tty, "/dev/"); strncat(full_tty, tty, UTX_LINESIZE); diff --git a/lib/yesno.c b/lib/yesno.c index 6d00616805..76b2f24529 100644 --- a/lib/yesno.c +++ b/lib/yesno.c @@ -16,7 +16,6 @@ #include #include "prototypes.h" -#include "string/strcmp/strcaseprefix.h" /* @@ -78,9 +77,9 @@ yes_or_no(bool read_only) static int rpmatch(const char *response) { - if (strcaseprefix(response, "y")) + if (strspn(response, "yY")) return 1; - if (strcaseprefix(response, "n")) + if (strspn(response, "nN")) return 0; return -1; diff --git a/src/check_subid_range.c b/src/check_subid_range.c index f8d620d0dd..402ed0cf23 100644 --- a/src/check_subid_range.c +++ b/src/check_subid_range.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -18,7 +19,6 @@ #include "idmapping.h" #include "prototypes.h" #include "shadowlog.h" -#include "string/strcmp/strprefix.h" #include "subordinateio.h" @@ -40,7 +40,7 @@ main(int argc, char **argv) exit(1); owner = argv[1]; - check_uids = strprefix(argv[2], "u"); + check_uids = strspn(argv[2], "u"); if (get_uid(argv[3], &start) == -1) exit(1); if (str2ul(&count, argv[4]) == -1) diff --git a/src/chsh.c b/src/chsh.c index 4fd6bd2e0d..24101164e5 100644 --- a/src/chsh.c +++ b/src/chsh.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include "chkname.h" @@ -533,7 +534,7 @@ int main (int argc, char **argv) fail_exit (1, process_selinux); } if (!streq(loginsh, "") - && (loginsh[0] != '/' + && (!strspn(loginsh, "/") || is_restricted_shell (loginsh, process_selinux) || (access (loginsh, X_OK) != 0))) { diff --git a/src/grpck.c b/src/grpck.c index 73469201a0..1a69c9b260 100644 --- a/src/grpck.c +++ b/src/grpck.c @@ -11,11 +11,12 @@ #include "config.h" #include +#include #include #include #include #include -#include +#include #include "chkname.h" #include "commonio.h" @@ -29,7 +30,6 @@ #include "shadowlog.h" #include "sssd.h" #include "string/strcmp/streq.h" -#include "string/strcmp/strprefix.h" #ifdef SHADOWGRP #include "sgroupio.h" @@ -487,10 +487,8 @@ static void check_grp_file(bool *errors, bool *changed, const struct option_flag /* * Skip all NIS entries. */ - - if (strprefix(gre->line, "+") || strprefix(gre->line, "-")) { + if (strspn(gre->line, "+-")) continue; - } /* * Start with the entries that are completely corrupt. They diff --git a/src/login.c b/src/login.c index 951125c6f1..248753c964 100644 --- a/src/login.c +++ b/src/login.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -44,7 +45,6 @@ #include "string/sprintf/stprintf.h" #include "string/strcmp/streq.h" #include "string/strcmp/strneq.h" -#include "string/strcmp/strprefix.h" #include "string/strcpy/strtcpy.h" #include "string/strdup/strdup.h" #include "string/strftime.h" @@ -265,7 +265,7 @@ static void process_flags (int argc, char *const *argv) * clever telnet, and getty holes. */ for (arg = 1; arg < argc; arg++) { - if (strprefix(argv[arg], "-") && strlen(argv[arg]) > 2) { + if (strspn(argv[arg], "-") && strlen(argv[arg]) > 2) { usage (); } if (streq(argv[arg], "--")) { @@ -346,7 +346,7 @@ static void init_env (void) else { cp = getdef_str ("ENV_TZ"); if (NULL != cp) { - addenv(strprefix(cp, "/") ? tz(cp) : cp, NULL); + addenv(strspn(cp, "/") ? tz(cp) : cp, NULL); } } #endif /* !USE_PAM */ @@ -855,10 +855,8 @@ int main (int argc, char **argv) * login, even if they have been * "pre-authenticated." */ - if ( strprefix(user_passwd, "!") - || strprefix(user_passwd, "*")) { + if (strspn(user_passwd, "*!")) failed = true; - } if (streq(user_passwd, "")) { const char *prevent_no_auth = getdef_str("PREVENT_NO_AUTH"); @@ -1011,7 +1009,7 @@ int main (int argc, char **argv) addenv ("IFS= \t\n", NULL); /* ... instead, set a safe IFS */ } - if (strprefix(pwd->pw_shell, "*")) { /* subsystem root */ + if (strspn(pwd->pw_shell, "*")) { /* subsystem root */ pwd->pw_shell++; /* skip the '*' */ subsystem (pwd); /* figure out what to execute */ subroot = true; /* say I was here again */ diff --git a/src/login_nopam.c b/src/login_nopam.c index ef78143bf2..78692a1b9e 100644 --- a/src/login_nopam.c +++ b/src/login_nopam.c @@ -130,7 +130,7 @@ login_access(const char *user, const char *from) TABLE, lineno); continue; } - if (perm[0] != '+' && perm[0] != '-') { + if (!strspn(perm, "+-")) { SYSLOG(LOG_ERR, "%s: line %jd: bad first field", TABLE, lineno); continue; @@ -142,7 +142,7 @@ login_access(const char *user, const char *from) } else if (errno != ENOENT) { SYSLOGE(LOG_ERR, "cannot open %s", TABLE); } - return (!match || strprefix(line, "+"))?1:0; + return (!match || strspn(line, "+"))?1:0; } /* list_match - match an item against a list of tokens with exceptions */ @@ -226,7 +226,7 @@ static bool user_match (char *tok, const char *string) if (host != NULL) { return user_match(tok, string) && from_match(host, myhostname()); #if HAVE_INNETGR - } else if (strprefix(tok, "@")) { /* netgroup */ + } else if (strspn(tok, "@")) { /* netgroup */ return (netgroup_match (tok + 1, NULL, string)); #endif } else if (string_match (tok, string)) { /* ALL or exact match */ @@ -299,13 +299,13 @@ static bool from_match (char *tok, const char *string) * if it matches the head of the string. */ #if HAVE_INNETGR - if (strprefix(tok, "@")) { /* netgroup */ + if (strspn(tok, "@")) { /* netgroup */ return (netgroup_match (tok + 1, string, NULL)); } else #endif if (string_match (tok, string)) { /* ALL or exact match */ return true; - } else if (strprefix(tok, ".")) { /* domain: match last fields */ + } else if (strspn(tok, ".")) { /* domain: match last fields */ size_t str_len, tok_len; str_len = strlen (string); diff --git a/src/newgrp.c b/src/newgrp.c index 69ee7c5611..dd779f99c1 100644 --- a/src/newgrp.c +++ b/src/newgrp.c @@ -476,7 +476,7 @@ int main (int argc, char **argv) * Do the command line for everything that is * not "newgrp". */ - if ((argc > 0) && (argv[0][0] != '-')) { + if ((argc > 0) && !strspn(argv[0], "-")) { if (!is_valid_group_name(argv[0])) { eprintf(_("%s: provided group is not a valid group name\n"), Prog); @@ -508,7 +508,7 @@ int main (int argc, char **argv) * Do the command line for "newgrp". It's just making sure * there aren't any flags and getting the new group name. */ - if ((argc > 0) && strprefix(argv[0], "-")) { + if ((argc > 0) && strspn(argv[0], "-")) { usage (); goto failure; } else if (argv[0] != NULL) { diff --git a/src/newusers.c b/src/newusers.c index 1ee89347ef..6baaea20aa 100644 --- a/src/newusers.c +++ b/src/newusers.c @@ -1090,7 +1090,7 @@ int main (int argc, char **argv) /* FIXME: should check for directory */ mode_t mode = getdef_num ("HOME_MODE", 0777 & ~getdef_num ("UMASK", GETDEF_DEFAULT_UMASK)); - if (newpw.pw_dir[0] != '/') { + if (!strspn(newpw.pw_dir, "/")) { eprintf(_("%s: line %jd: homedir must be an absolute path\n"), Prog, line); fail_exit (EXIT_FAILURE, process_selinux); diff --git a/src/passwd.c b/src/passwd.c index fb774705a5..8557524d14 100644 --- a/src/passwd.c +++ b/src/passwd.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -37,7 +38,6 @@ #include "string/sprintf/aprintf.h" #include "string/sprintf/stprintf.h" #include "string/strcmp/streq.h" -#include "string/strcmp/strprefix.h" #include "string/strcpy/strtcpy.h" #include "string/strdup/strdup.h" #include "time/day_to_str.h" @@ -368,7 +368,7 @@ static void check_password (const struct passwd *pw, const struct spwd *sp, bool * locked may not be changed. * Passwords which have been inactive too long cannot be changed. */ - if ( strprefix(sp->sp_pwdp, "!") + if ( strspn(sp->sp_pwdp, "!") || (exp_status > 1)) { eprintf(_("The password for %s cannot be changed.\n"), sp->sp_namp); @@ -380,9 +380,8 @@ static void check_password (const struct passwd *pw, const struct spwd *sp, bool static /*@observer@*/const char *pw_status (const char *pass) { - if (strprefix(pass, "*") || strprefix(pass, "!")) { + if (strspn(pass, "*!")) return "L"; - } if (streq(pass, "")) { return "NP"; } @@ -526,7 +525,7 @@ static char *update_crypt_pw (char *cp, bool process_selinux) if (dflg) strcpy(cp, ""); - if (uflg && strprefix(cp, "!")) { + if (uflg && strspn(cp, "!")) { if (cp[1] == '\0') { (void) eprintf(_("%s: unlocking the password would result in a passwordless account.\n" "You should set a password with usermod -p to unlock the password of this account.\n"), @@ -537,7 +536,7 @@ static char *update_crypt_pw (char *cp, bool process_selinux) } } - if (lflg && *cp != '!') { + if (lflg && !strspn(cp, "!")) { char *newpw; newpw = xaprintf("!%s", cp); diff --git a/src/pwck.c b/src/pwck.c index 3f1dec5f16..451779cd0b 100644 --- a/src/pwck.c +++ b/src/pwck.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "chkname.h" #include "commonio.h" @@ -30,7 +31,6 @@ #include "shadowlog.h" #include "sssd.h" #include "string/strcmp/streq.h" -#include "string/strcmp/strprefix.h" #ifdef WITH_TCB #include "tcbfuncs.h" #endif /* WITH_TCB */ @@ -387,9 +387,8 @@ static void check_pw_file(bool *errors, bool *changed, const struct option_flags * If this is a NIS line, skip it. You can't "know" what NIS * is going to do without directly asking NIS ... */ - if (strprefix(pfe->line, "+") || strprefix(pfe->line, "-")) { + if (strspn(pfe->line, "+-")) continue; - } /* * Start with the entries that are completely corrupt. They @@ -704,9 +703,8 @@ static void check_spw_file (bool *errors, bool *changed) * If this is a NIS line, skip it. You can't "know" what NIS * is going to do without directly asking NIS ... */ - if (strprefix(spe->line, "+") || strprefix(spe->line, "-")) { + if (strspn(spe->line, "+-")) continue; - } /* * Start with the entries that are completely corrupt. They diff --git a/src/su.c b/src/su.c index cc74ab3a74..51a29975b9 100644 --- a/src/su.c +++ b/src/su.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #ifndef USE_PAM @@ -63,7 +64,6 @@ #include "shadowlog.h" #include "string/sprintf/aprintf.h" #include "string/strcmp/streq.h" -#include "string/strcmp/strprefix.h" #include "string/strcpy/strtcpy.h" #include "string/strdup/strdup.h" @@ -191,7 +191,7 @@ static bool restricted_shell (const char *shellname) setusershell (); while (NULL != (line = getusershell())) { - if (('#' != *line) && streq(line, shellname)) { + if (!strspn(line, "#") && streq(line, shellname)) { endusershell (); return false; } @@ -697,7 +697,7 @@ static /*@only@*/struct passwd * do_check_perms (void) * the shell specified in /etc/passwd (not the one specified with * --shell, which will be the one executed in the chroot later). */ - if (strprefix(pw->pw_shell, "*")) { /* subsystem root required */ + if (strspn(pw->pw_shell, "*")) { /* subsystem root required */ subsystem (pw); /* change to the subsystem root */ endpwent (); /* close the old password databases */ endspent (); @@ -894,7 +894,7 @@ static void set_environment (struct passwd *pw) #ifndef USE_PAM cp = getdef_str ("ENV_TZ"); if (NULL != cp) { - addenv(strprefix(cp, "/") ? tz(cp) : cp, NULL); + addenv(strspn(cp, "/") ? tz(cp) : cp, NULL); } /* diff --git a/src/sulogin.c b/src/sulogin.c index 3daee58b25..0ec124a6ef 100644 --- a/src/sulogin.c +++ b/src/sulogin.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -28,7 +29,6 @@ #include "exitcodes.h" #include "shadowlog.h" #include "string/strcmp/streq.h" -#include "string/strcmp/strprefix.h" #include "string/strdup/strdup.h" @@ -119,7 +119,7 @@ main(int argc, char *argv[]) #ifndef USE_PAM env = getdef_str ("ENV_TZ"); if (NULL != env) { - addenv(strprefix(env, "/") ? tz(env) : env, NULL); + addenv(strspn(env, "/") ? tz(env) : env, NULL); } env = getdef_str ("ENV_HZ"); if (NULL != env) { diff --git a/src/useradd.c b/src/useradd.c index a56d737123..b17bfd3121 100644 --- a/src/useradd.c +++ b/src/useradd.c @@ -70,7 +70,6 @@ #include "string/sprintf/stprintf.h" #include "string/strcmp/strcaseeq.h" #include "string/strcmp/streq.h" -#include "string/strcmp/strprefix.h" #include "string/strdup/strdup.h" #include "string/strtok/stpsep.h" #include "sysconf.h" @@ -1177,7 +1176,8 @@ static void process_flags (int argc, char **argv, struct option_flags *flags) switch (c) { case 'b': if ( ( !VALID (optarg) ) - || ( optarg[0] != '/' )) { + || !strspn(optarg, "/")) + { eprintf(_("%s: invalid base directory '%s'\n"), Prog, optarg); exit (E_BAD_ARG); @@ -1202,7 +1202,8 @@ static void process_flags (int argc, char **argv, struct option_flags *flags) break; case 'd': if ( ( !VALID (optarg) ) - || ( optarg[0] != '/' )) { + || !strspn(optarg, "/")) + { eprintf(_("%s: invalid home directory '%s'\n"), Prog, optarg); exit (E_BAD_ARG); @@ -2176,7 +2177,7 @@ static void create_home(const struct option_flags *flags) bool dir_created; /* Avoid turning a relative path into an absolute path. */ - if (strprefix(bhome, "/") || !streq(path, "")) + if (strspn(bhome, "/") || !streq(path, "")) strcat(path, "/"); strcat(path, cp); diff --git a/src/usermod.c b/src/usermod.c index bd9dc82c64..2b511ca296 100644 --- a/src/usermod.c +++ b/src/usermod.c @@ -64,7 +64,6 @@ #include "string/memset/memzero.h" #include "string/sprintf/aprintf.h" #include "string/strcmp/streq.h" -#include "string/strcmp/strprefix.h" #include "string/strdup/strdup.h" #include "string/strspn/stprspn.h" #include "sysconf.h" @@ -341,7 +340,7 @@ getid_range(const char *str) return result; } - if ('-' != *pos++) + if (!strspn(pos++, "-")) return result; if (a2i(id_t, &last, pos, NULL, 10, first, last) == -1) @@ -469,14 +468,14 @@ usage (int status) static char * new_pw_passwd(char *pw_pass, bool process_selinux) { - if (Lflg && ('!' != pw_pass[0])) { + if (Lflg && !strspn(pw_pass, "!")) { #ifdef WITH_AUDIT audit_logger (AUDIT_USER_CHAUTHTOK, "updating-passwd", user_newname, user_newid, 1); #endif SYSLOG(LOG_INFO, "lock user '%s' password", user_newname); pw_pass = xaprintf("!%s", pw_pass); - } else if (Uflg && strprefix(pw_pass, "!")) { + } else if (Uflg && strspn(pw_pass, "!")) { if (pw_pass[1] == '\0') { eprintf(_("%s: unlocking the user's password would result in a passwordless account.\n" "You should set a password with usermod -p to unlock this user's password.\n"), From 597026fd93016e01f280927a896f838be04cb72f Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sun, 12 Oct 2025 20:14:36 +0200 Subject: [PATCH 2/3] lib/, src/: Use strcspn(3) instead of its pattern Signed-off-by: Alejandro Colomar --- lib/env.c | 2 +- lib/getdef.c | 3 +-- lib/limits.c | 4 ++-- lib/nss.c | 3 +-- lib/port.c | 7 +++---- lib/setupenv.c | 10 ++++++---- src/login_nopam.c | 6 +++--- src/suauth.c | 3 +-- src/useradd.c | 8 +++----- src/userdel.c | 4 +--- src/usermod.c | 10 ++++------ 11 files changed, 26 insertions(+), 34 deletions(-) diff --git a/lib/env.c b/lib/env.c index 37edc05907..eb118440c8 100644 --- a/lib/env.c +++ b/lib/env.c @@ -101,7 +101,7 @@ void addenv (const char *string, /*@null@*/const char *value) */ for (i = 0; i < newenvc; i++) { if ( (strncmp (newstring, newenvp[i], n) == 0) - && (('=' == newenvp[i][n]) || ('\0' == newenvp[i][n]))) { + && !strcspn(newenvp[i] + n, "=")) { break; } } diff --git a/lib/getdef.c b/lib/getdef.c index 4616b65efe..c50d4f4129 100644 --- a/lib/getdef.c +++ b/lib/getdef.c @@ -33,7 +33,6 @@ #include "string/sprintf/aprintf.h" #include "string/strcmp/strcaseeq.h" #include "string/strcmp/streq.h" -#include "string/strcmp/strprefix.h" #include "string/strspn/stpspn.h" #include "string/strspn/stprspn.h" #include "string/strtok/stpsep.h" @@ -567,7 +566,7 @@ static void def_load (void) * Break the line into two fields. */ name = stpspn(buf, " \t"); /* first nonwhite */ - if (streq(name, "") || strprefix(name, "#")) + if (!strcspn(name, "#")) continue; /* comment or empty */ s = stpsep(name, " \t"); /* next field */ diff --git a/lib/limits.c b/lib/limits.c index 160fadde91..7698b17d79 100644 --- a/lib/limits.c +++ b/lib/limits.c @@ -363,9 +363,9 @@ static int setup_user_limits (const char *uname) * FIXME: a better (smarter) checking should be done */ while (fgets_a(buf, fil) != NULL) { - if (strprefix(buf, "#") || strprefix(buf, "\n")) { + if (!strcspn(buf, "#\n")) continue; - } + memzero_a(tempbuf); /* a valid line should have a username, then spaces, * then limits diff --git a/lib/nss.c b/lib/nss.c index 583f0b65f3..2dc08bbb7d 100644 --- a/lib/nss.c +++ b/lib/nss.c @@ -17,7 +17,6 @@ #include "string/sprintf/stprintf.h" #include "string/strcmp/strcaseprefix.h" #include "string/strcmp/streq.h" -#include "string/strcmp/strprefix.h" #include "string/strspn/stpspn.h" #include "string/strtok/stpsep.h" @@ -84,7 +83,7 @@ nss_init(const char *nsswitch_path) { fprintf(log_get_logfd(), "%s: Non-text file.\n", nsswitch_path); goto null_subid; } - if (strprefix(line, "#")) + if (!strcspn(line, "#")) continue; if (strlen(line) < 7) continue; diff --git a/lib/port.c b/lib/port.c index 0529514265..1e48ab2574 100644 --- a/lib/port.c +++ b/lib/port.c @@ -277,11 +277,10 @@ getportent(void) dtime = dtime * 10 + cp[i] - '0'; } - if ( ((',' != cp[i]) && ('\0' != cp[i])) - || (dtime > 2400) - || ((dtime % 100) > 59)) { + if (strcspn(cp + i, ",")) + goto next; + if (dtime > 2400 || (dtime % 100) > 59) goto next; - } port.pt_times[j].t_end = dtime; cp = cp + i + 1; diff --git a/lib/setupenv.c b/lib/setupenv.c index efa0f2d820..86cff6bbdc 100644 --- a/lib/setupenv.c +++ b/lib/setupenv.c @@ -16,9 +16,10 @@ #include #include #include +#include +#include #include #include -#include #include "prototypes.h" #include "defines.h" @@ -27,11 +28,12 @@ #include "shadowlog.h" #include "string/sprintf/aprintf.h" #include "string/strcmp/streq.h" -#include "string/strcmp/strprefix.h" #include "string/strdup/strdup.h" #include "string/strspn/stpspn.h" #include "string/strtok/stpsep.h" +#include + #ifndef USE_PAM static void @@ -61,9 +63,9 @@ static void read_env_file (const char *filename) cp = buf; /* ignore whitespace and comments */ cp = stpspn(cp, " \t"); - if (streq(cp, "") || strprefix(cp, "#")) { + if (!strcspn(cp, "#")) continue; - } + /* * ignore lines which don't follow the name=value format * (for example, the "export NAME" shell commands) diff --git a/src/login_nopam.c b/src/login_nopam.c index 78692a1b9e..5034c7af15 100644 --- a/src/login_nopam.c +++ b/src/login_nopam.c @@ -114,9 +114,9 @@ login_access(const char *user, const char *from) TABLE, lineno); continue; } - if (strprefix(line, "#")) { - continue; /* comment line */ - } + if (!strcspn(line, "#")) + continue; /* comment or empty */ + stpcpy(stprspn(line, " \t"), ""); if (streq(line, "")) { /* skip blank lines */ continue; diff --git a/src/suauth.c b/src/suauth.c index 453f9a4dfa..7bf5b70f6d 100644 --- a/src/suauth.c +++ b/src/suauth.c @@ -22,7 +22,6 @@ #include "io/syslog.h" #include "prototypes.h" #include "string/strcmp/streq.h" -#include "string/strcmp/strprefix.h" #include "string/strspn/stpspn.h" #include "string/strspn/stprspn.h" #include "string/strtok/stpsep.h" @@ -88,7 +87,7 @@ check_su_auth(const char *actual_id, const char *wanted_id, bool su_to_root) stpcpy(stprspn(temp, " \t"), ""); p = stpspn(temp, " \t"); - if (strprefix(p, "#") || streq(p, "")) + if (!strcspn(p, "#")) continue; to_users = strsep(&p, ":"); diff --git a/src/useradd.c b/src/useradd.c index b17bfd3121..df44e5876a 100644 --- a/src/useradd.c +++ b/src/useradd.c @@ -1348,15 +1348,13 @@ static void process_flags (int argc, char **argv, struct option_flags *flags) break; case 's': if ( ( !VALID (optarg) ) - || ( !streq(optarg, "") - && ('/' != optarg[0]) - && ('*' != optarg[0]) )) { + || strcspn(optarg, "/*")) + { eprintf(_("%s: invalid shell '%s'\n"), Prog, optarg); exit (E_BAD_ARG); } - if (!streq(optarg, "") - && '*' != optarg[0] + if (strcspn(optarg, "*") && !streq(optarg, "/sbin/nologin") && !streq(optarg, "/usr/sbin/nologin") && ( stat(optarg, &st) != 0 diff --git a/src/userdel.c b/src/userdel.c index 20f28281c1..eda2b1fe06 100644 --- a/src/userdel.c +++ b/src/userdel.c @@ -683,9 +683,7 @@ static void user_cancel (const char *user) #ifdef EXTRA_CHECK_HOME_DIR static bool path_prefix (const char *s1, const char *s2) { - return ( strprefix(s2, s1) - && ( ('\0' == s2[strlen (s1)]) - || ('/' == s2[strlen (s1)]))); + return strprefix(s2, s1) && !strcspn(s2 + strlen(s1), "/"); } #endif /* EXTRA_CHECK_HOME_DIR */ diff --git a/src/usermod.c b/src/usermod.c index 2b511ca296..30d2c6f2e1 100644 --- a/src/usermod.c +++ b/src/usermod.c @@ -1092,7 +1092,7 @@ process_flags(int argc, char **argv, struct option_flags *flags) } dflg = true; user_newhome = optarg; - if ((user_newhome[0] != '/') && !streq(user_newhome, "")) { + if (strcspn(user_newhome, "/")) { eprintf(_("%s: homedir must be an absolute path\n"), Prog); exit (E_BAD_ARG); @@ -1179,15 +1179,13 @@ process_flags(int argc, char **argv, struct option_flags *flags) break; case 's': if ( ( !VALID (optarg) ) - || ( !streq(optarg, "") - && ('/' != optarg[0]) - && ('*' != optarg[0]) )) { + || strcspn(optarg, "/*")) + { eprintf(_("%s: invalid shell '%s'\n"), Prog, optarg); exit (E_BAD_ARG); } - if (!streq(optarg, "") - && '*' != optarg[0] + if (strcspn(optarg, "*") && !streq(optarg, "/sbin/nologin") && !streq(optarg, "/usr/sbin/nologin") && ( stat(optarg, &st) != 0 From 3360d3a2b870cffdbb7a710ed1a8b57885ab8ea3 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Wed, 12 Nov 2025 16:43:04 +0100 Subject: [PATCH 3/3] lib/env.c: Use strcspn(3) to calculate the length of a substring This removes a local variable. Signed-off-by: Alejandro Colomar --- lib/env.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/env.c b/lib/env.c index eb118440c8..f3aa37fe93 100644 --- a/lib/env.c +++ b/lib/env.c @@ -74,7 +74,7 @@ void initenv (void) void addenv (const char *string, /*@null@*/const char *value) { - char *cp, *newstring; + char *newstring; size_t i, n; if (NULL != value) { @@ -88,13 +88,12 @@ void addenv (const char *string, /*@null@*/const char *value) * just ignore the whole string. */ - cp = strchr (newstring, '='); - if (NULL == cp) { + if (!strchr(newstring, '=')) { free(newstring); return; } - n = (size_t) (cp - newstring); + n = strcspn(newstring, "="); /* * If this environment variable is already set, change its value.