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

From: Dag-Erling Smørgrav <des_at_des.no>
Date: Tue, 13 Mar 2007 14:23:53 +0100
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
Received on Tue Mar 13 2007 - 12:55:48 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:39:06 UTC