From owner-freebsd-current@FreeBSD.ORG Tue Mar 13 14:38:36 2007 Return-Path: X-Original-To: freebsd-current@FreeBSD.ORG Delivered-To: freebsd-current@FreeBSD.ORG Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 690C116A401; Tue, 13 Mar 2007 14:38:36 +0000 (UTC) (envelope-from mux@freebsd.org) Received: from elvis.mu.org (elvis.mu.org [192.203.228.196]) by mx1.freebsd.org (Postfix) with ESMTP id 4CEB713C448; Tue, 13 Mar 2007 14:38:36 +0000 (UTC) (envelope-from mux@freebsd.org) Received: by elvis.mu.org (Postfix, from userid 1920) id C0E021A4D88; Tue, 13 Mar 2007 07:06:32 -0700 (PDT) Date: Tue, 13 Mar 2007 15:06:32 +0100 From: Maxime Henrion To: Ricardo Nabinger Sanchez Message-ID: <20070313140632.GK65356@elvis.mu.org> References: <20070313121106.GA96293@nagual.pp.ru> <20070313101312.71d35c32.rnsanchez@wait4.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20070313101312.71d35c32.rnsanchez@wait4.org> User-Agent: Mutt/1.4.2.2i Cc: freebsd-bugs@FreeBSD.ORG, freebsd-current@FreeBSD.ORG Subject: Re: Bad gcc -O optimization cause core dump. What to do? X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 13 Mar 2007 14:38:36 -0000 Ricardo Nabinger Sanchez wrote: > On Tue, 13 Mar 2007 15:11:07 +0300 > Andrey Chernov wrote: > > > cc -O -S a.c > > .file "a.c" > > .text > > .p2align 2,,3 > > .globl main > > .type main, @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" > > Confirmed on FreeBSD-6.1 RELEASE: > > .file "bla.c" > .text > .p2align 2,,3 > .globl main > .type main, @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.4 [FreeBSD] 20050518" > > > 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? > > Given that this is not what the user asked (replacing printf with puts), I > consider this a bug. GCC made its assumption, and it was incorrect--it's not > user's fault. GCC can do whatever it wants here, even printing "foobar42", because the C standard says that passing a NULL pointer to a %s format will yield undefined behaviour. It *is* user's fault to have passed NULL to printf() in the first place. So, while we could argue that GCC's behaviour here is useless, annoying, etc, this just can't be called a bug in GCC. As a side note, these "optimizations" are in place since a *long* time now. Cheers, Maxime