This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/30475] assert(int+100 > int) optimized away
- From: "rguenth at gcc dot gnu dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 17 Jan 2007 17:05:59 -0000
- Subject: [Bug c/30475] assert(int+100 > int) optimized away
- References: <bug-30475-3511@http.gcc.gnu.org/bugzilla/>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- Comment #18 from rguenth at gcc dot gnu dot org 2007-01-17 17:05 -------
There is no single change that led to this situation and "reverting" it from
current development sources will not satisfy you anyway because old versions
are then still "affected".
The correct way to test for this overflow with compilers that implement
C90 6.2.1.2 / C99 6.3.1.3 as using modulo 2^N reduction is to simply
use unsigned arithmetic:
int foo(int a) {
assert((signed)((unsigned)a+100) > a);
printf("%d %d\n",a+100,a);
return a;
}
if that is not the case you need to avoid the overflow (as a conforming
implementation may also trap here) in the first place, so for example
int foo(int a)
{
if (a > INT_MAX - 100)
abort ();
printf("%d %d\n", a, a + 100);
return a;
}
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30475