Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: minor change

If a floating-point value is to be demoted to a floating-point value of a smaller range and precision or to an integer type, or if an integer type is to be converted to a floating-point type, the value must be represented representable in the new type.

Subclause The C Standard, subclause 6.3.1.4 paragraphs 1 and 2 of the C Standard paragraphs 1  [ISO/IEC 9899:2011] says,

When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.

Paragraph 2 of the same subclause says,

When a value of integer type is converted to a real floating type, if the value being converted can be represented exactly in the new type, it is unchanged. If the value being converted is in the range of values that can be represented but cannot be represented exactly, the result is either the nearest higher or nearest lower representable value, chosen in an implementation-defined manner. If the value being converted is outside the range of values that can be represented, the behavior is undefined.

...

Consequently, in implementations that do not allow for the representation of all numbers, conversions of values between zero and FLT_MIN may result in are undefined behavior. For example, if an implementation supports denormal subnormal numbers the conversion is representable, if not, the behavior is undefined.

...

This compliant solution assumes that the range of values of type float is greater than that of an int, as is the case in most implementations. Unfortunately, there is no safe way to inquire about this assumption in the code short of already knowing the implementation. Converting INT_MAX to float is a problem on many implementations, resulting in a number one greater than the value of INT_MAX.  Converting INT_MIN to float is a problem on many implementations, resulting in a number one less than the value of INT_MIN.

 

Code Block
bgColor#ccccff
langc
#include <limits.h>
 
void func(float f_a) {
  int i_a;

  if (f_a >= ((float)INT_MAX -1.0) || f_a < ((float)INT_MIN +1.0)|| (f_a >= 0F && f_a < FLT_MIN)) {
    /* Handle error */
  } else {
    i_a = f_a;
  }
}

...

Code Block
bgColor#ccccff
langc
#include <float.h>
 
void func(double d_a, long double big_d) {
  double d_b;
  float f_a;
  float f_b;

  if (d_a > FLT_MAX || d_a < -FLT_MAX) {
    /* Handle error condition */
  } else {
    f_a = (float)d_a;
  }
  if (big_d > FLT_MAX || big_d < -FLT_MAX) {
    /* Handle error condition */
  } else {
    f_b = (float)big_d;
  }
  if (big_d > DBL_MAX || big_d < -DBL_MAX) {
    /* Handle error condition */
  } else {
    d_b = (double)big_d;
  }  
}

Risk Assessment

Failing to check that Demoting a floating-point value to a floating-point value fits within a demoted of a smaller range and precision or to an integer type, or converting an integer type to a floating-point type can result in a value too large to be represented by that is not representable in the new type , resulting in and undefined behavior.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

FLP34-C

Low

Unlikely

Low

P3

L3

...