Skip to content

Instantly share code, notes, and snippets.

@Frank-Buss
Created January 5, 2025 19:05
Show Gist options
  • Save Frank-Buss/1af54b205629e3cf026eeec3af2a7869 to your computer and use it in GitHub Desktop.
Save Frank-Buss/1af54b205629e3cf026eeec3af2a7869 to your computer and use it in GitHub Desktop.
demonstration of the overflow problem reported here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=30475
frank@wopr:~/tmp$ cat overflow.c
#include <assert.h>
#include <stdio.h>
int foo(int a) {
assert(a+100 > a);
printf("%d %d\n",a+100,a);
return a;
}
int main() {
foo(100);
foo(0x7fffffff);
}
frank@wopr:~/tmp$ gcc -O0 overflow.c -o overflow_gcc
frank@wopr:~/tmp$ ./overflow_gcc
200 100
-2147483549 2147483647
frank@wopr:~/tmp$ gcc -O2 overflow.c -o overflow_gcc
frank@wopr:~/tmp$ ./overflow_gcc
200 100
-2147483549 2147483647
frank@wopr:~/tmp$ gcc -O0 -fwrapv overflow.c -o overflow_gcc
frank@wopr:~/tmp$ ./overflow_gcc
200 100
overflow_gcc: overflow.c:5: foo: Assertion `a+100 > a' failed.
Aborted (core dumped)
frank@wopr:~/tmp$ clang -O0 overflow.c -o overflow_clang
frank@wopr:~/tmp$ ./overflow_clang
200 100
overflow_clang: overflow.c:5: int foo(int): Assertion `a+100 > a' failed.
Aborted (core dumped)
frank@wopr:~/tmp$ clang -O2 overflow.c -o overflow_clang
frank@wopr:~/tmp$ ./overflow_clang
200 100
-2147483549 2147483647
frank@wopr:~/tmp$ clang --version
Debian clang version 19.1.6 (1)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-19/bin
frank@wopr:~/tmp$ gcc --version
gcc (Debian 14.2.0-8) 14.2.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment