Signed long integer overflow detection in C

  • Thread starter Thread starter rama1001
  • Start date Start date
  • Tags Tags
    Detection Integer
Click For Summary

Discussion Overview

The discussion revolves around detecting signed long integer overflow in C programming. Participants explore various methods for identifying potential overflow situations, share code snippets, and discuss the implications of signed integer overflow, particularly in the context of existing code that may be causing issues.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses uncertainty about how to write test cases to detect overflow, seeking suggestions from others.
  • Another participant references an article stating that overflow cannot be detected once it occurs, suggesting the need for an algorithm to detect it beforehand, which may slow down the program.
  • Some participants propose using floating-point calculations as a way to check if integer results are reasonable, implying this could help in detecting overflow.
  • Discussion includes methods for checking overflow during addition, subtraction, multiplication, and division, with some participants noting that behavior may depend on the CPU architecture.
  • One participant mentions that signed integer overflow is undefined behavior in C and C++, which can lead to significant problems, sharing a personal experience of encountering issues after modifying existing code.
  • A participant shares a code snippet intended to handle overflow by casting to unsigned long and back to signed long, asking for feedback on this approach.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the best method for detecting signed long integer overflow. Multiple competing views and approaches are presented, indicating ongoing debate and exploration of the topic.

Contextual Notes

Participants note that signed integer overflow is considered undefined behavior in C and C++, which adds complexity to the discussion. There are also references to specific CPU behaviors that may affect overflow detection methods.

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
 
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
9
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 0 ·
Replies
0
Views
331
  • · Replies 4 ·
Replies
4
Views
2K