From b648917e0bd3473c5cc26b6d8c5ca93883bfa8b3 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 1 Oct 2020 14:04:21 +0200 Subject: [PATCH] login: use mem2strcpy() rather than rely on printf() The strings from utmp does not have to be terminated. It's seems better to explicitly terminate it than rely on "%.*s" printf() functionality -- printf() man page assumes that "If a precision is given, no null byte need be present", but static analyzers are pretty unhappy with it. Signed-off-by: Karel Zak --- login-utils/login.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/login-utils/login.c b/login-utils/login.c index 248cfb2e3..5c1f03fa2 100644 --- a/login-utils/login.c +++ b/login-utils/login.c @@ -597,17 +597,20 @@ static void log_lastlog(struct login_context *cxt) if ((pread(fd, (void *)&ll, sizeof(ll), offset) == sizeof(ll)) && ll.ll_time != 0) { char time_string[CTIME_BUFSIZ]; + char buf[sizeof(ll.ll_host) + 1]; time_t ll_time = (time_t) ll.ll_time; ctime_r(&ll_time, time_string); printf(_("Last login: %.*s "), 24 - 5, time_string); - if (*ll.ll_host != '\0') - printf(_("from %.*s\n"), - (int)sizeof(ll.ll_host), ll.ll_host); - else - printf(_("on %.*s\n"), - (int)sizeof(ll.ll_line), ll.ll_line); + + if (*ll.ll_host != '\0') { + mem2strcpy(buf, ll.ll_host, sizeof(ll.ll_host), sizeof(buf)); + printf(_("from %s\n"), buf); + } else { + mem2strcpy(buf, ll.ll_line, sizeof(ll.ll_line), sizeof(buf)); + printf(_("on %s\n"), buf); + } } }