]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/utmp.c: is_my_tty(): Don't cache ttyname(3).
authorAlejandro Colomar <alx@kernel.org>
Fri, 24 Oct 2025 10:22:24 +0000 (12:22 +0200)
committerAlejandro Colomar <foss+github@alejandro-colomar.es>
Tue, 4 Nov 2025 01:56:30 +0000 (02:56 +0100)
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 <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/utmp.c

index 675d3c8ebf7eefacac0585bcf644d9513cfd790a..edd931b7add09b3822731984c52b5c28f276b1df 100644 (file)
@@ -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);
        }