Created
June 2, 2011 16:38
-
-
Save mastbaum/1004768 to your computer and use it in GitHub Desktop.
A demonstration of GCC's ftrapv flag for C++ integer overflow debugging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<signal.h> | |
#include<limits.h> | |
/** g++'s -ftrapv flag provides some protection against integer overflows. It | |
* is a little awkward to use, though. All it will do is "trap" -- you must | |
* provide a signal handler to deal with it. | |
* | |
* (You must compile with -ftrapv for this to work) | |
*/ | |
// a simple signal handler. it must take the signal as an argument, per | |
// signal.h, whether we use it or not. | |
void handler(int /*signal*/) | |
{ | |
std::cout << "Overflow'd!" << std::endl; | |
} | |
int main() | |
{ | |
// when we get a SIGABRT, call handler | |
signal(SIGABRT, &handler); | |
// LONG_MAX is the largest long integer on this system, from limits.h | |
long a = LONG_MAX; | |
int b = 1; | |
long c = a + b; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment