Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find (all) integer overflows in a C program?

I am working on a large project that generally works just fine, but shows serious issues once the input data size exceeds some limitations.

These issues are (suspected) only due to signed integer overflows like these:

int a, o;
// Initialize a and o
int x = (a+o) >> 1);

Obviously, once the sum of a and o overflows (gets larger than 2^31-1), x is no longer the mean of a and o.

Is there a generic way to find all of these integer overflows in a running program?

I am thinking of a tool like Valgrind or a GDB extension that breaks at every integer arithmetic instruction, takes the parameters and compares the correct result (calculated with a larger-sized datatype or arbitrary-precision arithmetic) with the actual result. If the results differ, it should output a warning, trigger a debug break or something like this.

I know, how to check a single arithmetic instruction for overflows (e.g. checking the sign for additions), however due to the vast amount of code, it is not viable solution for me to go through the whole project and insert checking code everywhere by hand.

like image 675
ChrisM Avatar asked Jan 04 '11 13:01

ChrisM


People also ask

How do you find the integer overflow?

Write a “C” function, int addOvf(int* result, int a, int b) If there is no overflow, the function places the resultant = sum a+b in “result” and returns 0. Otherwise it returns -1. The solution of casting to long and adding to find detecting the overflow is not allowed.

What's the overflow of an integer in C?

Overview. Integer Overflow is a phenomenon that occurs when the integer data type cannot hold the actual value of a variable. Integer Overflow and Integer Underflow in C, do not raise any errors, but the program continues to execute (with the incorrect values) as if nothing has happened.

How do you solve integer overflow problems?

In languages where integer overflow can occur, you can reduce its likelihood by using larger integer types, like Java's long or C's long long int. If you need to store something even bigger, there are libraries built to handle arbitrarily large numbers.

How do you check integer overflow multiplication?

Approach : If either of the number is 0, then it will never exceed the range. Else if the product of the two divided by one equals the other, then also it will be in range. In any other case overflow will occur.


2 Answers

For large code base, Coverity is a good tool. I am not sure it will detect all integer overflows or not, but its worth giving a try.

like image 105
Vikram.exe Avatar answered Oct 07 '22 22:10

Vikram.exe


You have to work through all the code and work out what the limit on the user-input is and validate the input. You may also need to re-write some algorithms to reduce overflow issues.

As the example you give doesn't work for negative values, you should be using an unsigned int anyway, giving you an extra order of magnitude already.

Edit: gcc has the -ftrapv option, but this usually doesn't actually do anything only works with -O0. If you are taking the approach of trapping overflows when they happen, you still need good knowledge of the code in order to test it fully.

like image 38
OrangeDog Avatar answered Oct 07 '22 20:10

OrangeDog