login: fix ut_line comparison logic

Collin Funk collin.funk1@gmail.com
Thu Aug 7 23:54:20 GMT 2025


DJ Delorie <dj@redhat.com> writes:

> ut_line[] is not a string, it's a fixed-width character field,
> and may not be NUL terminated.  Thus, the use of strcmp is incorrect.
> strncmp is more appropriate as it stops at the field size.
>
> Note that differences beyond the field size do not count here,
> as (1) this test doesn't do that, and (2) such differences are
> traditionally ignored (i.e. logins that are silently truncated to
> 8 characters, etc)
>
> While this is "only a test", we should still demonstrate the
> correct way of doing things.  Also, using strncmp avoids a
> "not a string" warning from gcc if you use -O1 or lower,
> where it can't deduce that overflow won't happen.

Looks good with the spacing change that Sam noticed.

Reviewed-by: Collin Funk <collin.funk1@gmail.com>

Out of curiosity, here is how it is documented in the BSDs:

OpenBSD man pages warn about this [1]. FreeBSD doesn't have utmp.h (it
was removed in FreeBSD 9.0), but the fields of utmpx are null-terminated
there [2]. NetBSD doesn't specify if they are null-terminated, as far as
I could tell [3].

Collin

[1] https://man.openbsd.org/utmp.5#CAVEATS
[2] https://man.freebsd.org/cgi/man.cgi?query=utmpx&apropos=0&sektion=0&manpath=FreeBSD+15.0-CURRENT&arch=default&format=html
[3] https://man.netbsd.org/getutxent.3


More information about the Libc-alpha mailing list