In the provided code, *dpb++ = strlen(userName) dpb is incorrectly incremented by two bytes, not one. The output buffer should result in: 1, 35, 6, 'sysdba'. Instead it results in: 1, 35, 6, x, 'sysdba' where x is random. Tracing in gdb shows that dpb is incremented by a value of two. Release: gcc 3.2 Environment: mandrake linux 9.0 How-To-Repeat: g++ -O2 -march=i686 -fPIC test1.cpp ./a.out 1
Fix: -O1 seems to work.
From: "Mark O'Donohue" <mark.odonohue@cytopia.com.au> To: gcc-gnats@gcc.gnu.org, gcc-prs@gcc.gnu.org, mark.odonohue@cytopia.com.au, gcc-bugs@gcc.gnu.org, nobody@gcc.gnu.org Cc: Subject: Re: optimization/8613: -O3 optimisation of ++ generates wrong code Date: Mon, 18 Nov 2002 12:28:45 +1100 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8613 confirmed on redhat 8.0 Also it seem tied up with i686 architecture. Since on redhat 8.0 gcc 3.2: g++ -O2 test1.cpp works: and on mandrak9.0 gcc 3.2 g++ -O2 -march=i386 test1.cpp also works. Mark
State-Changed-From-To: open->analyzed State-Changed-Why: Confirmed on all versions since gcc 3.0: simply compile with -O2 -mcpu=[456]86. Simplified C testcase attached.
From: Glen Nakamura <glen@imodulo.com> To: gcc-gnats@gcc.gnu.org, gcc-bugs@gcc.gnu.org, mark.odonohue@cytopia.com.au Cc: Subject: Re: optimization/8613: [3.2/3.3/3.4 regression] -O2 optimization generates wrong code Date: Tue, 18 Feb 2003 23:10:09 -1000 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8613 I've tracked this bug down to a problem with postincrements and the strlen builtin function. What happens is that the postincrement code isn't emitted before the builtin strlen function is expanded and gets emitted within the loop created by ix86_expand_strlensi_unroll_1. This causes the postincrement code to be executed multiple times when calculating the string length. The fix is to call emit_queue before expanding the strlen builtin so the postincrement code is emitted outside of the loop. I'm not exactly sure where the emit_queue call should be placed. Although adding emit_queue to expand_builtin_strlen would work, I added it to expand_builtin instead, because other builtins may have the same problem. It passed regression testing on i686-pc-linux-gnu for the 3.3 branch, but a more experienced developer should decide where the best place is. Here is a reduced testcase: extern void abort (void); int main () { char buf[16] = "1234567890"; char *p = buf; *p++ = (char) __builtin_strlen (buf); if ((buf[0] != 10) || (p - buf != 1)) abort (); } And here is the patch: 2003-02-19 Glen Nakamura <glen@imodulo.com> * builtins.c (expand_builtin): Emit postincrements before expanding builtin functions. diff -Nru3p gcc-3.3.orig/gcc/builtins.c gcc-3.3/gcc/builtins.c --- gcc-3.3.orig/gcc/builtins.c 2002-12-01 17:51:43.000000000 +0000 +++ gcc-3.3/gcc/builtins.c 2002-12-01 17:51:43.000000000 +0000 @@ -3691,6 +3691,9 @@ expand_builtin (exp, target, subtarget, tree arglist = TREE_OPERAND (exp, 1); enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); + /* Perform postincrements before expanding builtin functions. */ + emit_queue (); + if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) return (*targetm.expand_builtin) (exp, target, subtarget, mode, ignore);
From: Glen Nakamura <glen@imodulo.com> To: gcc-gnats@gcc.gnu.org, gcc-bugs@gcc.gnu.org Cc: Subject: Re: optimization/8613: [3.2/3.3/3.4 regression] -O2 optimization generates wrong code Date: Thu, 20 Feb 2003 09:39:43 -1000 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8613 Someone with write access should checkin this patch: http://gcc.gnu.org/ml/gcc-patches/2003-02/msg01531.html Patch approval is here: http://gcc.gnu.org/ml/gcc-patches/2003-02/msg01578.html FYI, I've only regtested this patch on the 3.3 branch.
From: Eric Botcazou <ebotcazou@libertysurf.fr> To: Glen Nakamura <glen@imodulo.com> Cc: gcc-gnats@gcc.gnu.org, gcc-bugs@gcc.gnu.org Subject: Re: optimization/8613: [3.2/3.3/3.4 regression] -O2 optimization generates wrong code Date: Thu, 20 Feb 2003 20:56:39 +0100 > Someone with write access should checkin this patch: > http://gcc.gnu.org/ml/gcc-patches/2003-02/msg01531.html I'll do. > FYI, I've only regtested this patch on the 3.3 branch. I'm going to bootstrap/regtest it on the 3.2 branch and commit it there too if everything goes well. -- Eric Botcazou
From: ebotcazou@gcc.gnu.org To: gcc-gnats@gcc.gnu.org Cc: Subject: optimization/8613 Date: 21 Feb 2003 08:07:25 -0000 CVSROOT: /cvs/gcc Module name: gcc Changes by: ebotcazou@gcc.gnu.org 2003-02-21 08:07:25 Modified files: gcc : ChangeLog builtins.c gcc/testsuite : ChangeLog Added files: gcc/testsuite/gcc.c-torture/execute: 20030221-1.c Log message: PR optimization/8613 * builtins.c (expand_builtin): Emit postincrements before expanding builtin functions. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=1.16804&r2=1.16805 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/builtins.c.diff?cvsroot=gcc&r1=1.177&r2=1.178 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.2427&r2=1.2428 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/20030221-1.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
From: ebotcazou@gcc.gnu.org To: gcc-gnats@gcc.gnu.org Cc: Subject: optimization/8613 Date: 21 Feb 2003 08:13:01 -0000 CVSROOT: /cvs/gcc Module name: gcc Branch: gcc-3_3-branch Changes by: ebotcazou@gcc.gnu.org 2003-02-21 08:13:01 Modified files: gcc : ChangeLog builtins.c gcc/testsuite : ChangeLog Added files: gcc/testsuite/gcc.c-torture/execute: 20030221-1.c Log message: PR optimization/8613 * builtins.c (expand_builtin): Emit postincrements before expanding builtin functions. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.16114.2.211&r2=1.16114.2.212 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/builtins.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.165&r2=1.165.2.1 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.2261.2.53&r2=1.2261.2.54 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/20030221-1.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.2.1
From: ebotcazou@gcc.gnu.org To: gcc-gnats@gcc.gnu.org Cc: Subject: optimization/8613 Date: 21 Feb 2003 08:18:06 -0000 CVSROOT: /cvs/gcc Module name: gcc Branch: gcc-3_2-branch Changes by: ebotcazou@gcc.gnu.org 2003-02-21 08:18:06 Modified files: gcc : ChangeLog builtins.c gcc/testsuite : ChangeLog Added files: gcc/testsuite/gcc.c-torture/execute: 20030221-1.c Log message: PR optimization/8613 * builtins.c (expand_builtin): Emit postincrements before expanding builtin functions. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.13152.2.657.2.231&r2=1.13152.2.657.2.232 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/builtins.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.142.2.2.4.1&r2=1.142.2.2.4.2 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.1672.2.166.2.96&r2=1.1672.2.166.2.97 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/20030221-1.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=NONE&r2=1.1.4.1
State-Changed-From-To: analyzed->closed State-Changed-Why: Fixed.