[Bug c++/48119] New: -Wtype-limits should warn when bit masking cannot possibly be true due to type size

eteran at alum dot rit.edu gcc-bugzilla@gcc.gnu.org
Mon Mar 14 19:23:00 GMT 2011


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

           Summary: -Wtype-limits should warn when bit masking cannot
                    possibly be true due to type size
           Product: gcc
           Version: 4.5.2
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: eteran@alum.rit.edu


I recently encountered a bug in some old code which boiled down to this
situation:

----snip----
#include <stdint.h>
#include <iostream>

int main() {
    uint16_t x = 0xffff;
    ++x;

    // did the value get to big?
    if(x & 0x10000) {
        x = 0xc000;
    }

    // I wanted it to say c000...
    std::cout << std::hex << x << std::endl;
}    
----snip----

Obviously, this was an issue where I needed to use a uint32_t, but mistakenly
used a smaller type, clearly my fault. However, I was surprised that there was
no warning, since the condition is clearly impossible given the uint16_t
datatype.

As far as I know, a conformant compiler is not required to emit a warning for
such code, but it would be a nice improvement to help catch this type of error
in the future.



More information about the Gcc-bugs mailing list