From: Alejandro Colomar Date: Sun, 30 Jul 2023 16:07:35 +0000 (+0200) Subject: Call zustr2stp() where appropriate X-Git-Tag: 4.15.0-rc1~202 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=02b1471d5bef083726bcfdf493cd344974de4009;p=thirdparty%2Fshadow.git Call zustr2stp() where appropriate These calls were intending to copy from a NUL-padded (possibly non-NUL-terminated) character sequences contained in fixed-width arrays, into a string, where extra padding is superfluous. Use the appropriate call, which removes the superfluous work. That reduces the chance of confusing maintainers about the intention of the code. While at it, use the appropriate third parameter, which is the size of the source buffer, and not the one of the destination buffer. As a side effect, this reduces the use of '-1', which itself reduces the chance of off-by-one bugs. Also, since using sizeof() on an array is dangerous, use SIZEOF_ARRAY(). Cc: Christian Göttsche Cc: Serge Hallyn Cc: Iker Pedrosa Signed-off-by: Alejandro Colomar --- diff --git a/lib/utmp.c b/lib/utmp.c index be8efa340..f202abe32 100644 --- a/lib/utmp.c +++ b/lib/utmp.c @@ -24,6 +24,7 @@ #include "alloc.h" #include "sizeof.h" +#include "zustr2stp.h" #ident "$Id$" @@ -244,9 +245,8 @@ static #ifdef HAVE_STRUCT_UTMP_UT_HOST } else if ( (NULL != ut) && ('\0' != ut->ut_host[0])) { - hostname = XMALLOC(sizeof(ut->ut_host) + 1, char); - strncpy (hostname, ut->ut_host, sizeof (ut->ut_host)); - hostname[sizeof (ut->ut_host)] = '\0'; + hostname = XMALLOC(SIZEOF_ARRAY(ut->ut_host) + 1, char); + zustr2stp(hostname, ut->ut_host, SIZEOF_ARRAY(ut->ut_host)); #endif /* HAVE_STRUCT_UTMP_UT_HOST */ } diff --git a/src/logoutd.c b/src/logoutd.c index 3a833d88b..2612a178b 100644 --- a/src/logoutd.c +++ b/src/logoutd.c @@ -19,6 +19,8 @@ #include "defines.h" #include "prototypes.h" #include "shadowlog.h" +#include "sizeof.h" +#include "zustr2stp.h" /* * Global variables */ @@ -44,11 +46,7 @@ static int check_login (const struct utmp *ut) char user[sizeof (ut->ut_user) + 1]; time_t now; - /* - * ut_user may not have the terminating NUL. - */ - strncpy (user, ut->ut_user, sizeof (ut->ut_user)); - user[sizeof (ut->ut_user)] = '\0'; + zustr2stp(user, ut->ut_user, SIZEOF_ARRAY(ut->ut_user)); (void) time (&now); @@ -226,8 +224,7 @@ int main (int argc, char **argv) kill (-ut->ut_pid, SIGKILL); } - strncpy (user, ut->ut_user, sizeof (user) - 1); - user[sizeof (user) - 1] = '\0'; + zustr2stp(user, ut->ut_user, SIZEOF_ARRAY(ut->ut_user)); SYSLOG ((LOG_NOTICE, "logged off user '%s' on '%s'", user,