Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 14 Mar 2007 00:05:29 +1100 (EST)
From:      Bruce Evans <bde@zeta.org.au>
To:        Andrey Chernov <ache@freebsd.org>
Cc:        bugs@freebsd.org, ache@nagual.pp.ru, current@freebsd.org
Subject:   Re: Bad gcc -O optimization cause core dump. What to do?
Message-ID:  <20070314000017.Y52372@delplex.bde.org>
In-Reply-To: <20070313121106.GA96293@nagual.pp.ru>
References:  <20070313121106.GA96293@nagual.pp.ru>

next in thread | previous in thread | raw e-mail | index | archive | help
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)".

Bruce



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