From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4831 invoked by alias); 18 Dec 2002 22:26:01 -0000 Mailing-List: contact gcc-prs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-prs-owner@gcc.gnu.org Received: (qmail 4783 invoked by uid 71); 18 Dec 2002 22:26:01 -0000 Resent-Date: 18 Dec 2002 22:26:01 -0000 Resent-Message-ID: <20021218222601.4782.qmail@sources.redhat.com> Resent-From: gcc-gnats@gcc.gnu.org (GNATS Filer) Resent-Cc: gcc-prs@gcc.gnu.org, gcc-bugs@gcc.gnu.org Resent-Reply-To: gcc-gnats@gcc.gnu.org, hakonrk@fys.uio.no Received: (qmail 2102 invoked by uid 61); 18 Dec 2002 22:20:20 -0000 Message-Id: <20021218222020.2101.qmail@sources.redhat.com> Date: Wed, 18 Dec 2002 14:26:00 -0000 From: hakonrk@fys.uio.no Reply-To: hakonrk@fys.uio.no To: gcc-gnats@gcc.gnu.org X-Send-Pr-Version: gnatsweb-2.9.3 (1.1.1.1.2.31) Subject: c/8996: Unreliable bitfields X-SW-Source: 2002-12/txt/msg00977.txt.bz2 List-Id: >Number: 8996 >Category: c >Synopsis: Unreliable bitfields >Confidential: no >Severity: serious >Priority: medium >Responsible: unassigned >State: open >Class: wrong-code >Submitter-Id: net >Arrival-Date: Wed Dec 18 14:26:00 PST 2002 >Closed-Date: >Last-Modified: >Originator: hakonrk@fys.uio.no >Release: gcc 3.2.1 >Organization: >Environment: System: Linux 2.4.20 i686 configured with: /local/build/gcc/gcc-3.2.1/configure --prefix=/local/gcc/3.2.1 --enable-languages=c --disable-nls >Description: Hard to explain, easy to show. In some cases, bit-field operations are very unreliable. The following program illustrates the problem: # 1 "foo.c" int main(void) { struct { unsigned a : 8; unsigned b : 23; unsigned c : 1; } x; *(unsigned *) &x = -1; x.c = 1; x.b = 1; x.a = 0; printf("%08X\n", *(unsigned *) &x); return 0; } The correct output from this program is 80000100. When compiling with -O2 (or higher) using GCC 3.2.1, the final assignment is not done (x.a = 0), and the output is 800001FF. A slight change in the program will make the bug disappear. Here are some examples of changes that will make the bug disappear: - commenting out the assignments to either x.b or x.c - changing the bit widths of x.a/x.b/x.c - inserting a "nop" between the printf and the assignments - changing the order of the assignments >How-To-Repeat: See description. >Fix: >Release-Note: >Audit-Trail: >Unformatted: