From 59e5eef38f899cf00925f0117e3173566b14a5fc Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sun, 19 May 2024 01:53:12 +0200 Subject: [PATCH] contrib, lib/, src/, tests/: Use stpcpy(3) instead of its pattern Signed-off-by: Alejandro Colomar --- contrib/adduser.c | 4 ++-- lib/commonio.c | 4 ++-- lib/console.c | 2 +- lib/copydir.c | 3 ++- lib/fields.c | 4 ++-- lib/fputsx.c | 8 ++++---- lib/getdate.y | 8 ++++---- lib/getdef.c | 6 +++--- lib/gshadow.c | 41 ++++++++++++--------------------------- lib/hushed.c | 2 +- lib/loginprompt.c | 4 ++-- lib/obscure.c | 10 ++++------ lib/port.c | 16 ++++++--------- lib/readpassphrase.c | 2 +- lib/salt.c | 19 +++++++++--------- lib/setupenv.c | 7 +++---- lib/sgetgrent.c | 2 +- lib/sgetspent.c | 2 +- lib/shadow.c | 4 ++-- lib/sssd.c | 18 ++++++++--------- lib/tcbfuncs.c | 8 ++++---- lib/ttytype.c | 2 +- lib/tz.c | 2 +- lib/utmp.c | 3 ++- src/chgpasswd.c | 4 ++-- src/chpasswd.c | 5 ++--- src/groupadd.c | 2 +- src/login_nopam.c | 6 +++--- src/logoutd.c | 9 ++++----- src/newusers.c | 5 ++--- src/passwd.c | 7 +++---- src/suauth.c | 2 +- src/useradd.c | 11 +++++------ tests/unit/test_chkname.c | 4 ++-- 34 files changed, 104 insertions(+), 132 deletions(-) diff --git a/contrib/adduser.c b/contrib/adduser.c index 584e098ac..c1c9b9d3d 100644 --- a/contrib/adduser.c +++ b/contrib/adduser.c @@ -491,12 +491,12 @@ safeget (char *buf, int maxlen) bad = (!isalnum (c) && (c != '_') && (c != ' ')); *(buf++) = c; } - *buf = '\0'; + stpcpy(buf, ""); if (bad) { printf ("\nString contained banned character. Please stick to alphanumerics.\n"); - *bstart = '\0'; + stpcpy(bstart, ""); } } diff --git a/lib/commonio.c b/lib/commonio.c index 7bc706707..6a9b77525 100644 --- a/lib/commonio.c +++ b/lib/commonio.c @@ -197,7 +197,7 @@ static int do_lock_file (const char *file, const char *lock, bool log) errno = EINVAL; return 0; } - buf[len] = '\0'; + stpcpy(&buf[len], ""); if (get_pid(buf, &pid) == -1) { if (log) { (void) fprintf (shadow_logfd, @@ -659,7 +659,7 @@ int commonio_open (struct commonio_db *db, int mode) goto cleanup_buf; } } - *strchrnul(buf, '\n') = '\0'; + stpcpy(strchrnul(buf, '\n'), ""); line = strdup (buf); if (NULL == line) { diff --git a/lib/console.c b/lib/console.c index fed315c3b..4691fc436 100644 --- a/lib/console.c +++ b/lib/console.c @@ -76,7 +76,7 @@ static bool is_listed (const char *cfgin, const char *tty, bool def) */ while (fgets (buf, sizeof (buf), fp) != NULL) { - *strchrnul(buf, '\n') = '\0'; + stpcpy(strchrnul(buf, '\n'), ""); if (strcmp (buf, tty) == 0) { (void) fclose (fp); return true; diff --git a/lib/copydir.c b/lib/copydir.c index ae48d10eb..0d855b716 100644 --- a/lib/copydir.c +++ b/lib/copydir.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "alloc/malloc.h" #include "alloc/x/xmalloc.h" @@ -563,7 +564,7 @@ static /*@null@*/char *readlink_malloc (const char *filename) if ((size_t) nchars < size) { /* The buffer was large enough */ /* readlink does not nul-terminate */ - buffer[nchars] = '\0'; + stpcpy(&buffer[nchars], ""); return buffer; } diff --git a/lib/fields.c b/lib/fields.c index b6d2fa14e..256ee34a2 100644 --- a/lib/fields.c +++ b/lib/fields.c @@ -84,7 +84,7 @@ void change_field (char *buf, size_t maxsize, const char *prompt) if (NULL == cp) { return; } - *cp = '\0'; + stpcpy(cp, ""); if ('\0' != newf[0]) { /* @@ -92,7 +92,7 @@ void change_field (char *buf, size_t maxsize, const char *prompt) * makes it possible to change the field to empty, by * entering a space. --marekm */ - *strrspn(newf, " \t\n") = '\0'; + stpcpy(strrspn(newf, " \t\n"), ""); cp = newf; while (isspace (*cp)) { diff --git a/lib/fputsx.c b/lib/fputsx.c index 056531010..72d620992 100644 --- a/lib/fputsx.c +++ b/lib/fputsx.c @@ -10,6 +10,8 @@ #include #include +#include + #include "defines.h" #include "prototypes.h" @@ -33,10 +35,8 @@ fgetsx(/*@returned@*/char *restrict buf, int cnt, FILE *restrict f) ep = strrchr (cp, '\\'); if ((NULL != ep) && (*(ep + 1) == '\n')) { cnt -= ep - cp; - if (cnt > 0) { - cp = ep; - *cp = '\0'; - } + if (cnt > 0) + cp = stpcpy(ep, ""); } else { break; } diff --git a/lib/getdate.y b/lib/getdate.y index b71bf1984..31f1aa0fb 100644 --- a/lib/getdate.y +++ b/lib/getdate.y @@ -646,7 +646,7 @@ static int LookupWord (char *buff) else if (strlen (buff) == 4 && buff[3] == '.') { abbrev = true; - buff[3] = '\0'; + stpcpy(&buff[3], ""); } else abbrev = false; @@ -689,7 +689,7 @@ static int LookupWord (char *buff) i = strlen (buff) - 1; if (buff[i] == 's') { - buff[i] = '\0'; + stpcpy(&buff[i], ""); for (tp = UnitsTable; tp->name; tp++) if (strcmp (buff, tp->name) == 0) { @@ -723,7 +723,7 @@ static int LookupWord (char *buff) *p++ = *q; else i++; - *p = '\0'; + stpcpy(p, ""); if (0 != i) for (tp = TimezoneTable; NULL != tp->name; tp++) if (strcmp (buff, tp->name) == 0) @@ -772,7 +772,7 @@ yylex (void) for (p = buff; (c = *yyInput++, isalpha (c)) || c == '.';) if (p < &buff[sizeof buff - 1]) *p++ = c; - *p = '\0'; + stpcpy(p, ""); yyInput--; return LookupWord (buff); } diff --git a/lib/getdef.c b/lib/getdef.c index 91f40702f..d33f45b14 100644 --- a/lib/getdef.c +++ b/lib/getdef.c @@ -560,7 +560,7 @@ static void def_load (void) /* * Trim trailing whitespace. */ - *strrspn(buf, " \t\n") = '\0'; + stpcpy(strrspn(buf, " \t\n"), ""); /* * Break the line into two fields. @@ -573,9 +573,9 @@ static void def_load (void) if (*s == '\0') continue; /* only 1 field?? */ - *s++ = '\0'; + stpcpy(s++, ""); value = stpspn(s, " \"\t"); /* next nonwhite */ - *strchrnul(value, '"') = '\0'; + stpcpy(strchrnul(value, '"'), ""); /* * Store the value in def_table. diff --git a/lib/gshadow.c b/lib/gshadow.c index 042138882..e79fb7841 100644 --- a/lib/gshadow.c +++ b/lib/gshadow.c @@ -91,7 +91,7 @@ void endsgent (void) } strcpy (sgrbuf, string); - *strchrnul(sgrbuf, '\n') = '\0'; + stpcpy(strchrnul(sgrbuf, '\n'), ""); /* * There should be exactly 4 colon separated fields. Find @@ -172,7 +172,7 @@ void endsgent (void) return NULL; } } - *strchrnul(buf, '\n') = '\0'; + stpcpy(strchrnul(buf, '\n'), ""); return (sgetsgent (buf)); } return NULL; @@ -244,53 +244,36 @@ int putsgent (const struct sgrp *sgrp, FILE * fp) /* * Copy the group name and passwd. */ - - strcpy (cp, sgrp->sg_name); - cp += strlen (cp); - *cp++ = ':'; - - strcpy (cp, sgrp->sg_passwd); - cp += strlen (cp); - *cp++ = ':'; + cp = stpcpy(stpcpy(cp, sgrp->sg_name), ":"); + cp = stpcpy(stpcpy(cp, sgrp->sg_passwd), ":"); /* * Copy the administrators, separating each from the other * with a ",". */ - for (i = 0; NULL != sgrp->sg_adm[i]; i++) { - if (i > 0) { - *cp++ = ','; - } + if (i > 0) + cp = stpcpy(cp, ","); - strcpy (cp, sgrp->sg_adm[i]); - cp += strlen (cp); + cp = stpcpy(cp, sgrp->sg_adm[i]); } - *cp = ':'; - cp++; + cp = stpcpy(cp, ":"); /* * Now do likewise with the group members. */ - for (i = 0; NULL != sgrp->sg_mem[i]; i++) { - if (i > 0) { - *cp = ','; - cp++; - } + if (i > 0) + cp = stpcpy(cp, ","); - strcpy (cp, sgrp->sg_mem[i]); - cp += strlen (cp); + cp = stpcpy(cp, sgrp->sg_mem[i]); } - *cp = '\n'; - cp++; - *cp = '\0'; + stpcpy(cp, "\n"); /* * Output using the function which understands the line * continuation conventions. */ - if (fputsx (buf, fp) == EOF) { free (buf); return -1; diff --git a/lib/hushed.c b/lib/hushed.c index a0fc38f1b..3db09abb1 100644 --- a/lib/hushed.c +++ b/lib/hushed.c @@ -72,7 +72,7 @@ bool hushed (const char *username) return false; } for (found = false; !found && (fgets (buf, sizeof buf, fp) == buf);) { - *strchrnul(buf, '\n') = '\0'; + stpcpy(strchrnul(buf, '\n'), ""); found = (strcmp (buf, pw->pw_shell) == 0) || (strcmp (buf, pw->pw_name) == 0); } diff --git a/lib/loginprompt.c b/lib/loginprompt.c index 7c7531803..6233625ac 100644 --- a/lib/loginprompt.c +++ b/lib/loginprompt.c @@ -87,7 +87,7 @@ void login_prompt (char *name, int namesize) if (NULL == cp) { exit (EXIT_FAILURE); } - *cp = '\0'; /* remove \n [ must be there ] */ + stpcpy(cp, ""); /* remove \n [ must be there ] */ /* * Skip leading whitespace. This makes " username" work right. @@ -98,7 +98,7 @@ void login_prompt (char *name, int namesize) for (i = 0; i < namesize - 1 && *cp != '\0'; name[i++] = *cp++); - name[i] = '\0'; + stpcpy(&name[i], ""); /* * Set the SIGQUIT handler back to its original value diff --git a/lib/obscure.c b/lib/obscure.c index 136f26a67..74a32f0c3 100644 --- a/lib/obscure.c +++ b/lib/obscure.c @@ -189,12 +189,10 @@ static /*@observer@*//*@null@*/const char *obscure_msg ( new1 = xstrdup (new); old1 = xstrdup (old); - if (newlen > maxlen) { - new1[maxlen] = '\0'; - } - if (oldlen > maxlen) { - old1[maxlen] = '\0'; - } + if (newlen > maxlen) + stpcpy(&new1[maxlen], ""); + if (oldlen > maxlen) + stpcpy(&old1[maxlen], ""); msg = password_check (old1, new1, pwdp); diff --git a/lib/port.c b/lib/port.c index d70957d97..03543d5d6 100644 --- a/lib/port.c +++ b/lib/port.c @@ -152,7 +152,7 @@ static struct port *getportent (void) * TTY devices. */ - *strchrnul(buf, '\n') = '\0'; + stpcpy(strchrnul(buf, '\n'), ""); port.pt_names = ttys; for (cp = buf, j = 0; j < PORT_TTY; j++) { @@ -165,12 +165,10 @@ static struct port *getportent (void) break; } - if (',' == *cp) { /* end of current tty name */ - *cp++ = '\0'; - } + if (',' == *cp) /* end of current tty name */ + stpcpy(cp++, ""); } - *cp = '\0'; - cp++; + stpcpy(cp++, ""); port.pt_names[j] = NULL; /* @@ -186,8 +184,7 @@ static struct port *getportent (void) for (j = 1; ':' != *cp; cp++) { if ((',' == *cp) && (j < PORT_IDS)) { - *cp = '\0'; - cp++; + stpcpy(cp++, ""); port.pt_users[j] = cp; j++; } @@ -201,8 +198,7 @@ static struct port *getportent (void) goto again; } - *cp = '\0'; - cp++; + stpcpy(cp++, ""); /* * Get the list of valid times. The times field is the third diff --git a/lib/readpassphrase.c b/lib/readpassphrase.c index 5ff060cca..e23960f01 100644 --- a/lib/readpassphrase.c +++ b/lib/readpassphrase.c @@ -141,7 +141,7 @@ readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags) *p++ = ch; } } - *p = '\0'; + stpcpy(p, ""); save_errno = errno; if (!(term.c_lflag & ECHO)) (void)write(output, "\n", 1); diff --git a/lib/salt.c b/lib/salt.c index 529d59cac..efef4e59c 100644 --- a/lib/salt.c +++ b/lib/salt.c @@ -291,6 +291,7 @@ static /*@observer@*/unsigned long YESCRYPT_get_salt_cost (/*@null@*/const int * static /*@observer@*/void YESCRYPT_salt_cost_to_buf (char *buf, unsigned long cost) { const size_t buf_begin = strlen (buf); + char *p; /* * Check if the result buffer is long enough. @@ -302,17 +303,17 @@ static /*@observer@*/void YESCRYPT_salt_cost_to_buf (char *buf, unsigned long co */ assert (GENSALT_SETTING_SIZE > buf_begin + 4); - buf[buf_begin + 0] = 'j'; + p = &buf[buf_begin]; + p = stpcpy(p, "j"); if (cost < 3) { - buf[buf_begin + 1] = 0x36 + cost; + *p++ = 0x36 + cost; } else if (cost < 6) { - buf[buf_begin + 1] = 0x34 + cost; + *p++ = 0x34 + cost; } else { - buf[buf_begin + 1] = 0x3b + cost; + *p++ = 0x3b + cost; } - buf[buf_begin + 2] = cost >= 3 ? 'T' : '5'; - buf[buf_begin + 3] = '$'; - buf[buf_begin + 4] = '\0'; + p = stpcpy(p, (cost >= 3) ? "T" : "5"); + stpcpy(p, "$"); } #endif /* USE_YESCRYPT */ @@ -330,7 +331,7 @@ static /*@observer@*/const char *gensalt (size_t salt_size) strcat (salt, l64a (csrand ())); } while (strlen (salt) < salt_size); - salt[salt_size] = '\0'; + stpcpy(&salt[salt_size], ""); return salt; } @@ -421,7 +422,7 @@ static /*@observer@*/const char *gensalt (size_t salt_size) salt_len = GENSALT_SETTING_SIZE - 1; rounds = 0; memset(result, '.', salt_len); - result[salt_len] = '\0'; + stpcpy(&result[salt_len], ""); } char *retval = crypt_gensalt (result, rounds, NULL, 0); diff --git a/lib/setupenv.c b/lib/setupenv.c index 8eda5d58f..6ed08c98c 100644 --- a/lib/setupenv.c +++ b/lib/setupenv.c @@ -56,7 +56,7 @@ static void read_env_file (const char *filename) if (NULL == cp) { break; } - *cp = '\0'; + stpcpy(cp, ""); cp = buf; /* ignore whitespace and comments */ @@ -78,8 +78,7 @@ static void read_env_file (const char *filename) continue; } /* NUL-terminate the name */ - *cp = '\0'; - cp++; + stpcpy(cp++, ""); val = cp; #if 0 /* XXX untested, and needs rewrite with fewer goto's :-) */ /* @@ -112,7 +111,7 @@ static void read_env_file (const char *filename) goto finished; } else if (isspace (*cp)) { /* unescaped whitespace - end of string */ - *cp = '\0'; + stpcpy(cp, ""); goto finished; } else { cp++; diff --git a/lib/sgetgrent.c b/lib/sgetgrent.c index f62f6973c..36137bcf5 100644 --- a/lib/sgetgrent.c +++ b/lib/sgetgrent.c @@ -83,7 +83,7 @@ struct group *sgetgrent (const char *buf) } } strcpy (grpbuf, buf); - *strchrnul(grpbuf, '\n') = '\0'; + stpcpy(strchrnul(grpbuf, '\n'), ""); for (cp = grpbuf, i = 0; (i < NFIELDS) && (NULL != cp); i++) grpfields[i] = strsep(&cp, ":"); diff --git a/lib/sgetspent.c b/lib/sgetspent.c index f4ace5218..c693acd08 100644 --- a/lib/sgetspent.c +++ b/lib/sgetspent.c @@ -54,7 +54,7 @@ sgetspent(const char *string) return NULL; /* fail if too long */ } strcpy (spwbuf, string); - *strchrnul(spwbuf, '\n') = '\0'; + stpcpy(strchrnul(spwbuf, '\n'), ""); /* * Tokenize the string into colon separated fields. Allow up to diff --git a/lib/shadow.c b/lib/shadow.c index 3a867ae27..3152595c0 100644 --- a/lib/shadow.c +++ b/lib/shadow.c @@ -77,7 +77,7 @@ static struct spwd *my_sgetspent (const char *string) if (strlen (string) >= sizeof spwbuf) return 0; strcpy (spwbuf, string); - *strchrnul(spwbuf, '\n') = '\0'; + stpcpy(strchrnul(spwbuf, '\n'), ""); /* * Tokenize the string into colon separated fields. Allow up to @@ -202,7 +202,7 @@ struct spwd *fgetspent (FILE * fp) if (fgets (buf, sizeof buf, fp) != NULL) { - *strchrnul(buf, '\n') = '\0'; + stpcpy(strchrnul(buf, '\n'), ""); return my_sgetspent (buf); } return 0; diff --git a/lib/sssd.c b/lib/sssd.c index 4db799543..09f539f46 100644 --- a/lib/sssd.c +++ b/lib/sssd.c @@ -24,7 +24,7 @@ int sssd_flush_cache(int dbflags) { int status, code, rv; - int i = 0; + char *p; char *sss_cache_args = NULL; const char *cmd = "/usr/sbin/sss_cache"; const char *spawnedArgs[] = {"sss_cache", NULL, NULL}; @@ -40,15 +40,13 @@ sssd_flush_cache(int dbflags) return -1; } - sss_cache_args[i++] = '-'; - if (dbflags & SSSD_DB_PASSWD) { - sss_cache_args[i++] = 'U'; - } - if (dbflags & SSSD_DB_GROUP) { - sss_cache_args[i++] = 'G'; - } - sss_cache_args[i++] = '\0'; - if (i == 2) { + p = stpcpy(sss_cache_args, "-"); + if (dbflags & SSSD_DB_PASSWD) + stpcpy(p, "U"); + if (dbflags & SSSD_DB_GROUP) + stpcpy(p, "G"); + + if (*p == '\0') { /* Neither passwd nor group, nothing to do */ free(sss_cache_args); return 0; diff --git a/lib/tcbfuncs.c b/lib/tcbfuncs.c index b5915fcd2..3dc2abd70 100644 --- a/lib/tcbfuncs.c +++ b/lib/tcbfuncs.c @@ -135,13 +135,13 @@ static /*@null@*/ char *shadowtcb_path_rel_existing (const char *name) } free (path); if ((size_t)ret >= sizeof(link) - 1) { - link[sizeof(link) - 1] = '\0'; + stpcpy(&link[sizeof(link) - 1], ""); fprintf (shadow_logfd, _("%s: Suspiciously long symlink: %s\n"), shadow_progname, link); return NULL; } - link[ret] = '\0'; + stpcpy(&link[ret], ""); rval = strdup (link); if (NULL == rval) { OUT_OF_MEMORY; @@ -200,7 +200,7 @@ static shadowtcb_status mkdir_leading (const char *name, uid_t uid) goto out_free_path; } while ((ind = strchr (ptr, '/'))) { - *ind = '\0'; + stpcpy(ind, ""); if (asprintf (&dir, TCB_DIR "/%s", path) == -1) { OUT_OF_MEMORY; return SHADOWTCB_FAILURE; @@ -266,7 +266,7 @@ static shadowtcb_status rmdir_leading (char *path) char *ind, *dir; shadowtcb_status ret = SHADOWTCB_SUCCESS; while ((ind = strrchr (path, '/'))) { - *ind = '\0'; + stpcpy(ind, ""); if (asprintf (&dir, TCB_DIR "/%s", path) == -1) { OUT_OF_MEMORY; return SHADOWTCB_FAILURE; diff --git a/lib/ttytype.c b/lib/ttytype.c index b07b68624..c0f6de6fe 100644 --- a/lib/ttytype.c +++ b/lib/ttytype.c @@ -49,7 +49,7 @@ void ttytype (const char *line) continue; } - *strchrnul(buf, '\n') = '\0'; + stpcpy(strchrnul(buf, '\n'), ""); if ( (sscanf (buf, "%1023s %1023s", type, port) == 2) && (strcmp (line, port) == 0)) { diff --git a/lib/tz.c b/lib/tz.c index c22268bf6..cb04e5173 100644 --- a/lib/tz.c +++ b/lib/tz.c @@ -42,7 +42,7 @@ strcpy (tzbuf, def_tz); } else { - *strchrnul(tzbuf, '\n') = '\0'; + stpcpy(strchrnul(tzbuf, '\n'), ""); } if (NULL != fp) { diff --git a/lib/utmp.c b/lib/utmp.c index e8f9c7937..4e10eb986 100644 --- a/lib/utmp.c +++ b/lib/utmp.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "alloc/x/xcalloc.h" @@ -46,7 +47,7 @@ is_my_tty(const char tty[UTX_LINESIZE]) /* tmptty shall be bigger than full_tty */ static char tmptty[sizeof(full_tty) + 1]; - full_tty[0] = '\0'; + stpcpy(full_tty, ""); if (tty[0] != '/') strcpy (full_tty, "/dev/"); strncat(full_tty, tty, UTX_LINESIZE); diff --git a/src/chgpasswd.c b/src/chgpasswd.c index 1ff6776b1..6c1e632dc 100644 --- a/src/chgpasswd.c +++ b/src/chgpasswd.c @@ -462,7 +462,7 @@ int main (int argc, char **argv) line++; cp = strrchr (buf, '\n'); if (NULL != cp) { - *cp = '\0'; + stpcpy(cp, ""); } else { fprintf (stderr, _("%s: line %d: line too long\n"), Prog, line); @@ -482,7 +482,7 @@ int main (int argc, char **argv) name = buf; cp = strchr (name, ':'); if (NULL != cp) { - *cp = '\0'; + stpcpy(cp, ""); cp++; } else { fprintf (stderr, diff --git a/src/chpasswd.c b/src/chpasswd.c index 79880f568..5d87119aa 100644 --- a/src/chpasswd.c +++ b/src/chpasswd.c @@ -503,7 +503,7 @@ int main (int argc, char **argv) line++; cp = strrchr (buf, '\n'); if (NULL != cp) { - *cp = '\0'; + stpcpy(cp, ""); } else { if (feof (stdin) == 0) { @@ -535,8 +535,7 @@ int main (int argc, char **argv) name = buf; cp = strchr (name, ':'); if (NULL != cp) { - *cp = '\0'; - cp++; + stpcpy(cp++, ""); } else { fprintf (stderr, _("%s: line %d: missing new password\n"), diff --git a/src/groupadd.c b/src/groupadd.c index 095a41eed..f83bd1f57 100644 --- a/src/groupadd.c +++ b/src/groupadd.c @@ -431,7 +431,7 @@ static void process_flags (int argc, char **argv) exit (E_BAD_ARG); } /* terminate name, point to value */ - *cp++ = '\0'; + stpcpy(cp++, ""); if (putdef_str (optarg, cp, NULL) < 0) { exit (E_BAD_ARG); } diff --git a/src/login_nopam.c b/src/login_nopam.c index 03a2871f4..b9fb647c0 100644 --- a/src/login_nopam.c +++ b/src/login_nopam.c @@ -111,7 +111,7 @@ int login_access (const char *user, const char *from) if (line[0] == '#') { continue; /* comment line */ } - *strrspn(line, " \t\n") = '\0'; + stpcpy(strrspn(line, " \t\n"), ""); if (line[0] == '\0') { /* skip blank lines */ continue; } @@ -182,7 +182,7 @@ static char *myhostname (void) if (name[0] == '\0') { gethostname (name, sizeof (name)); - name[MAXHOSTNAMELEN] = '\0'; + stpcpy(&name[MAXHOSTNAMELEN], ""); } return (name); } @@ -222,7 +222,7 @@ static bool user_match (const char *tok, const char *string) */ at = strchr (tok + 1, '@'); if (NULL != at) { /* split user@host pattern */ - *at = '\0'; + stpcpy(at, ""); return ( user_match (tok, string) && from_match (at + 1, myhostname ())); #if HAVE_INNETGR diff --git a/src/logoutd.c b/src/logoutd.c index d77f757fb..11a5d60a3 100644 --- a/src/logoutd.c +++ b/src/logoutd.c @@ -200,11 +200,10 @@ main(int argc, char **argv) } /* child */ - if (strncmp (ut->ut_line, "/dev/", 5) != 0) { - strcpy (tty_name, "/dev/"); - } else { - tty_name[0] = '\0'; - } + if (strncmp(ut->ut_line, "/dev/", 5) != 0) + strcpy(tty_name, "/dev/"); + else + strcpy(tty_name, ""); STRNCAT(tty_name, ut->ut_line); #ifndef O_NOCTTY diff --git a/src/newusers.c b/src/newusers.c index 4f7b96fa8..6a87e3d9c 100644 --- a/src/newusers.c +++ b/src/newusers.c @@ -1107,9 +1107,8 @@ int main (int argc, char **argv) Prog, line); fail_exit (EXIT_FAILURE); } - if (cp != NULL) { - *cp = '\0'; - } + if (cp != NULL) + stpcpy(cp, ""); /* * Break the string into fields and screw around with them. diff --git a/src/passwd.c b/src/passwd.c index f1fc6595f..b7bf925db 100644 --- a/src/passwd.c +++ b/src/passwd.c @@ -226,7 +226,7 @@ static int new_password (const struct passwd *pw) erase_pass (clear); strzero (cipher); } else { - orig[0] = '\0'; + strcpy(orig, ""); } /* @@ -514,9 +514,8 @@ static char *update_crypt_pw (char *cp) } } - if (dflg) { - *cp = '\0'; - } + if (dflg) + strcpy(cp, ""); if (uflg && *cp == '!') { if (cp[1] == '\0') { diff --git a/src/suauth.c b/src/suauth.c index ed45a8564..ac9af264f 100644 --- a/src/suauth.c +++ b/src/suauth.c @@ -81,7 +81,7 @@ int check_su_auth (const char *actual_id, continue; } - *strrspn(temp, " \t\n") = '\0'; + stpcpy(strrspn(temp, " \t\n"), ""); posn = 0; while (temp[posn] == ' ' || temp[posn] == '\t') diff --git a/src/useradd.c b/src/useradd.c index d307aecae..bb907bab6 100644 --- a/src/useradd.c +++ b/src/useradd.c @@ -361,7 +361,7 @@ static void get_defaults (void) * values are used, everything else can be ignored. */ while (fgets (buf, sizeof buf, fp) == buf) { - *strchrnul(buf, '\n') = '\0'; + stpcpy(strchrnul(buf, '\n'), ""); cp = strchr (buf, '='); if (NULL == cp) { @@ -605,7 +605,7 @@ static int set_defaults (void) while (fgets (buf, sizeof buf, ifp) == buf) { cp = strrchr (buf, '\n'); if (NULL != cp) { - *cp = '\0'; + stpcpy(cp, ""); } else { /* A line which does not end with \n is only valid * at the end of the file. @@ -1358,8 +1358,7 @@ static void process_flags (int argc, char **argv) exit (E_BAD_ARG); } /* terminate name, point to value */ - *cp = '\0'; - cp++; + stpcpy(cp++, ""); if (putdef_str (optarg, cp, NULL) < 0) { exit (E_BAD_ARG); } @@ -2223,7 +2222,7 @@ static void create_home (void) if (access (prefix_user_home, F_OK) == 0) return; - path[0] = '\0'; + strcpy(path, ""); bhome = strdup(prefix_user_home); if (!bhome) { fprintf(stderr, @@ -2269,7 +2268,7 @@ static void create_home (void) Prog, path); fail_exit(E_HOMEDIR); } - btrfs_check[strlen(path) - strlen(cp) - 1] = '\0'; + stpcpy(&btrfs_check[strlen(path) - strlen(cp) - 1], ""); if (is_btrfs(btrfs_check) <= 0) { fprintf(stderr, _("%s: home directory \"%s\" must be mounted on BTRFS\n"), diff --git a/tests/unit/test_chkname.c b/tests/unit/test_chkname.c index 127114dcb..3f190a773 100644 --- a/tests/unit/test_chkname.c +++ b/tests/unit/test_chkname.c @@ -139,10 +139,10 @@ test_is_valid_user_name_long(void **state) memset(name, '_', max); - name[max] = '\0'; + stpcpy(&name[max], ""); assert_true(false == is_valid_user_name(name)); - name[max - 1] = '\0'; + stpcpy(&name[max - 1], ""); assert_true(is_valid_user_name(name)); free(name);