Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 16 Jan 2009 09:53:00 +0100
From:      Christoph Mallon <christoph.mallon@gmx.de>
To:        Garrett Cooper <yanefbsd@gmail.com>
Cc:        "amd64@freebsd.org" <amd64@freebsd.org>, Hackers freeBSD <freebsd-hackers@freebsd.org>
Subject:   Re: Confused by segfault with legitimate call to strerror(3) on amd64 /	sysctl(3) setting `odd' errno's
Message-ID:  <49704AEC.3080709@gmx.de>
In-Reply-To: <7d6fde3d0901160041n55466290l55f737d274a40895@mail.gmail.com>
References:  <7d6fde3d0901160041n55466290l55f737d274a40895@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Garrett Cooper schrieb:
> Hi amd64 and Hackers,
>     Uh, I'm really confused why 1) this error (errno => ENOMEM) would
> occur when I have more than enough free memory (both on x86 and amd64)
> and 2) why strerror would segfault in the call to errx in the attached
> sourcefile on amd64 only. Not initializing len causes the second
> output sample (errno => 14, which is EFAULT).
>     Any ideas?
>     Please CC me if mailing on amd64@ as I'm not subscribed to the list.
> Thanks,
> -Garrett

len is not uninitialised. This leads to undefined behaviour. Anything 
can happen. Probably the syscall overwrites parts of the stack because 
len has some (random) high value.

> /* Program */
> #include <err.h>
> #include <errno.h>
> #include <stdio.h>
> #include <sys/types.h>
> #include <sys/sysctl.h>
> 
> int
> main() {
> 
>         int mib[4];
> 
>         size_t len;
> 
>         if (sysctlnametomib("kern.ipc.shmmax", mib, &len) != 0) {
>                 printf("Errno: %d\n", errno);
>                 errx(errno, "Error: %s", strerror(errno));

The use of errno is wrong. printf might change errno. Store the errno 
into a local variable before you do any call, which might modify it.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?49704AEC.3080709>