Created
March 5, 2016 00:14
-
-
Save jefftrull/8728f41b7cdf19922d54 to your computer and use it in GitHub Desktop.
Simple test of signed overflow UB
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 <cstdint> | |
#include <limits> | |
int main(int argc, char **argv) { | |
std::int32_t i = std::numeric_limits<int32_t>::max(); | |
i++; // signed overflow | |
if (i < 0) { | |
std::cerr << "i<0 and i=" << i << "\n"; // "expected" or "safe" result (-O1) | |
} else { | |
std::cerr << "i>=0 and i=" << i << "\n"; // result taking advantage of UB | |
} // to eliminate branch (-O2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment