summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlejandro Colomar <alx@kernel.org>2023-11-15 22:14:18 +0100
committerAlejandro Colomar <alx@kernel.org>2023-11-16 15:26:45 +0100
commit8711bc63fef4c6e186039bfb1ed543485ae478db (patch)
treeb9b83e1eb9ca6117c0b69e370ff9869ec25e5a3a
parent6907d3c960a0aa07123cea50dc1da2c2e735def1 (diff)
lib/: Remove off-by-one bugs in calls to strncpy(3)
We're not even zeroing the last byte after this call. This was a completely gratuitous truncation of one byte, and the resulting character array still wasn't guaranteed to be null terminated, because strncpy(3) can't do that. Just to clarify, none of these structures needed zeroing, as they are treated as null-padded fixed-size character arrays. Calling strncpy(3) was actually the correct call, and the only problem was unnecessarily truncating strings by one byte more than necessary. Cc: Matthew House <mattlloydhouse@gmail.com> Signed-off-by: Alejandro Colomar <alx@kernel.org>
-rw-r--r--lib/log.c2
-rw-r--r--lib/utmp.c8
2 files changed, 5 insertions, 5 deletions
diff --git a/lib/log.c b/lib/log.c
index 9457b1cd..04aa3cfa 100644
--- a/lib/log.c
+++ b/lib/log.c
@@ -82,7 +82,7 @@ void dolastlog (
newlog.ll_time = ll_time;
STRTCPY(newlog.ll_line, line);
#if HAVE_LL_HOST
- strncpy (newlog.ll_host, host, sizeof (newlog.ll_host) - 1);
+ strncpy(newlog.ll_host, host, sizeof(newlog.ll_host));
#endif
if ( (lseek (fd, offset, SEEK_SET) != offset)
|| (write_full(fd, &newlog, sizeof newlog) == -1)
diff --git a/lib/utmp.c b/lib/utmp.c
index 906a9fac..0ec2692c 100644
--- a/lib/utmp.c
+++ b/lib/utmp.c
@@ -262,25 +262,25 @@ static
utent->ut_type = USER_PROCESS;
#endif /* HAVE_STRUCT_UTMP_UT_TYPE */
utent->ut_pid = getpid ();
- strncpy (utent->ut_line, line, sizeof (utent->ut_line) - 1);
+ strncpy(utent->ut_line, line, sizeof(utent->ut_line));
#ifdef HAVE_STRUCT_UTMP_UT_ID
if (NULL != ut) {
strncpy (utent->ut_id, ut->ut_id, sizeof (utent->ut_id));
} else {
/* XXX - assumes /dev/tty?? */
- strncpy (utent->ut_id, line + 3, sizeof (utent->ut_id) - 1);
+ strncpy(utent->ut_id, line + 3, sizeof(utent->ut_id));
}
#endif /* HAVE_STRUCT_UTMP_UT_ID */
#ifdef HAVE_STRUCT_UTMP_UT_NAME
strncpy (utent->ut_name, name, sizeof (utent->ut_name));
#endif /* HAVE_STRUCT_UTMP_UT_NAME */
#ifdef HAVE_STRUCT_UTMP_UT_USER
- strncpy (utent->ut_user, name, sizeof (utent->ut_user) - 1);
+ strncpy(utent->ut_user, name, sizeof(utent->ut_user));
#endif /* HAVE_STRUCT_UTMP_UT_USER */
if (NULL != hostname) {
struct addrinfo *info = NULL;
#ifdef HAVE_STRUCT_UTMP_UT_HOST
- strncpy (utent->ut_host, hostname, sizeof (utent->ut_host) - 1);
+ strncpy(utent->ut_host, hostname, sizeof(utent->ut_host));
#endif /* HAVE_STRUCT_UTMP_UT_HOST */
#ifdef HAVE_STRUCT_UTMP_UT_SYSLEN
utent->ut_syslen = MIN (strlen (hostname),