On 2014/01/30 11:32, Jérémie Courrèges-Anglas wrote:
> will...@25thandclement.com writes:
> 
> >>Synopsis:   Bad return value for getpwnam_r et al
> >>Category:   42
> >>Environment:
> >     System      : OpenBSD 5.4
> >     Details     : OpenBSD 5.4 (GENERIC.MP) #41: Tue Jul 30 15:30:02 MDT 2013
> >                      
> > dera...@amd64.openbsd.org:/usr/src/sys/arch/amd64/compile/GENERIC.MP
> >
> >     Architecture: OpenBSD.amd64
> >     Machine     : amd64
> >>Description:
> >     POSIX says "[t]he getpwnam_r() function shall return zero on success
> >     or if the requested entry was not found and no error has occurred.
> >     If an error has occurred, an error number shall be returned to
> >     indicate the error."
> >
> >     However, OpenBSD returns 0 on success or 1 on failure; it doesn't
> >     return an error number. Linux, OS X, Solaris, FreeBSD, and NetBSD
> >     all return an error number, and in particular return ERANGE when the
> >     provided buffer is too small.

I don't disagree with this..

http://pubs.opengroup.org/onlinepubs/009695399/functions/getpwnam.html
"If successful, the getpwnam_r() function shall return zero; otherwise, an 
error number shall be returned to indicate the error."

> >     Background: I was looping over getpwnam_r so I wouldn't have to use
> >     sysconf() to get _SC_GETPW_R_SIZE_MAX. Linux/glibc has an annoying
> >     habit of setting some limits to INT_MAX. It doesn't in this case,
> >     but I'm not confident they won't do something stupid down the line.

..but doing a bunch of reallocs to work-around a case of them possibly
breaking something in the future seems like overkill? just checking for
malloc failure (which should be done anyway) and printing a good
diagnostic would surely be enough to point the blame if a bug
of this nature creeps into glibc..

> >     int main(void) {
> >             struct passwd ent, *found;
> >             char *buf = NULL;
> >             size_t bufsiz = 1;
> >             int error;
> >
> >             found = NULL;
> >
> >             buf = malloc(bufsiz);
> >
> >             while ((error = getpwnam_r("root", &ent, buf, bufsiz, &found))) 
> > {
> >                     fprintf(stderr, "bufsiz:%zu error:%s\n", bufsiz, 
> > strerror(error));
>                                                                               
>     ^^^^^
> I think you mean errno, not error.

errno for getpwnam, but this is getpwnam_r, generally the thread-safe
functions specify returning an error code in addition to setting errno.

Reply via email to