The -ftrapv doesn't seem to want to generate code that will trap on overflow with optimization. I've tried with and without -fwrapv to try and get it to work.
$ cat t.c #include <stdio.h> int foo( int a) { if (a + 100 < a) { printf("Integer overflow detected!\n"); } else { printf("Integer overflow not detected!\n"); } return a; } int main() { foo(0x7fffffff); } mrs2 $ ./xgcc -B./ -fno-wrapv -ftrapv t.c -O2 mrs2 $ a.out Integer overflow detected! mrs2 $ ./xgcc -B./ -fwrapv -ftrapv t.c -O2 mrs2 $ a.out Integer overflow detected! mrs2 $ ./xgcc -B./ -fwrapv -ftrapv t.c mrs2 $ a.out Abort trap mrs2 $ ./xgcc -B./ -fno-wrapv -ftrapv t.c mrs2 $ a.out Abort trap -- Summary: -ftrapv doesn't trap Product: gcc Version: 4.4.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: mrs at apple dot com GCC host triplet: i686-apple-darwin9 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36868