From df7235568460d537bc4a5c55c66a897b6389d0da Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Fri, 24 Oct 2025 12:22:24 +0200 Subject: [PATCH] lib/utmp.c: is_my_tty(): Don't cache ttyname(3). The method for checking for truncation was quite weird. By not caching ttyname(3), we use it directly, without needing a temporary copy, which removes opportunities for bugs. Reviewed-by: Serge Hallyn Signed-off-by: Alejandro Colomar --- lib/utmp.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/utmp.c b/lib/utmp.c index 675d3c8eb..edd931b7a 100644 --- a/lib/utmp.c +++ b/lib/utmp.c @@ -52,21 +52,15 @@ static bool is_my_tty(const char tty[UTX_LINESIZE]) { char full_tty[STRLEN("/dev/") + UTX_LINESIZE + 1]; - /* tmptty shall be bigger than full_tty */ - static char tmptty[sizeof(full_tty) + 1]; + const char *tmptty; stpcpy(full_tty, ""); if (tty[0] != '/') strcpy (full_tty, "/dev/"); strncat(full_tty, tty, UTX_LINESIZE); - if (streq(tmptty, "")) { - const char *tname = ttyname (STDIN_FILENO); - if (NULL != tname) - STRTCPY(tmptty, tname); - } - - if (streq(tmptty, "")) { + tmptty = ttyname(STDIN_FILENO); + if (NULL != tmptty) (void) puts (_("Unable to determine your tty name.")); exit (EXIT_FAILURE); } -- 2.47.3