Bug 7209 - Faulty array dereferencing
Faulty array dereferencing
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: c++
3.1
: P3 normal
: ---
Assigned To: Nathan Sidwell
: wrong-code
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2002-07-04 13:06 UTC by jdbuiso
Modified: 2003-07-25 17:33 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Bug.cpp (434 bytes, application/octet-stream)
2003-05-21 15:16 UTC, jdbuiso
Details

Note You need to log in before you can comment on or make changes to this bug.
Description jdbuiso 2002-07-04 13:06:00 UTC
Dereferencing an array using a ternary operation (?:) and a function return value as indices can give faulty (and variable) results. In the example code one or more of the asserts can fail (depending on the optimization level), but only when compiling as C++.

Release:
3.1

Environment:
DOS (DJGPP) and Linux, both x86.
Comment 1 jdbuiso 2002-07-04 13:06:00 UTC
Fix:
Doing the ternary operation and/or function call before the array indexing solves (avoids) the problem.
Comment 2 Nathan Sidwell 2002-09-14 13:11:37 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: confirmed, regression
Comment 3 Nathan Sidwell 2002-10-16 14:57:08 UTC
Responsible-Changed-From-To: unassigned->nathan
Responsible-Changed-Why: fixing
Comment 4 Nathan Sidwell 2002-10-22 02:10:22 UTC
State-Changed-From-To: analyzed->closed
State-Changed-Why: this will appear in 3.2.1 & 3.3
    2002-10-22  Nathan Sidwell  <nathan@codesourcery.com>
    
    	PR c++/7209
    	* fold_const.c (fold_binary_op_with_conditional_arg): Always
    	build compound_expr if we used save_expr.
Comment 5 Nathan Sidwell 2002-10-22 08:56:50 UTC
From: nathan@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c++/7209
Date: 22 Oct 2002 08:56:50 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Changes by:	nathan@gcc.gnu.org	2002-10-22 01:56:49
 
 Modified files:
 	gcc            : ChangeLog fold-const.c 
 	gcc/testsuite  : ChangeLog 
 Added files:
 	gcc/testsuite/g++.dg/expr: cond1.C 
 
 Log message:
 	PR c++/7209
 	* fold_const.c (fold_binary_op_with_conditional_arg): Always
 	build compound_expr if we used save_expr.
 	testsuite:
 	* g++.dg/expr/cond1.C: New test.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=1.15750&r2=1.15751
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fold-const.c.diff?cvsroot=gcc&r1=1.223&r2=1.224
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.2171&r2=1.2172
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/expr/cond1.C.diff?cvsroot=gcc&r1=NONE&r2=1.1
 
Comment 6 Nathan Sidwell 2002-10-22 09:06:20 UTC
From: nathan@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c++/7209
Date: 22 Oct 2002 09:06:20 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Branch: 	gcc-3_2-branch
 Changes by:	nathan@gcc.gnu.org	2002-10-22 02:06:20
 
 Modified files:
 	gcc            : fold-const.c 
 	gcc/testsuite  : ChangeLog 
 
 Log message:
 	PR c++/7209
 	* fold_const.c (fold_binary_op_with_conditional_arg): Always
 	build compound_expr if we used save_expr.
 	testsuite:
 	* g++.dg/expr: New directory.
 	* g++.dg/expr/cond1.C: New test.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fold-const.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.185.2.4.2.1&r2=1.185.2.4.2.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.34&r2=1.1672.2.166.2.35