From 441008c038bc4053d35ed7aa7ff53558f6d178a0 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 10 Dec 2024 04:58:12 +0100 Subject: [PATCH] lib/, src/: Use strprefix() instead of its pattern Signed-off-by: Alejandro Colomar --- lib/copydir.c | 3 ++- lib/env.c | 5 +++-- src/newgidmap.c | 9 ++++++--- src/userdel.c | 3 ++- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/copydir.c b/lib/copydir.c index 6312399e5..3a6c132b5 100644 --- a/lib/copydir.c +++ b/lib/copydir.c @@ -40,6 +40,7 @@ #include "shadowlog.h" #include "string/sprintf/xasprintf.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" static /*@null@*/const char *src_orig; @@ -576,7 +577,7 @@ static int copy_symlink (const struct path_info *src, const struct path_info *ds * create a link to the corresponding entry in the dst_orig * directory. */ - if (strncmp(oldlink, src_orig, strlen(src_orig)) == 0) { + if (strprefix(oldlink, src_orig)) { char *dummy; xasprintf(&dummy, "%s%s", dst_orig, oldlink + strlen(src_orig)); diff --git a/lib/env.c b/lib/env.c index 9cb313712..df1a2c08a 100644 --- a/lib/env.c +++ b/lib/env.c @@ -23,6 +23,7 @@ #include "shadowlog.h" #include "string/sprintf/snprintf.h" #include "string/sprintf/xasprintf.h" +#include "string/strcmp/strprefix.h" #include "string/strdup/xstrdup.h" @@ -175,7 +176,7 @@ void set_env (int argc, char *const *argv) const char *const *p; for (p = forbid; NULL != *p; p++) { - if (strncmp (*argv, *p, strlen (*p)) == 0) { + if (strprefix(*argv, *p)) { break; } } @@ -210,7 +211,7 @@ void sanitize_env (void) for (cur = envp; NULL != *cur; cur++) { for (bad = forbid; NULL != *bad; bad++) { - if (strncmp (*cur, *bad, strlen (*bad)) == 0) { + if (strprefix(*cur, *bad)) { for (move = cur; NULL != *move; move++) { *move = *(move + 1); } diff --git a/src/newgidmap.c b/src/newgidmap.c index 863c5fb66..53c401e75 100644 --- a/src/newgidmap.c +++ b/src/newgidmap.c @@ -13,12 +13,15 @@ #include #include #include + #include "defines.h" -#include "prototypes.h" -#include "subordinateio.h" #include "getdef.h" #include "idmapping.h" +#include "prototypes.h" #include "shadowlog.h" +#include "string/strcmp/strprefix.h" +#include "subordinateio.h" + /* * Global variables @@ -116,7 +119,7 @@ static void write_setgroups(int proc_dir_fd, bool allow_setgroups) strerror(errno)); exit(EXIT_FAILURE); } - if (!strncmp(policy_buffer, policy, strlen(policy))) + if (strprefix(policy_buffer, policy)) goto out; /* Write the policy. */ diff --git a/src/userdel.c b/src/userdel.c index c034afbc1..6bc4074bc 100644 --- a/src/userdel.c +++ b/src/userdel.c @@ -53,6 +53,7 @@ #include "shadowlog.h" #include "string/sprintf/xasprintf.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" #include "string/strdup/xstrdup.h" @@ -701,7 +702,7 @@ static void user_cancel (const char *user) #ifdef EXTRA_CHECK_HOME_DIR static bool path_prefix (const char *s1, const char *s2) { - return ( (strncmp (s2, s1, strlen (s1)) == 0) + return ( strprefix(s2, s1) && ( ('\0' == s2[strlen (s1)]) || ('/' == s2[strlen (s1)]))); } -- 2.47.2