Overflowing signed integers
Overflowing signed integers.[1]
This checker checks for these issues:
Integer overflow.
Integer constant overflow.
Integer overflow occurs when an operation on integer variables can result in values that cannot be represented by the result data type. The data type of a variable determines the number of bytes allocated for the variable storage and constrains the range of allowed values.
The exact storage allocation for different integer types
depends on your processor. See Target
processor type (-target).
Integer overflows on signed integers result in undefined behavior.
The fix depends on the root cause of the defect. Often the result details show a sequence of events that led to the defect. Use this event list to determine how the variables in the overflowing computation acquire their current values. You can implement the fix on any event in the sequence. If the result details do not show the event history, you can trace back using right-click options in the source code and see previous related events. See also Interpret Bug Finder Results in Polyspace Desktop User Interface.
You can fix the defect by:
Using a bigger data type for the result of the operation so that all values can be accommodated.
Checking for values that lead to the overflow and performing appropriate error handling.
To avoid overflows in general, try one of these techniques:
Keep integer variable values restricted to within half the range of signed integers.
In operations that might overflow, check for conditions that can lead to the overflow and implement wrap around or saturation behavior depending on how the result of the operation is used. The result then becomes predictable and can be safely used in subsequent computations.
See examples of fixes below.
If you do not want to fix the issue, add comments to your result or code to avoid another review. See Address Polyspace Results Through Bug Fixes or Justifications.
#include <limits.h>
int plusplus(void) {
int var = INT_MAX;
var++;
return var;
}In the third statement of this function, the
variable var is increased by one. But the value
of var is the maximum integer value, so an int cannot
represent one plus the maximum integer value.
One possible correction is to change data types.
Store the result of the operation in a larger data type (Note that
on a 32-bit machine, int and long has
the same size). In this example, on a 32-bit machine, by returning
a long long instead of an int,
the overflow error is fixed.
#include <limits.h>
long long plusplus(void) {
long long lvar = INT_MAX;
lvar++;
return lvar;
}Integer constant overflow occurs when you assign a
compile-time constant to a signed integer variable whose data type cannot accommodate the
value. An n-bit signed integer holds values in the range
[-2.n-1,
2n-1-1]
For instance, c is an 8-bit
signed char variable that cannot hold the value
255.
signed char c = 255;
To determine the sizes of fundamental types, Bug
Finder uses your specification for Target
processor type (-target).
The default behavior for constant overflows can vary between compilers and platforms. Retaining constant overflows can reduce the portability of your code.
Even if your compilers wraps around overflowing constants with a warning, the wrap-around behavior can be unintended and cause unexpected results.
Check if the constant value is what you intended. If the value is correct, use a different, possibly wider, data type for the variable.
#define MAX_UNSIGNED_CHAR 255 #define MAX_SIGNED_CHAR 127 void main() { char c1 = MAX_UNSIGNED_CHAR; char c2 = MAX_SIGNED_CHAR+1; }
In this example, the defect appears on the macros
because at least one use of the macro causes an overflow. To reproduce these
defects, use analysis option Target
processor type (-target) where char is signed by
default.
One possible correction is to use a different data type for the variables that overflow.
#define MAX_UNSIGNED_CHAR 255
#define MAX_SIGNED_CHAR 127
void main() {
unsigned char c1 = MAX_UNSIGNED_CHAR;
unsigned char c2 = MAX_SIGNED_CHAR+1;
}| Decidability: Undecidable |
[1] Extracts from the standard "ISO/IEC TS 17961 Technical Specification - 2013-11-15" are reproduced with the agreement of AFNOR. Only the original and complete text of the standard, as published by AFNOR Editions - accessible via the website www.boutique.afnor.org - has normative value.