integer overflow exception

This topic is 4853 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

Share on other sites
for (int x = 1;; x = x + x + 1;);

That should do it.

From,
Nice coder

Share on other sites
Thanks for your reply, but I know how to overflow an integer. The problem is that the exception is not being generated.

Share on other sites
That is a definite problem.

I also have one.

From,
Nice coder

Share on other sites
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.

Share on other sites
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.

Share on other sites
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");}

Share on other sites
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.

1. 1
2. 2
Rutin
21
3. 3
JoeJ
17
4. 4
5. 5

• 37
• 23
• 13
• 13
• 17
• Forum Statistics

• Total Topics
631705
• Total Posts
3001824
×