Signed long integer overflow detection in C

  • Thread starter Thread starter rama1001
  • Start date Start date
  • Tags Tags
    Detection Integer
AI Thread Summary
The discussion revolves around addressing potential overflow issues in code, particularly when dealing with signed integers in C and C++. It emphasizes the importance of developing algorithms to detect overflow before it occurs, as overflow can lead to system crashes and is often undefined behavior in these languages. Various methods for checking overflow during addition, subtraction, multiplication, and division are suggested, including using floating-point comparisons and leveraging processor-specific features like overflow bits. The conversation also touches on the challenges of modifying legacy code, where recent changes have led to crashes, and the need for careful testing of new code segments. Suggestions include using exception handling and interrupt hooks where applicable, as well as casting between signed and unsigned types to manage overflow risks.
rama1001
Messages
130
Reaction score
1
Hi,
I have three situations where might be overflow is occurring. I need to write test cases to resolve this problem. I don't know how to continue after this, please anyone have suggestions to overcome this.

Please help me.
 
Technology news on Phys.org
Post your code
 
http://www.phrack.org/issues.html?issue=60&id=10#article

from the article it says that they can't be detected once they happen. So you need to develop an algorithm to detect it before it happens which can seriously slowdown your program but since its a testcase that may not matter.
 
i suppose you could do a second calculation in floating pt and then check to see if the integer answer is in the ballpark of the floating pt answer.
 
Most processors have an overflow bit for integer math, but most compilers don't provide access to it (short of using inline assembly or assembly callable functions). Checking for overflow on addition can be done by checking to see if both addends have the same sign, and if so, if the sum also has the same sign (else overflow has occurred). Subtraction can be handled by negating the number to subtract and using the addition check. Overflow from multiplication may cause an exception depending on the cpu. If not, multiplication can be checked by dividing the product by one of the multiplicands to see if you get the other multiplicand. Overflow from division may cause an exception depending on the cpu. If not, you need to check the quotient by multiplying the quotient by the divisor to see if it matches the dividend.
 
If you are using a language with exception catch an exception.

If you are using something where you can add a kind of interrupt hook, then use that. Chances are if you are developing in an environment, then you will be able to do this.

From this website:

The Kernel's Trap Handler

The kernel's trap handler mediates handling of interrupts, exceptions, system service calls, and virtual memory management.

The difference between interrupts and exceptions is that interrupts occur asynchronously (for instance, when hardware peripheral devices needs processor attention), and exceptions occur as a part of standard application execution (for instance, when a math overflow occurs). So, exceptions are generally reproducable, but interrupts involve timing relationships that are difficult to reproduce.
 
rcgldr said:
Most processors have an overflow bit for integer math, but most compilers don't provide access to it (short of using inline assembly or assembly callable functions). Checking for overflow on addition can be done by checking to see if both addends have the same sign, and if so, if the sum also has the same sign (else overflow has occurred). Subtraction can be handled by negating the number to subtract and using the addition check. Overflow from multiplication may cause an exception depending on the cpu. If not, multiplication can be checked by dividing the product by one of the multiplicands to see if you get the other multiplicand. Overflow from division may cause an exception depending on the cpu. If not, you need to check the quotient by multiplying the quotient by the divisor to see if it matches the dividend.

I agree with you if my case is regarded to unsigned integers. I am dealing with signed integers and the code was executed based on the user settings. Anyhow, i have read many documents that are explicitly saying that signed integer overflow is undefined(in C and C++) and creates lot of problems. I haven't experienced this before but it was really a nightmare for me. The code was written by some one else long ago. Recently, after including 20 lines code to the old one was creating the problem. I can not post that code here due some reasons. There is no syntax errors in that 20 lines but excution of that code was creating system crash. I can send it to some other mail if you want to have a look.
 
if((current<0 && rStrom>0)||(current>0 && rStrom<0))
{
unerror=unrstrom-uncurrent;
Error = (Signed long)unerror;
unerrorint = unerrorint+unerror;
ErrorInt = (Signed long)unerrorint;
}
else
{
Error=rStrom-current;
ErrorInt=ErrorInt+Error;
}


I have not tested this yet but i have written the test case like above. All current,rstrom, error and errorint are declared as signed long at the beginning but i casted them to unsigned long to eliminate the overflow and then cast back to signed long. Any suggestions would be appreciated.
 
Back
Top