Warn for certain integer overflows

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

 



Hi,

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...

Kind regards,

Frank
-- 
Dr.-Ing. Frank Mehnert

Sitz der Gesellschaft:
Sun Microsystems GmbH, Sonnenallee 1, 85551 Kirchheim-Heimstetten
Amtsgericht München: HRB 161028
Geschäftsführer: Jürgen Kunz

Attachment: signature.asc
Description: This is a digitally signed message part.


[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