Skip to content

Instantly share code, notes, and snippets.

@jefftrull
Created March 5, 2016 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jefftrull/8728f41b7cdf19922d54 to your computer and use it in GitHub Desktop.
Save jefftrull/8728f41b7cdf19922d54 to your computer and use it in GitHub Desktop.
Simple test of signed overflow UB
#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