This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

c/8996: Unreliable bitfields


>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:


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]