summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlejandro Colomar <alx@kernel.org>2023-11-16 17:18:04 +0100
committerAlejandro Colomar <alx@kernel.org>2023-11-16 17:20:04 +0100
commit7fa2c4fe05416331310da21a19eecacbb1ec7756 (patch)
tree7d8d224094b7c820a2126d3bcc2a8d5d91772977
parent743416d8bd40bf0093fbd76ac2cac67367e6ea44 (diff)
lib/utmp.c: strdup() to avoid strtcpy()
Technically, this is a memory leak, but since it's a static pointer, and we only dup it once in the program life, it's a controlled memory leak. That's fine. This reduces arbitrary string truncation, removing one call to strtcpy(). The truncation probably didn't matter, because we were comparing to a string that came from what I suspect was a buffer of size 32. Nevertheless, it doesn't hurt to get rid of that arbitrary limitation. Signed-off-by: Alejandro Colomar <alx@kernel.org>
-rw-r--r--lib/utmp.c11
1 files changed, 4 insertions, 7 deletions
diff --git a/lib/utmp.c b/lib/utmp.c
index 8bb76cf2..5fb49d06 100644
--- a/lib/utmp.c
+++ b/lib/utmp.c
@@ -37,22 +37,19 @@ static bool is_my_tty (const char tty[UT_LINESIZE])
{
char full_tty[STRLEN("/dev/") + UT_LINESIZE + 1];
/* tmptty shall be bigger than full_tty */
- static char tmptty[sizeof(full_tty) + 1];
+ static char *tmptty;
full_tty[0] = '\0';
if (tty[0] != '/')
strcpy (full_tty, "/dev/");
strncat (full_tty, tty, UT_LINESIZE);
- if ('\0' == tmptty[0]) {
- const char *tname = ttyname (STDIN_FILENO);
-
- if (NULL == tname) {
+ if (tmptty == NULL) {
+ tmptty = strdup0(ttyname(STDIN_FILENO));
+ if (tmptty == NULL) {
(void) puts (_("Unable to determine your tty name."));
exit (EXIT_FAILURE);
}
-
- STRTCPY(tmptty, tname);
}
return strcmp (full_tty, tmptty) == 0;