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 <cgzones@googlemail.com>
Cc: Serge Hallyn <serge@hallyn.com>
Cc: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar
2023-07-30 18:07:35 +02:00
committed by Iker Pedrosa
parent 6a576391d6
commit 02b1471d5b
2 changed files with 7 additions and 10 deletions

View File

@@ -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 */
}

View File

@@ -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,