]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
Call zustr2stp() where appropriate
authorAlejandro Colomar <alx@kernel.org>
Sun, 30 Jul 2023 16:07:35 +0000 (18:07 +0200)
committerIker Pedrosa <ikerpedrosam@gmail.com>
Fri, 1 Sep 2023 07:39:23 +0000 (09:39 +0200)
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>
lib/utmp.c
src/logoutd.c

index be8efa340bd6fc731aafc1790b2317aae2c73189..f202abe3248b87f0e9a6edd74686d664f7613ad8 100644 (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 */
        }
 
index 3a833d88b455b63db3dd21836dfcb852084f28b9..2612a178bb3db42043daf130ca832602ab0869a4 100644 (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,