signed long integer overflow detection in C


by rama1001
Tags: detection, integer, overflow, signed
rama1001
rama1001 is offline
#1
Feb22-12, 07:26 AM
P: 133
Hi,
I have three situations where might be overflow is occuring. I need to write test cases to resolve this problem. I don't know how to continue after this, please any one have suggestions to overcome this.

Please help me.
Phys.Org News Partner Science news on Phys.org
Review: With Galaxy S5, Samsung proves less can be more
Making graphene in your kitchen
Study casts doubt on climate benefit of biofuels from corn residue
camel-man
camel-man is offline
#2
Feb22-12, 09:59 AM
P: 55
Post your code
jedishrfu
jedishrfu is offline
#3
Feb22-12, 10:07 AM
P: 2,480
http://www.phrack.org/issues.html?is...&id=10#article

from the article it says that they cant 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.

jedishrfu
jedishrfu is offline
#4
Feb22-12, 10:11 AM
P: 2,480

signed long integer overflow detection in C


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.
rcgldr
rcgldr is offline
#5
Feb22-12, 11:54 AM
HW Helper
P: 6,925
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.
chiro
chiro is offline
#6
Feb22-12, 09:54 PM
P: 4,570
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.
rama1001
rama1001 is offline
#7
Feb23-12, 03:20 AM
P: 133
Quote Quote by rcgldr View Post
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.
rama1001
rama1001 is offline
#8
Feb23-12, 04:41 AM
P: 133
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.


Register to reply

Related Discussions
Angular momentum - integer or half-integer Quantum Physics 2
Log Overflow Problem Precalculus Mathematics Homework 2
Proof Question: Prove integer + 1/2 is not an integer Calculus & Beyond Homework 4
Stack Overflow with Quicksort Programming & Computer Science 7
Arithmetic Overflow Engineering, Comp Sci, & Technology Homework 3