The pointer returned by getutxent() function may always point to
the same shared and reused buffer.
Instead of copying the utmp entry pointer value the content of utmp
entry must be copied otherwise the next call of getutxent() will
overwrite previously found entry.
This commit has no optimisations to highlight what is really fixed.
Fixes: 841776561f56bae7382c6bd47e428201a155d39c (09-08-2025; "lib/utmp.c: Fix umtp entry search")
Signed-off-by: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
if (is_my_tty(ut->ut_line))
break; /* Perfect match, stop the search */
- if (NULL == ut_by_pid)
- ut_by_pid = ut;
+ if (NULL == ut_by_pid) {
+ ut_by_pid = XMALLOC(1, struct utmpx);
+ *ut_by_pid = *ut;
+ }
} else if ( (NULL == ut_by_line)
&& (LOGIN_PROCESS == ut->ut_type) /* Be more picky when matching by 'ut_line' only */
&& (is_my_tty(ut->ut_line))) {
- ut_by_line = ut;
+ ut_by_line = XMALLOC(1, struct utmpx);
+ *ut_by_line = *ut;
}
}
ut = ut_copy;
}
+ free(ut_by_line);
+ free(ut_by_pid);
endutxent();
return ut;