integer overflow exception

Started by
6 comments, last by KrazeIke 19 years ago
I am using C and Windows Structured Exception Handling on a Pentium 4. I am trying to generate an integer overflow exception (EXCEPTION_INT_OVERFLOW), but cannot seem to get it to work. I don't have any trouble generating other exceptions, including integer division by zero. For floating-point numbers, there is the _controlfp function that allows you to enable/disable certain exceptions, but I can't seem to find a corresponding function for integers. I know that some exceptions just aren't generated by x86 processors (e.g. array boundaries) - is this one of those? I found a site that said the assembly "into" instruction needed to be used in order for the exception to be generated, but that didn't seem to work either. It's not really that big of a deal, but I'm a little curious about it.
Advertisement
for (int x = 1;; x = x + x + 1;);

That should do it.

From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
Thanks for your reply, but I know how to overflow an integer. The problem is that the exception is not being generated.
That is a definite problem.

I also have one.
I don't know enough about exeptions to help you! [bawling]!!!

From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
You get an integer overflow exception by executing the INTO assembly instruction when the overflow flag is set. As far as I know there's no way to tell the processor to fire an exception for any integer overflow. And if there is such a thing and you turned it on it would probably break a ton code...

You need to check for overflows manually.

In C++ you can try the SafeInt class. For straight C code you have to do everything yourself.
-Mike
Okay, that's what I suspected. This was really just a theoretical excercise - I'm not really interested in handling integer overflow exceptions. It just seems a bit ridiculous that that exception exists at all if the only way to trigger it is by checking the overflow manually with the INTO instruction. Of course you are right that if that exception were enabled by default, it would break a lot of code. I'm still not sure why I can't get the INTO instruction to throw the exception though. Is it possible that I need to use a vectored exception handler for some reason? Anyway, thanks for the confirmation.
It's really only recently (at least in the consumer pc world) that people really understood (and cared about) the implications of integer overflows. It might seem ridiculous to us but 20 years ago when all this was being designed the masses didn't have the Internet and the average level of computer expertise (relative to the time) was much much higher. Security was not a concern and didn't really need to be.

Here is some code that demonstrates INTO throwing an exception:

#include <windows.h>#include <stdio.h>int main(){    bool hit = false;    __try    {        __asm        {            mov     al, 0x70            add     al, al            into        }    }    __except((GetExceptionCode() == EXCEPTION_INT_OVERFLOW)             ? EXCEPTION_EXECUTE_HANDLER             : EXCEPTION_CONTINUE_SEARCH)    {        hit = true;    }    printf("integer overlow exception %s hit\n", hit ? "was" : "was not");}
-Mike
The only difference between that and what I was doing is that I was doing the addition outside of the __asm block. I guess the overflow flag must get cleared when entering that block.

This topic is closed to new replies.

Advertisement