diff --git a/lib/fields.c b/lib/fields.c index 8a560352..1002d9d8 100644 --- a/lib/fields.c +++ b/lib/fields.c @@ -100,8 +100,7 @@ void change_field (char *buf, size_t maxsize, const char *prompt) cp++; } - strncpy (buf, cp, maxsize - 1); - buf[maxsize - 1] = '\0'; + strlcpy (buf, cp, maxsize); } } diff --git a/lib/gshadow.c b/lib/gshadow.c index cff8cb58..f075aa97 100644 --- a/lib/gshadow.c +++ b/lib/gshadow.c @@ -15,6 +15,7 @@ #ident "$Id$" #include +#include #include "prototypes.h" #include "defines.h" static /*@null@*/FILE *shadow; @@ -124,8 +125,7 @@ void endsgent (void) sgrbuflen = len; } - strncpy (sgrbuf, string, len); - sgrbuf[len-1] = '\0'; + strlcpy (sgrbuf, string, len); cp = strrchr (sgrbuf, '\n'); if (NULL != cp) { diff --git a/libmisc/console.c b/libmisc/console.c index c475aa23..bc024eba 100644 --- a/libmisc/console.c +++ b/libmisc/console.c @@ -44,8 +44,7 @@ static bool is_listed (const char *cfgin, const char *tty, bool def) if (*cons != '/') { char *pbuf; - strncpy (buf, cons, sizeof (buf)); - buf[sizeof (buf) - 1] = '\0'; + strlcpy (buf, cons, sizeof (buf)); pbuf = &buf[0]; while ((s = strtok (pbuf, ":")) != NULL) { if (strcmp (s, tty) == 0) { diff --git a/libmisc/date_to_str.c b/libmisc/date_to_str.c index 07e99f1a..f3b9dc76 100644 --- a/libmisc/date_to_str.c +++ b/libmisc/date_to_str.c @@ -38,9 +38,10 @@ void date_to_str (size_t size, char buf[size], long date) time_t t; t = date; - if (date < 0) - (void) strncpy (buf, "never", size); - else + if (date < 0) { + (void) strlcpy (buf, "never", size); + } else { (void) strftime (buf, size, "%Y-%m-%d", gmtime (&t)); - buf[size - 1] = '\0'; + buf[size - 1] = '\0'; + } } diff --git a/libmisc/utmp.c b/libmisc/utmp.c index 6662bbe9..e435c322 100644 --- a/libmisc/utmp.c +++ b/libmisc/utmp.c @@ -40,10 +40,8 @@ static bool is_my_tty (const char *tty) if ('\0' == tmptty[0]) { const char *tname = ttyname (STDIN_FILENO); - if (NULL != tname) { - (void) strncpy (tmptty, tname, sizeof tmptty); - tmptty[sizeof (tmptty) - 1] = '\0'; - } + if (NULL != tname) + (void) strlcpy (tmptty, tname, sizeof tmptty); } if ('\0' == tmptty[0]) { diff --git a/src/su.c b/src/su.c index b2f0378b..9578ab2b 100644 --- a/src/su.c +++ b/src/su.c @@ -653,8 +653,7 @@ static /*@only@*/struct passwd * check_perms (void) SYSLOG ((LOG_INFO, "Change user from '%s' to '%s' as requested by PAM", name, tmp_name)); - strncpy (name, tmp_name, sizeof(name) - 1); - name[sizeof(name) - 1] = '\0'; + strlcpy (name, tmp_name, sizeof(name)); pw = xgetpwnam (name); if (NULL == pw) { (void) fprintf (stderr,