Bad gcc -O optimization cause core dump. What to do?

Dag-Erling Smørgrav des at des.no
Tue Mar 13 13:55:48 UTC 2007


Andrey Chernov <ache at freebsd.org> writes:
> Copy the segment below to the file a.c
> ---------------------- cut me here ---------------------
> #include <stdio.h>
>
> main() {
> printf("%s\n", NULL);
> }
> ---------------------- cut me here ---------------------
> [...]
> It calls "puts(NULL)" with core dump.
> It means "printf("%s\n", NULL)" is overoptimized.
> BTW, things like "printf("1%s\n", NULL)" are not overoptimized.
> Any ideas? Is it right or needs to be fixed?

The behaviour of printf("%s\n", NULL) is undefined.  GCC is perfectly
within its rights to translate it into something that dumps core (or
causes your disk to crash, your monitor to explode, your dog to die of
a venereal disease, and demons to fly out of your nose)

Specifically, the C standard (§7.19.6.1) requires the argument that
corresponds to %s to be a pointer to "the initial element of an array
of character type", which NULL is not.

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


More information about the freebsd-current mailing list