public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* how to make gcc warn about arithmetic signed overflow
@ 2013-09-21 16:45 wempwer
  2013-09-21 17:24 ` Jonathan Wakely
  2013-09-21 17:36 ` Brian Drummond
  0 siblings, 2 replies; 40+ messages in thread
From: wempwer @ 2013-09-21 16:45 UTC (permalink / raw)
  To: gcc-help

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@gmail.com>

^ permalink raw reply	[flat|nested] 40+ messages in thread

end of thread, other threads:[~2013-09-27  9:43 UTC | newest]

Thread overview: 40+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-21 16:45 how to make gcc warn about arithmetic signed overflow wempwer
2013-09-21 17:24 ` Jonathan Wakely
2013-09-21 17:41   ` wempwer
2013-09-21 18:30     ` Jonathan Wakely
2013-09-21 18:50       ` wempwer
2013-09-21 19:55         ` Jędrzej Dudkiewicz
2013-09-21 20:16           ` wempwer
2013-09-21 20:52             ` Jędrzej Dudkiewicz
2013-09-21 21:07               ` wempwer
2013-09-23  4:04       ` James K. Lowden
2013-09-23  7:55         ` Jonathan Wakely
2013-09-23 15:47           ` James K. Lowden
2013-09-23 21:50             ` Jonathan Wakely
2013-09-23 22:44               ` James K. Lowden
2013-09-23 23:20                 ` Jonathan Wakely
2013-09-23 19:38         ` Dave Allured - NOAA Affiliate
2013-09-23 19:43           ` Oleg Endo
2013-09-23 20:37             ` Dave Allured - NOAA Affiliate
2013-09-23 19:48           ` Andrew Haley
2013-09-23 22:00             ` James K. Lowden
2013-09-24 17:48               ` Andrew Haley
2013-09-26  2:30                 ` James K. Lowden
2013-09-26  8:29                   ` Vincent Lefevre
2013-09-26 14:49                     ` Andrew Haley
2013-09-26 17:03                       ` Vincent Lefevre
2013-09-26 18:19                         ` Andrew Haley
2013-09-27  7:58                           ` Vincent Lefevre
2013-09-27  8:23                             ` Andrew Haley
2013-09-27  9:28                               ` Vincent Lefevre
2013-09-27  9:43                                 ` Andrew Haley
2013-09-26 17:41                   ` Andrew Haley
2013-09-24  7:42           ` Brian Drummond
2013-09-21 17:53   ` Marc Glisse
2013-09-21 18:09     ` wempwer
2013-09-21 18:27       ` Jonathan Wakely
2013-09-21 19:32         ` wempwer
2013-09-22 15:52           ` Jonathan Wakely
2013-09-23 13:04           ` David Brown
2013-09-21 17:36 ` Brian Drummond
2013-09-21 17:45   ` wempwer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).