public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* c/8996: Unreliable bitfields
@ 2002-12-18 14:26 hakonrk
  0 siblings, 0 replies; 2+ messages in thread
From: hakonrk @ 2002-12-18 14:26 UTC (permalink / raw)
  To: gcc-gnats


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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: c/8996: Unreliable bitfields
@ 2002-12-18 15:17 reichelt
  0 siblings, 0 replies; 2+ messages in thread
From: reichelt @ 2002-12-18 15:17 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, hakonrk, nobody

Synopsis: Unreliable bitfields

State-Changed-From-To: open->closed
State-Changed-By: reichelt
State-Changed-When: Wed Dec 18 15:17:02 2002
State-Changed-Why:
    Not a bug.
    
    Your program does not obey aliasing rules.
    You can use the option "-fno-strict-aliasing" as a workaround
    or you can correct your code:
    
    int main(void)
    {
            union {
                struct {
                        unsigned a :  8;
                        unsigned b : 23;
                        unsigned c :  1;
                } x;
                unsigned i;
            } u;
    
            u.i = -1;
            u.x.c = 1;
            u.x.b = 1;
            u.x.a = 0;
            printf("%08X\n", u.i);
    
            return 0;
    }

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8996


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-12-18 23:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-18 14:26 c/8996: Unreliable bitfields hakonrk
2002-12-18 15:17 reichelt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).