Re: Warn for certain integer overflows

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Frank Mehnert <Frank.Mehnert@xxxxxxx> 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


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux