[Bug tree-optimization/43089] New: Optimizer ignores type in a conversion

0xe2 dot 0x9a dot 0x9b at gmail dot com gcc-bugzilla@gcc.gnu.org
Tue Feb 16 09:53:00 GMT 2010


#include <stdio.h>
#include <assert.h>

struct AB {
        unsigned a:1;
        unsigned b:31;
};

int main(int argc, char **argv) {
        unsigned in;
        struct AB ab;
        unsigned b2;

        sscanf(argv[1], "%x", &in);
        ab = (struct AB){0,in};

        b2 = ab.b + ab.b;
        assert(!(b2 <= 0x7fffffff));

        return 0;
}

Architecture: i386
Command line: ./a.out 7fffffff
Succeeds when compiled with: gcc -O0 ...
Fails when compiled with: gcc -O2 ...
Expected behavior: the program should execute successfully

Possible explanation: In the expression (ab.b + ab.b), the bit-field "b" gets
converted into an int. The addition is therefore of type (int+int), with an
(int) as result. The (int) result is then converted into an (unsigned int) -
but this step is skipped when using -O2, which leads the compiler to the wrong
conclusion that (b2 <= 0x7fffffff) is always true.


-- 
           Summary: Optimizer ignores type in a conversion
           Product: gcc
           Version: 4.4.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: 0xe2 dot 0x9a dot 0x9b at gmail dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43089



More information about the Gcc-bugs mailing list