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

From: Andrey Chernov <ache_at_freebsd.org>
Date: Tue, 13 Mar 2007 15:11:07 +0300
Copy the segment below to the file a.c
---------------------- cut me here ---------------------
#include <stdio.h>

main() {
printf("%s\n", NULL);
}
---------------------- cut me here ---------------------
Compile first as
cc a.c
./a.out
got
(null)
Then compile as
cc -O a.c
./a.out
got core dump.
Lets see assembler output from
cc -O -S a.c
	.file	"a.c"
	.text
	.p2align 2,,3
.globl main
	.type	main, _at_function
main:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$8, %esp
	andl	$-16, %esp
	subl	$28, %esp
	pushl	$0
	call	puts
	leave
	ret
	.size	main, .-main
	.ident	"GCC: (GNU) 3.4.6 [FreeBSD] 20060825"
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?

-- 
http://ache.pp.ru/
Received on Tue Mar 13 2007 - 11:11:10 UTC

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