Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl(3) setting `odd' errno's
Garrett Cooper yanef...@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 - d...@des.no ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl(3) setting `odd' errno's
Hi Garrett, On Fri, 16 Jan 2009, Garrett Cooper wrote: On Fri, Jan 16, 2009 at 12:58 AM, Garrett Cooper yanef...@gmail.com wrote: On Fri, Jan 16, 2009 at 12:57 AM, Christoph Mallon christoph.mal...@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; } [gcoo...@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-am...@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-amd64 To unsubscribe, send any mail to freebsd-amd64-unsubscr...@freebsd.org ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl(3) setting `odd' errno's
On Fri, Jan 16, 2009 at 12:41 AM, Garrett Cooper yanef...@gmail.com wrote: 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 /* 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)); } printf(%lu\n, len); return 0; } # output for len preset to 0: [gcoo...@optimus ~]$ ./test2 Errno: 12 test2: Segmentation fault: 11 (core dumped) [gcoo...@optimus ~]$ uname -a FreeBSD optimus.gateway.2wire.net 8.0-CURRENT FreeBSD 8.0-CURRENT #4: Sun Jan 11 12:30:31 PST 2009 r...@optimus.gateway.2wire.net:/usr/obj/usr/src/sys/OPTIMUS amd64 [gcoo...@orangebox /usr/home/gcooper]$ ./test Errno: 12 test: Error: Cannot allocate memory [gcoo...@orangebox /usr/home/gcooper]$ uname -a FreeBSD orangebox.gateway.2wire.net 8.0-CURRENT FreeBSD 8.0-CURRENT #4: Sat Jan 3 22:54:52 PST 2009 gcoo...@orangebox.gateway.2wire.net:/usr/obj/usr/src/sys/ORANGEBOX i386 # output for len not preset to 0: [gcoo...@optimus ~]$ ./test2 Errno: 14 test2: Segmentation fault: 11 (core dumped) Almost forgot -- here are the actual values reported by sysctl(1), just for reference: [gcoo...@optimus ~]$ sysctl kern.ipc.shmall kern.ipc.shmmin kern.ipc.shmmax kern.ipc.shmall: 8192 kern.ipc.shmmin: 1 kern.ipc.shmmax: 33554432 [gcoo...@orangebox /usr/src/sys]$ sysctl kern.ipc.shmall kern.ipc.shmmin kern.ipc.shmmax kern.ipc.shmall: 8192 kern.ipc.shmmin: 1 kern.ipc.shmmax: 33554432 Thanks, -Garrett ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl(3) setting `odd' errno's
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. ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl(3) setting `odd' errno's
On Fri, Jan 16, 2009 at 12:47 AM, Jacques Fourie jacques.fou...@gmail.com wrote: You need to initialize len to the number of entries in the mib array. Try adding 'len = 4' before calling sysctlnametomib() and see if your issues go away. Ok, that solution works (I think). So, problem 2 down. Now: what about the segfaulting strerror(3) call on amd64 ;\? -Garrett ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl(3) setting `odd' errno's
On Fri, 16 Jan 2009, 00:44-0800, Garrett Cooper wrote: On Fri, Jan 16, 2009 at 12:41 AM, Garrett Cooper yanef...@gmail.com wrote: 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? - size_t len; + size_t len = 4; -- Maxim Konovalov ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl(3) setting `odd' errno's
On Fri, Jan 16, 2009 at 10:47 AM, Jacques Fourie jacques.fou...@gmail.com wrote: On Fri, Jan 16, 2009 at 10:44 AM, Garrett Cooper yanef...@gmail.com wrote: On Fri, Jan 16, 2009 at 12:41 AM, Garrett Cooper yanef...@gmail.com wrote: 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 /* 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)); } printf(%lu\n, len); return 0; } # output for len preset to 0: [gcoo...@optimus ~]$ ./test2 Errno: 12 test2: Segmentation fault: 11 (core dumped) [gcoo...@optimus ~]$ uname -a FreeBSD optimus.gateway.2wire.net 8.0-CURRENT FreeBSD 8.0-CURRENT #4: Sun Jan 11 12:30:31 PST 2009 r...@optimus.gateway.2wire.net:/usr/obj/usr/src/sys/OPTIMUS amd64 [gcoo...@orangebox /usr/home/gcooper]$ ./test Errno: 12 test: Error: Cannot allocate memory [gcoo...@orangebox /usr/home/gcooper]$ uname -a FreeBSD orangebox.gateway.2wire.net 8.0-CURRENT FreeBSD 8.0-CURRENT #4: Sat Jan 3 22:54:52 PST 2009 gcoo...@orangebox.gateway.2wire.net:/usr/obj/usr/src/sys/ORANGEBOX i386 # output for len not preset to 0: [gcoo...@optimus ~]$ ./test2 Errno: 14 test2: Segmentation fault: 11 (core dumped) Almost forgot -- here are the actual values reported by sysctl(1), just for reference: [gcoo...@optimus ~]$ sysctl kern.ipc.shmall kern.ipc.shmmin kern.ipc.shmmax kern.ipc.shmall: 8192 kern.ipc.shmmin: 1 kern.ipc.shmmax: 33554432 [gcoo...@orangebox /usr/src/sys]$ sysctl kern.ipc.shmall kern.ipc.shmmin kern.ipc.shmmax kern.ipc.shmall: 8192 kern.ipc.shmmin: 1 kern.ipc.shmmax: 33554432 Thanks, -Garrett ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org You need to initialize len to the number of entries in the mib array. Try adding 'len = 4' before calling sysctlnametomib() and see if your issues go away. Sorry, I only scanned through the code without reading the whole message before replying :) Please ignore... ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl(3) setting `odd' errno's
On Fri, Jan 16, 2009 at 12:57 AM, Christoph Mallon christoph.mal...@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 ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl(3) setting `odd' errno's
On Fri, Jan 16, 2009 at 10:44 AM, Garrett Cooper yanef...@gmail.com wrote: On Fri, Jan 16, 2009 at 12:41 AM, Garrett Cooper yanef...@gmail.com wrote: 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 /* 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)); } printf(%lu\n, len); return 0; } # output for len preset to 0: [gcoo...@optimus ~]$ ./test2 Errno: 12 test2: Segmentation fault: 11 (core dumped) [gcoo...@optimus ~]$ uname -a FreeBSD optimus.gateway.2wire.net 8.0-CURRENT FreeBSD 8.0-CURRENT #4: Sun Jan 11 12:30:31 PST 2009 r...@optimus.gateway.2wire.net:/usr/obj/usr/src/sys/OPTIMUS amd64 [gcoo...@orangebox /usr/home/gcooper]$ ./test Errno: 12 test: Error: Cannot allocate memory [gcoo...@orangebox /usr/home/gcooper]$ uname -a FreeBSD orangebox.gateway.2wire.net 8.0-CURRENT FreeBSD 8.0-CURRENT #4: Sat Jan 3 22:54:52 PST 2009 gcoo...@orangebox.gateway.2wire.net:/usr/obj/usr/src/sys/ORANGEBOX i386 # output for len not preset to 0: [gcoo...@optimus ~]$ ./test2 Errno: 14 test2: Segmentation fault: 11 (core dumped) Almost forgot -- here are the actual values reported by sysctl(1), just for reference: [gcoo...@optimus ~]$ sysctl kern.ipc.shmall kern.ipc.shmmin kern.ipc.shmmax kern.ipc.shmall: 8192 kern.ipc.shmmin: 1 kern.ipc.shmmax: 33554432 [gcoo...@orangebox /usr/src/sys]$ sysctl kern.ipc.shmall kern.ipc.shmmin kern.ipc.shmmax kern.ipc.shmall: 8192 kern.ipc.shmmin: 1 kern.ipc.shmmax: 33554432 Thanks, -Garrett ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org You need to initialize len to the number of entries in the mib array. Try adding 'len = 4' before calling sysctlnametomib() and see if your issues go away. ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl(3) setting `odd' errno's
On Fri, Jan 16, 2009 at 12:58 AM, Garrett Cooper yanef...@gmail.com wrote: On Fri, Jan 16, 2009 at 12:57 AM, Christoph Mallon christoph.mal...@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: #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; } [gcoo...@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-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl(3) setting `odd' errno's
Garrett Cooper schrieb: On Fri, Jan 16, 2009 at 12:58 AM, Garrett Cooper yanef...@gmail.com wrote: On Fri, Jan 16, 2009 at 12:57 AM, Christoph Mallon christoph.mal...@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: #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)); You are still using the wrong errno. Also err() itself prints the error string using strerror(). There might be some interference when the result of one call to strerror() (your call) is used after another call to strerror() (err() internally). I doubt there is a bug in the library, otherwise we would see many bugreports of segfaults on AMD64. ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl (3) setting `odd' errno's
On Friday 16 January 2009 09:53, Christoph Mallon wrote: 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. I don't think printf() can set errno. And even if it could, it wouldn't matter, because C has call-by-value semantics. Christian ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl (3) setting `odd' errno's
Christian Kandeler schrieb: On Friday 16 January 2009 09:53, Christoph Mallon wrote: 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. I don't think printf() can set errno. And even if it could, it Of course it can. See ISO/IEC 9899:1999 (E) §7.5:3. wouldn't matter, because C has call-by-value semantics. This has nothing to do with call-by-value. errno is read (even twice!) *after* the call to printf(). ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl (3) setting `odd' errno's
On Fri, Jan 16, 2009 at 2:21 AM, Christoph Mallon christoph.mal...@gmx.de wrote: Christian Kandeler schrieb: On Friday 16 January 2009 09:53, Christoph Mallon wrote: 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. I don't think printf() can set errno. And even if it could, it Of course it can. See ISO/IEC 9899:1999 (E) §7.5:3. wouldn't matter, because C has call-by-value semantics. This has nothing to do with call-by-value. errno is read (even twice!) *after* the call to printf(). Ok, I just installworld'ed, recompiled the program with the following modifications, and I still get segfaults. And the question of the night is: why amd64 on a VERY recent CURRENT? I'm going to try the same app on an amd64 freebsd VMware instance with RELENG_7. Remember: just because a bunch of other people aren't reporting issues with CURRENT/amd64 doesn't mean that it isn't environmental, related to my hardware or compile options ;). Cheers, -Garrett #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); printf(%s\n, strerror(o_errno)); } return 0; } #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); printf(%s\n, strerror(o_errno)); } return 0; } [gcoo...@optimus ~]$ gcc -o badfile badfile.c [gcoo...@optimus ~]$ ./badfile Errno: 2 Segmentation fault: 11 (core dumped) [gcoo...@optimus ~]$ ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl (3) setting `odd' errno's
On Fri, Jan 16, 2009 at 2:35 AM, Garrett Cooper yanef...@gmail.com wrote: On Fri, Jan 16, 2009 at 2:21 AM, Christoph Mallon christoph.mal...@gmx.de wrote: Christian Kandeler schrieb: On Friday 16 January 2009 09:53, Christoph Mallon wrote: 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. I don't think printf() can set errno. And even if it could, it Of course it can. See ISO/IEC 9899:1999 (E) §7.5:3. wouldn't matter, because C has call-by-value semantics. This has nothing to do with call-by-value. errno is read (even twice!) *after* the call to printf(). Ok, I just installworld'ed, recompiled the program with the following modifications, and I still get segfaults. And the question of the night is: why amd64 on a VERY recent CURRENT? I'm going to try the same app on an amd64 freebsd VMware instance with RELENG_7. Remember: just because a bunch of other people aren't reporting issues with CURRENT/amd64 doesn't mean that it isn't environmental, related to my hardware or compile options ;). Cheers, -Garrett Ugh... I pasted it twice by accident. Sorry. -Garrett ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl (3) setting `odd' errno's
Garrett Cooper schrieb: Ok, I just installworld'ed, recompiled the program with the following modifications, and I still get segfaults. And the question of the night is: why amd64 on a VERY recent CURRENT? I'm going to try the same app on an amd64 freebsd VMware instance with RELENG_7. Remember: just because a bunch of other people aren't reporting issues with CURRENT/amd64 doesn't mean that it isn't environmental, related to my hardware or compile options ;). Cheers, -Garrett #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); printf(%s\n, strerror(o_errno)); } return 0; } #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); printf(%s\n, strerror(o_errno)); } return 0; } [gcoo...@optimus ~]$ gcc -o badfile badfile.c [gcoo...@optimus ~]$ ./badfile Errno: 2 Segmentation fault: 11 (core dumped) [gcoo...@optimus ~]$ Well, compile with -g, start in gdb, check what value is wrong, the usual stuff. Probably the return value of strerror() is interesting. ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl (3) setting `odd' errno's
Garrett Cooper schrieb: On Fri, Jan 16, 2009 at 2:21 AM, Christoph Mallon christoph.mal...@gmx.de wrote: Christian Kandeler schrieb: On Friday 16 January 2009 09:53, Christoph Mallon wrote: 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. I don't think printf() can set errno. And even if it could, it Of course it can. See ISO/IEC 9899:1999 (E) §7.5:3. wouldn't matter, because C has call-by-value semantics. This has nothing to do with call-by-value. errno is read (even twice!) *after* the call to printf(). Ok, I just installworld'ed, recompiled the program with the following modifications, and I still get segfaults. And the question of the night is: why amd64 on a VERY recent CURRENT? I'm going to try the same app on an amd64 freebsd VMware instance with RELENG_7. Remember: just because a bunch of other people aren't reporting issues with CURRENT/amd64 doesn't mean that it isn't environmental, related to my hardware or compile options ;). Cheers, -Garrett #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); printf(%s\n, strerror(o_errno)); } return 0; } #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); printf(%s\n, strerror(o_errno)); } return 0; } [gcoo...@optimus ~]$ gcc -o badfile badfile.c [gcoo...@optimus ~]$ ./badfile Errno: 2 Segmentation fault: 11 (core dumped) [gcoo...@optimus ~]$ Compile with -Wall (you ALWAYS should do that) and then you'll see what the problem is. ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl (3) setting `odd' errno's
Garrett Cooper schrieb: Ok, I just installworld'ed, recompiled the program with the following modifications, and I still get segfaults. And the question of the night is: why amd64 on a VERY recent CURRENT? I'm going to try the same app on an amd64 freebsd VMware instance with RELENG_7. Remember: just because a bunch of other people aren't reporting issues with CURRENT/amd64 doesn't mean that it isn't environmental, related to my hardware or compile options ;). Cheers, -Garrett #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); printf(%s\n, strerror(o_errno)); } return 0; } #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); printf(%s\n, strerror(o_errno)); } return 0; } [gcoo...@optimus ~]$ gcc -o badfile badfile.c [gcoo...@optimus ~]$ ./badfile Errno: 2 Segmentation fault: 11 (core dumped) [gcoo...@optimus ~]$ Well, compile with -g, start in gdb, check what value is wrong, the usual stuff. Probably the return value of strerror() is interesting. some facts: #include stdio.h int main() { printf(%s\n, strerror(2)); return 0; } 1- it works fine on i386 2- it bombs on amd64 3- with a local strerror.c (instead of the one in libc) works fine so, there is something realy wrong going on here! (and it gows back to at least 7.0-stable) danny ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl (3) setting `odd' errno's
Danny Braniss schrieb: some facts: #include stdio.h int main() { printf(%s\n, strerror(2)); return 0; } 1- it works fine on i386 2- it bombs on amd64 3- with a local strerror.c (instead of the one in libc) works fine so, there is something realy wrong going on here! (and it gows back to at least 7.0-stable) No, everything is perfectly correct. I suggested this earlier: Compile with -Wall and you'll see what the problem is. ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl (3) setting `odd' errno's
On Fri, Jan 16, 2009 at 01:33:38PM +0200, Danny Braniss wrote: some facts: #include stdio.h int main() { printf(%s\n, strerror(2)); return 0; } 1- it works fine on i386 2- it bombs on amd64 3- with a local strerror.c (instead of the one in libc) works fine so, there is something realy wrong going on here! (and it gows back to at least 7.0-stable) The compiler thinks strerror returns an int. Include string.h. ahh, RTFM ALL THE WAY! I just saw the top few lines: LIBRARY Standard C Library (libc, -lc) SYNOPSIS #include stdio.h but later it shows: #include string.h char * strerror(int errnum); on the other hand, compiling with -static workes ok, which sent me on the wrong trail. danny ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl (3) setting `odd' errno's
On Fri, Jan 16, 2009 at 01:33:38PM +0200, Danny Braniss wrote: some facts: #include stdio.h int main() { printf(%s\n, strerror(2)); return 0; } 1- it works fine on i386 2- it bombs on amd64 3- with a local strerror.c (instead of the one in libc) works fine so, there is something realy wrong going on here! (and it gows back to at least 7.0-stable) The compiler thinks strerror returns an int. Include string.h. ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl (3) setting `odd' errno's
Le Friday 16 January 2009, Garrett Cooper a écrit : On Fri, Jan 16, 2009 at 2:21 AM, Christoph Mallon #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); printf(%s\n, strerror(o_errno)); } return 0; } with this, it's better on an amd64/ RELENG_7 machine : % diff -ub badfile.c.ori badfile.c --- badfile.c.ori 2009-01-16 11:49:44.778991057 +0100 +++ badfile.c 2009-01-16 11:49:03.470465677 +0100 @@ -1,6 +1,7 @@ #include errno.h #include stdio.h #include sys/stat.h +#include string.h int main() Cheers TfH ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl (3) setting `odd' errno's
On Fri, Jan 16, 2009 at 2:52 AM, Thierry Herbelot thierry.herbe...@free.fr wrote: Le Friday 16 January 2009, Garrett Cooper a écrit : On Fri, Jan 16, 2009 at 2:21 AM, Christoph Mallon #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); printf(%s\n, strerror(o_errno)); } return 0; } with this, it's better on an amd64/ RELENG_7 machine : % diff -ub badfile.c.ori badfile.c --- badfile.c.ori 2009-01-16 11:49:44.778991057 +0100 +++ badfile.c 2009-01-16 11:49:03.470465677 +0100 @@ -1,6 +1,7 @@ #include errno.h #include stdio.h #include sys/stat.h +#include string.h int main() Cheers TfH That's hilarious -- why does it pass though without issue on x86 though? -Garrett ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl (3) setting `odd' errno's
On Fri, 16 Jan 2009, Garrett Cooper wrote: On Fri, Jan 16, 2009 at 2:52 AM, Thierry Herbelot thierry.herbe...@free.fr wrote: Le Friday 16 January 2009, Garrett Cooper a écrit : On Fri, Jan 16, 2009 at 2:21 AM, Christoph Mallon #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); printf(%s\n, strerror(o_errno)); } return 0; } with this, it's better on an amd64/ RELENG_7 machine : % diff -ub badfile.c.ori badfile.c --- badfile.c.ori 2009-01-16 11:49:44.778991057 +0100 +++ badfile.c 2009-01-16 11:49:03.470465677 +0100 @@ -1,6 +1,7 @@ #include errno.h #include stdio.h #include sys/stat.h +#include string.h int main() Cheers TfH That's hilarious -- why does it pass though without issue on x86 though? -Garrett As pointed out, when you don't have a declaration for strerror, it's implicitly assumed to return `int'. This feature was widely used in the early days of C and so continues to be accepted by compilers, and gcc by default doesn't warn about it. On x86, int and char * are the same size. So even though the compiler thinks strerror is returning an int which is being passed to printf, the code it generates is the same as for a char *. On amd64, int is 32 bits but char * is 64. When the compiler thinks it's using int, it only keeps track of the lower 32 bits, and the upper 32 bits get zeroed. So the pointer that printf receives has had its upper 32 bits zeroed, and no longer points where it should. Hence segfault. Since running on amd64 I've seen a lot of bugs where people carelessly assume (perhaps without noticing) that ints and pointers are practically interchangeable, which works on x86 and the like but breaks on amd64. Variadic functions are special offenders because the compiler can't do much type checking. Pop quiz: which of the following statements is correct? #include stdlib.h #include unistd.h execl(/bin/sh, /bin/sh, 0); execl(/bin/sh, /bin/sh, NULL); -- Nate Eldredge neldre...@math.ucsd.edu___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl (3) setting `odd' errno's
On Fri, Jan 16, 2009 at 10:33:15AM -0800, Nate Eldredge wrote: Pop quiz: which of the following statements is correct? #include stdlib.h #include unistd.h execl(/bin/sh, /bin/sh, 0); execl(/bin/sh, /bin/sh, NULL); None, as NULL is allowed to expand to 0. You have to write execl(/bin/sh, /bin/sh, (char *)0); ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org