Warn for certain integer overflows
Ian Lance Taylor
iant@google.com
Tue Jun 1 06:55:00 GMT 2010
Frank Mehnert <Frank.Mehnert@Sun.COM> writes:
> consider the following case:
>
> uint32_t foo, bar;
> ...
> foo = bar << 20;
>
> Of course, this operation can overflow and warning about this
> probably does not make sense as the programmer should be aware
> of this case. However, a warning in the following case could be
> appropriate IMO:
>
> uint32_t bar;
> uint64_t foo;
> ...
> foo = bar << 20;
>
> The intension is to catch the overflow case, therefore the
> assignment to a 64-bit variable. The intended code would be
>
> uint32_t bar;
> uint64_t foo;
> ...
> foo = (uint64_t)bar << 20;
>
> I think it would be a big help if gcc could warn in the 2nd case.
>
> What do you think? Or is such a warning already available? I'm
> a big fan of -Wlogical-op which catches suspicious uses of certain
> logical operations...
As far as I know there is no current warning in gcc for this kind of
case. gcc can warn about cases where a conversion may alter a value,
but that is not happening here. The warning here would be something
like an implicit widening conversion happens after an arithmetic
operation. This seems like a reasonable warning to me. Would you
mind opening a feature request at http://gcc.gnu.org/bugzilla/ ?
Thanks.
Ian
More information about the Gcc-help
mailing list