how to make gcc warn about arithmetic signed overflow

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

 



Hello,

I am trying to C learn language on a quite high level. I spend a
couple of days learning about unsigned and signed
arithmetic/conversion overflow, integer promotion and arithmetic
conversion. From what I understand the following snippet causes an
undefined behavior on all platforms:

int ab = 50000;
int bc = 50000;
int r = ab * bc;

In the first and second line we assign 50000 to signed int, nothing
bad happens here because on my computers int is 32 bits long so there
is no overflow. If it was an overflow, it is implementation defined
and would cause wraparound on most platforms. However, in the third
line there is no integer promotion performed because both operands are
already of type int but we have an arithmetic overflow because 50000 *
50000 doesn't fit in 32 bits signed integer. According to the C
standard this is an undefined behavior but again on most platforms it
comes down to wraparound. Value r is printed in printf with %d
specifier as -1794967296 using two's complement mechanism. To my
surprise gcc doesn't print any warnings in the 3rd line. I tried
several options such as -Wall, -Wstrict-overflow=5, -pedantic, -Wextra
but nothing produces a warning. Is it possible for gcc to produce a
warning in such situation?

-- 
<wempwer@xxxxxxxxx>





[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