Created
February 21, 2012 14:38
-
-
Save antirez/1876875 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* If you compile with -O2 the code will print "Wrong behavior", compiling | |
* with -O0 the code will print "Correct behavior". | |
* | |
* Tested with: | |
* Apple clang version 2.1 (tags/Apple/clang-163.7.1) (based on LLVM 3.0svn) | |
* Target: x86_64-apple-darwin11.3.0 | |
* Thread model: posix | |
*/ | |
#include <stdio.h> | |
int getValue(long long *l) { | |
*l = -9223372036854775484LL; | |
return 1; | |
} | |
void bug(void) { | |
long long value, incr, oldvalue; | |
incr = -1000; | |
if (getValue(&value) != 1) return; | |
oldvalue = value; | |
value += incr; | |
if ((incr < 0 && value > oldvalue) || (incr > 0 && value < oldvalue)) { | |
printf("Correct behavior\n"); | |
} else { | |
printf("Wrong behavior\n"); | |
} | |
} | |
int main(void) { | |
bug(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm turning the old code into this one:
Should be safe since it is guaranteed that all the parts never overflow.
LLONG_MIN-oldvalue can't overflow because oldvalue is guaranteed to be < 0
LLONG_MAX-oldvalue can't overflow because oldvalue is guaranteed to be > 0
Now trying it in practice :)