Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl(3) setting `odd' errno's

Dag-Erling Smørgrav des at des.no
Sat Jan 17 05:23:51 PST 2009


Garrett Cooper <yanefbsd at gmail.com> writes:
> #include <err.h>
> #include <errno.h>
> #include <stdio.h>
> #include <sys/types.h>
> #include <sys/sysctl.h>

You should always put your sys includes before your non-sys includes,
and in any case, <sys/types.h> should always come first.

>                 printf("Errno: %d\n", errno);
>                 errx(errno, "Error: %s", strerror(errno));

In addition to what everybody else said, errno is not an appropriate
value for errx's first argument.  Use 1 or EXIT_FAILURE (or one of the
macros defined in <sysexits.h>, but I wouldn't recommend it).  Also, you
probably want to use err(), not errx(), and *always* compile with -Wall
-Wextra, and unless you're going to run gdb on your program, -O2 (which
enables additional code analysis)

DES
-- 
Dag-Erling Smørgrav - des at des.no


More information about the freebsd-hackers mailing list