]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
login: use system definitions to determine maxium login name length
authorSami Kerola <kerolasa@iki.fi>
Fri, 13 Nov 2020 21:25:02 +0000 (21:25 +0000)
committerSami Kerola <kerolasa@iki.fi>
Thu, 3 Dec 2020 20:07:29 +0000 (20:07 +0000)
Allocating stack for about 10x more than longest possible user name is
wasteful, even if it happens for reletively short period of time.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
login-utils/login.c

index b03808a5c2e16acf1a736c09abb0af5d6a26af87..0e7629f20f1cb75d72b57c8cb2ddee288dae7b5e 100644 (file)
 # define TTY_MODE 0600
 #endif
 
-#ifndef SPT_BUFSIZE
-# define SPT_BUFSIZE     2048
-#endif
-
 static char **argv0;
 static size_t argv_lth;
 
@@ -260,18 +256,19 @@ static void process_title_init (int argc, char **argv)
                argv0 = argv;
 }
 
-static void process_title_update (const char *prog, const char *txt)
+static void process_title_update(const char *username)
 {
         size_t i;
-        char buf[SPT_BUFSIZE];
+        const char prefix[] = "login -- ";
+        char buf[sizeof(prefix) + LOGIN_NAME_MAX];
 
         if (!argv0)
                 return;
 
-       if (strlen(prog) + strlen(txt) + 5 > SPT_BUFSIZE)
+       if (sizeof(buf) < (sizeof(prefix) + strlen(username) + 1))
                return;
 
-       sprintf(buf, "%s -- %s", prog, txt);
+       snprintf(buf, sizeof(buf), "%s%s", prefix, username);
 
         i = strlen(buf);
         if (i > argv_lth - 2) {
@@ -1452,7 +1449,7 @@ int main(int argc, char **argv)
 
        init_environ(&cxt);             /* init $HOME, $TERM ... */
 
-       process_title_update("login", cxt.username);
+       process_title_update(cxt.username);
 
        log_syslog(&cxt);