getpwnam(3) issues
Alejandro Colomar
alx@kernel.org
Thu Sep 17 20:12:24 GMT 2026
> Date: 2026-09-17 19:08:56+0200
> From: Alejandro Colomar <alx@kernel.org>
>
> Hi!
>
> I've noticed a few issues related to getpwnam(3).
>
> 1)
>
> The glibc manual says 'getpwname' once to (presumably) refer to
> getpwnam(3) --note no trailing 'e'--.
>
> I checked the source code, and there seems to also be a source-
> code comment and a README comment with the same issues.
>
> alx@devuan:~/src/gnu/glibc/master$ grep -rn getpwname
> nss/bug17079.c:120: /* Test getpwname_r. */
> hesiod/README.hesiod:20: * getpwname(), getpwuid()
> manual/pattern.texi:2127:uses @code{getpwname} to look up that user in the user database, and
> alx@devuan:~/src/gnu/glibc/master$ find | grep getpwname
> alx@devuan:~/src/gnu/glibc/master$
>
> 2)
>
> getpwnam(3) sets errno=0 if no entry was found. However, it should
> preserve the errno set by the user (this is what musl does --at least,
> that's what I've been told--, and what POSIX requires).
>
> alx@devuan:~/tmp$ cat getpwnam.c
> #include <errno.h>
> #include <pwd.h>
> #include <stdio.h>
>
> int
> main(void)
> {
> errno = 42;
> if (getpwnam("nonexistent!!!") == NULL)
> printf("%d\n", errno);
> }
> alx@devuan:~/tmp$ gcc -Wall -Wextra getpwnam.c
> alx@devuan:~/tmp$ ./a.out
> 0
>
> The glibc manual doesn't document this weirdness (or I couldn't fint
> it), so it seems it could be unintentional. But even if it were
> intentional, it's not good behavior.
>
> I didn't find any promise in the glibc manual that errno is never
> cleared, but this is assumed to hold everywhere.
POSIX indeed requires it:
No function in this volume of POSIX.1-2024 shall set errno to 0.
Cheers,
Alex
>
>
> Have a lovely day!
> Alex
>
> --
> <https://www.alejandro-colomar.es>
--
<https://www.alejandro-colomar.es>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20260917/43cb15ba/attachment-0001.sig>
More information about the Libc-alpha
mailing list