CWE Rule 681
Description
Rule Description
When converting from one data type to another, such as long to integer, data can be omitted or translated in a way that produces unexpected values. If the resulting values are used in a sensitive context, then dangerous behaviors may occur.
Polyspace Implementation
The rule checker checks for these issues:
Float conversion overflow
Precision loss in integer to float conversion
Examples
Float conversion overflow
This issue occurs when converting a floating point number to a smaller floating point data type. If the variable does not have enough memory to represent the original number, the conversion overflows.
The exact storage allocation for different floating point types depends on your processor. See
Target processor type (-target)
.
Overflows can result in unpredictable values from computations. The result can be infinity or the maximum finite value depending on the rounding mode used in the implementation. If you use the result of an overflowing conversion in subsequent computations and do not account for the overflow, you can see unexpected results.
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 variable being converted acquires its current value 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 or Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access).
You can fix the defect by:
Using a bigger data type for the result of the conversion so that all values can be accommodated.
Checking for values that lead to the overflow and performing appropriate error handling.
In general, avoid conversions to smaller floating point types.
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 Results in Polyspace User Interface Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
By default, a Bug Finder analysis does not recognize infinities and
NaNs
. Operations that results in infinities and
NaNs
might be flagged as defects. To handle infinities and
NaN
values in your code, use the option Consider non finite floats
(-allow-non-finite-floats).
A default Bug Finder analysis might not raise this defect when the input values are unknown and only a subset of inputs cause an issue. To check for defects caused by specific system input values, run a stricter Bug Finder analysis. See Extend Bug Finder Checkers to Find Defects from Specific System Input Values.
float convert(void) { double diam = 1e100; return (float)diam; //Noncompliant }
In the return statement, the variable diam
of
type double (64 bits) is converted to a variable of type float (32
bits). However, the value 1^100 requires more than 32 bits to be precisely
represented.
Precision loss in integer to float conversion
This issue occurs when you cast an integer value to a floating-point type that cannot represent the original integer value.
For instance, the long int
value
1234567890L
is too large for a variable of type
float
.
If the floating-point type cannot represent the integer value, the behavior is undefined (see C11 standard, 6.3.1.4, paragraph 2). For instance, least significant bits of the variable value can be dropped leading to unexpected results.
Convert to a floating-point type that can represent the integer value.
For instance, if the float
data type cannot represent the integer
value, use the double
data type instead.
When writing a function that converts an integer to floating point type, before the
conversion, check if the integer value can be represented in the floating-point type. For
instance, DBL_MANT_DIG * log2(FLT_RADIX)
represents the number of base-2
digits in the type double
. Before conversion to the type
double
, check if this number is greater than or equal to the precision
of the integer that you are converting. To determine the precision of an integer
num
, use this
code:
size_t precision = 0; while (num != 0) { if (num % 2 == 1) { precision++; } num >>= 1; }
Some implementations provide a builtin function to determine the precision of an
integer. For instance, GCC provides the function
__builtin_popcount
.
#include <stdio.h> int main(void) { long int big = 1234567890L; float approx = big; //Noncompliant printf("%ld\n", (big - (long int)approx)); return 0; }
In this C code, the long int
variable big
is converted to float
.
One possible correction is to convert to the double
data type
instead of float
.
#include <stdio.h> int main(void) { long int big = 1234567890L; double approx = big; printf("%ld\n", (big - (long int)approx)); return 0; }
Check Information
Category: Numeric Errors |
Version History
Introduced in R2024a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)