4 ms·
Another interesting case is dividing INT_MIN by -1. On x86 at least, that will not only overflow, but raise an arithmetic exception too, just like divide by zer
by jart 5y ago
Another interesting case is dividing INT_MIN by -1. On x86 at least, that will not only overflow, but raise an arithmetic exception too, just like divide by zero. So it's a useful trick for crashing code that tries to harden itself against SIGFPE being raised.
- bruce343434 5y agoJust tried this on x86-64, doesn't crash. #include <limits.h> #include <stdio.h> int main(int argc, char const *argv[]) { printf("%d", INT_MIN/(-1) == INT_MIN); return 0; }
- jart 5y agoGive this a whirl: int Div(int a, int b) { return a / b; } int (*pDiv)(int, int) = Div; int main(int argc, char *argv[]) { printf("%d\n", pDiv(INT_MIN, -1)); return 0; }
- zetalyrae 5y agoYes, for signed integer division of any bit-width this is the only case that results in overflow (division by zero is another exceptional case).
- benmmurphy 5y agoat some point in time you could crash solaris kernel by running a dtrace program that would divide INT_MIN by -1.