tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: strerror(0) POSIX compliance



In article <20160324125858.516aa36bc70bddd1b180e8be%schemamania.org@localhost>,
James K. Lowden <tech-userlevel%netbsd.org@localhost> wrote:
>On Thu, 24 Mar 2016 14:29:39 +0000 (UTC)
>mlelstv%serpens.de@localhost (Michael van Elst) wrote:
>
>> >(I agree that calling strerror(0) is odd and a likely sign of a bug,
>> >but that's separate from complying with standards when compliance
>> >isn't harmful.)
>> 
>> Well, if calling strerror(0) is a bug, then usually because it's
>> called after an error condition that didn't set errno.
>
>Has that happened to you?  I feel sure that's never happened to me, but
>maybe I'm less adventurous.  
>
>IMO it's incorrect to say 0 is an "undefined error".  It's defined as
>success by nearly every syscall.  The emitted string should reflect
>that, even if [sic] Posix says so.  Perhaps, 

Actually not; syscalls set errno on error, don't clear errno on success [1].
Thus it is incorrect code to test errno if the syscall succeeded. It shows
nothing, try:

#include <stdio.h>
#include <fcntl.h>
#include <errno.h>

int
main(void)
{
        errno = 3;
        open("/", O_RDONLY);
        printf("%d\n", errno);
        return 0;
}

[1] There is at least one syscall stub that clears errno before the syscall
which I find bogus; can you guess which one?


christos



Home | Main Index | Thread Index | Old Index