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

Bruce Evans bde at
Tue Mar 13 13:05:34 UTC 2007

On Tue, 13 Mar 2007, Andrey Chernov wrote:

> 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?

This happens with gcc-3.4.6 and 4.2 but not with 3.3.3.  It also
happens if NULL is replaced by a variable containing a null pointer.

The case of a literal NULL should probably be an error at compile
time (__nonnull() doesn't apply to printf() but the compiler could
detect this error when it optimizes to use puts()).

This is not wrong, since the null pointer gives undefined behaviour,
but it breaks the normal undefined behaviour of printing "(null)".


More information about the freebsd-bugs mailing list