This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/48119] New: -Wtype-limits should warn when bit masking cannot possibly be true due to type size
- From: "eteran at alum dot rit.edu" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Mon, 14 Mar 2011 19:22:59 +0000
- Subject: [Bug c++/48119] New: -Wtype-limits should warn when bit masking cannot possibly be true due to type size
- Auto-submitted: auto-generated
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.