Bug 5186 - -O works; -O2 doesn't; partial assignment through cast to smaller type
Summary: -O works; -O2 doesn't; partial assignment through cast to smaller type
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 3.0.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2001-12-24 10:06 UTC by tonyb
Modified: 2005-06-05 09:34 UTC (History)
3 users (show)

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


Attachments
gccbug.i.bz2 (3.65 KB, application/octet-stream)
2003-05-21 15:17 UTC, tonyb
Details

Note You need to log in before you can comment on or make changes to this bug.
Description tonyb 2001-12-24 10:06:00 UTC
The included small test program fails with -O2 but works with -O.  Note that the program assumes a little-endian architecture (i386).

The program does partial modification of a variable by casting its address to a pointer of a smaller type, and then reads back the entire value.

The first write to 'test' through the (char *) really should do nothing, but it is part of what triggers the bug.  Also, the bug is not triggered if the array index on this line is [0] rather than [1].

The same sort of thing happens when the partial assignment takes place in a function that is inlined.

I encountered the problem with RedHat's gcc-2.96-98, and then verified that the problem also exists in gcc-3.0.2.

Release:
gcc version 3.0.2 20010905 (Red Hat Linux 7.1 3.0.1-3)

Environment:
RedHat Linux 7.2 i386 + latest errata updates

How-To-Repeat:
gcc3 -Wall -O -ogood gccbug.c
gcc3 --save-temps -Wall -O2 -obad gccbug.c
./good
(no output)
./bad
test failed: 0xaabbccdd
Comment 1 tonyb 2001-12-24 13:20:29 UTC
From: "Tony Battersby" <tonyb@cybernetics.com>
To: <gcc-gnats@gcc.gnu.org>, <gcc-prs@gcc.gnu.org>, <tonyb@cybernetics.com>,
	<gcc-bugs@gcc.gnu.org>, <nobody@gcc.gnu.org>
Cc:  
Subject: Re: optimization/5186: -O works; -O2 doesn't; partial assignment through cast 
Date: Mon, 24 Dec 2001 13:20:29 -0500

 This is a multi-part message in MIME format.
 
 ------=_NextPart_000_0003_01C18C7D.C2341B80
 Content-Type: text/plain;
 	charset="iso-8859-1"
 Content-Transfer-Encoding: 7bit
 
 Sorry, it looks like my attachment didn't make it.  Here it is.
 
 Anthony J. Battersby
 Cybernetics
 ------=_NextPart_000_0003_01C18C7D.C2341B80
 Content-Type: application/octet-stream;
 	name="gccbug.i.bz2"
 Content-Transfer-Encoding: base64
 Content-Disposition: attachment;
 	filename="gccbug.i.bz2"
 
 QlpoOTFBWSZTWbRD/VsABpd/gHxw/lt/f//7v+/fLr////pgE594A+vPT3PXu6dexy93jdrzuePd
 u89b1uDSGvT0OnoA2YDRQAAG7t0I65KoSRQAmmEFPFMT1NBo2oyDTQeoAAA0A9Q9IA9QaaAkERMj
 QepoAAAaNHqADQAAAAADhoAAaDQaAwQA0NNMgGho0yAGIGgASaSRQRpqDQMgaA0AAAAAAANGg00D
 QRJEVPCZJ5JPNKaejUep6jRoeoyAepoHqAABoHqPFDQESQQQE0Go0JoJPKaD9UDQ0HqAAAA0AAPU
 0A8oJGMiEijCMIMWCgPyQ+yEgvYiSUthKNBArJFC+AXsMNmGwUoqCRFEFkV8ZqKLBYK5YWaij4Cy
 bkk1hq1wWYxRcmMRmbNDFRZLUsWKFWrIUUq2WKKVOr1z9sDeG0Sd+1ETmLqoKCKrEWAqxUFixRFF
 iSpQcFo2WgtLGNpUbVbBqorasCqKtKLZuSsyuFbUSlYURRRgoIjFolWijYsaUWsufbzOIwOqPPcT
 +drowyZkT6Zqho0T+9xVQpVIwYnPsJDtmYhijBZBtAUArKyp26EGgMdbW6CZ9cOckNnIYpneM9Qg
 l9JWV6lxM3UsJFpLstqe1Wi6ZDUoq5VKm66KDJQIsLLRCK80DVYxqRuIj4jMh3gWitMyb6o/UsBw
 6YdiOIhbT/II4X8yFIk3b6Y3puBIh1yg1e9x0QNBNhHW3AwCikGAue/C+7AKGFhYi+5rBSSMCQTh
 VG18KzuAdh++2tDQNXaTlP5ff378DvlyB9li21qqBJ5Qq1N2Da60bbFA0RKR6aGma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Richard Henderson 2002-04-03 01:48:17 UTC
State-Changed-From-To: open->closed
State-Changed-Why: Not a bug.  This code violates ISO C aliasing rules.
    Use a union instead.
Comment 3 Andrew Pinski 2005-06-05 09:34:26 UTC
Reopening to ...
Comment 4 Andrew Pinski 2005-06-05 09:34:46 UTC
Mark as a dup of bug 21920.

*** This bug has been marked as a duplicate of 21920 ***