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

Mikko Työläjärvi mbsd at pacbell.net
Sat Jan 17 14:16:29 PST 2009


Hi Garrett,

On Fri, 16 Jan 2009, Garrett Cooper wrote:

> On Fri, Jan 16, 2009 at 12:58 AM, Garrett Cooper <yanefbsd at gmail.com> wrote:
>> On Fri, Jan 16, 2009 at 12:57 AM, Christoph Mallon
>> <christoph.mallon at gmx.de> wrote:
>>> Garrett Cooper schrieb:
>>>>
>>>> Good point. I modified the source to do that.
>>>> Thanks,
>>>> -Garrett
>>>
>>> You should reply to all so the discussion stays on the list.
>>
>> Yeah, that was a goofup on my part. Go-go Gmail web interface!
>> -Garrett
>
> Hmmm... looks like the strerror issue it could be a serious bug:

Add #include <string.h>.

Without it you don't get the strerror() prototype, so the return value
defaults to an int. Thus the compiler will truncate the pointer value
to junk.  The crash happens when formatting the output.

Compile with -Wall and pay attention to warnings (or use -Werror)
to catch these things.

     $.02,
     /Mikko

>
> #include <errno.h>
> #include <stdio.h>
> #include <sys/stat.h>
>
> int
> main()
> {
>
>        struct stat sb;
>
>        int o_errno;
>
>        if (stat("/some/file/that/doesn't/exist", &sb) != 0) {
>                o_errno = errno;
>                printf("Errno: %d\n", errno);
>                err(errno, "%s", strerror(o_errno));
>        }
>
>        return 0;
>
> }
>
> [gcooper at optimus ~]$ ./badfile
> Errno: 2
> badfile: Segmentation fault: 11 (core dumped)
>
>    I rebuilt my kernel and installed it, and I rebuilt world, but
> haven't installed it yet though, so let me reboot the amd64 machine
> and see what happens (may be a mismatched ABI issue)...
> Cheers,
> -Garrett
> _______________________________________________
> freebsd-amd64 at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-amd64
> To unsubscribe, send any mail to "freebsd-amd64-unsubscribe at freebsd.org"
>


More information about the freebsd-hackers mailing list