From 24367027d6aa3b2a906c303d3adc6020fcc1a0a3 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sat, 29 Jul 2023 18:22:12 +0200 Subject: [PATCH] Use STRLCPY() instead of its pattern MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This makes it harder to make mistakes while editing the code. Since the sizeof's can be autocalculated, let the machine do that. It also reduces the cognitive load while reading the code. Cc: Christian Göttsche Cc: Serge Hallyn Cc: Iker Pedrosa Signed-off-by: Alejandro Colomar --- lib/console.c | 3 ++- lib/utmp.c | 3 ++- src/login.c | 3 +-- src/su.c | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/console.c b/lib/console.c index 7e2132dd7..a847720a4 100644 --- a/lib/console.c +++ b/lib/console.c @@ -13,6 +13,7 @@ #include #include "getdef.h" #include "prototypes.h" +#include "strlcpy.h" #ident "$Id$" @@ -44,7 +45,7 @@ static bool is_listed (const char *cfgin, const char *tty, bool def) if (*cons != '/') { char *pbuf; - strlcpy (buf, cons, sizeof (buf)); + STRLCPY(buf, cons); pbuf = &buf[0]; while ((s = strtok (pbuf, ":")) != NULL) { if (strcmp (s, tty) == 0) { diff --git a/lib/utmp.c b/lib/utmp.c index aa837ffb5..9b173327a 100644 --- a/lib/utmp.c +++ b/lib/utmp.c @@ -24,6 +24,7 @@ #include "alloc.h" #include "sizeof.h" +#include "strlcpy.h" #include "zustr2stp.h" #ident "$Id$" @@ -46,7 +47,7 @@ static bool is_my_tty (const char tty[UT_LINESIZE]) if ('\0' == tmptty[0]) { const char *tname = ttyname (STDIN_FILENO); if (NULL != tname) - (void) strlcpy (tmptty, tname, sizeof(tmptty)); + STRLCPY(tmptty, tname); } if ('\0' == tmptty[0]) { diff --git a/src/login.c b/src/login.c index 9faec8189..93fd248c1 100644 --- a/src/login.c +++ b/src/login.c @@ -711,8 +711,7 @@ int main (int argc, char **argv) sizeof (loginprompt), _("%s login: "), hostn); } else { - strlcpy (loginprompt, _("login: "), - sizeof (loginprompt)); + STRLCPY(loginprompt, _("login: ")); } retcode = pam_set_item (pamh, PAM_USER_PROMPT, loginprompt); diff --git a/src/su.c b/src/su.c index e3ede5424..9c577f35a 100644 --- a/src/su.c +++ b/src/su.c @@ -676,7 +676,7 @@ static /*@only@*/struct passwd * do_check_perms (void) SYSLOG ((LOG_INFO, "Change user from '%s' to '%s' as requested by PAM", name, tmp_name)); - if (strlcpy (name, tmp_name, sizeof(name)) >= sizeof(name)) { + if (STRLCPY(name, tmp_name) == -1) { fprintf (stderr, _("Overlong user name '%s'\n"), tmp_name); SYSLOG ((LOG_NOTICE, "Overlong user name '%s'", -- 2.47.2