Checking return values (was: Trojan Horses, NFS, etc.)

Jim Balter jim at segue.segue.com
Wed Oct 31 11:18:44 AEST 1990


In article <27201 at mimsy.umd.edu> chris at mimsy.umd.edu (Chris Torek) writes:
[ stuff about strerror, including an implementation ]

If you use strerror to implement perror, beware of a subtle trap:

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

int main(void)
{
	char *	p;

	p = strerror(31415);	/* presumably unknown error */
	errno = 27182;		/* ditto */
	perror("");
	printf("%s\n", p);	
}


A naive implementation will print 27182 twice, which is wrong, because ANSI
says that the string returned by strerror may be overwritten by a subsequent
call of strerror, but it doesn't say that perror may overwrite it or that
perror may call strerror.  This and many other gotchas were known to the
committee, but unfortunately they didn't provide an implementers' guide
enumerating them.



More information about the Comp.unix.programmer mailing list