Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ccccff
float f1;
int i1;

/* initialize fl */

if (f1 > (float) INT_MAX || f1 < (float) INT_MIN) {
  /* Handle Errorerror */
} else {
  i1 = f1;
}

Noncompliant Code Example (demotions)

...

Code Block
bgColor#ccccff
#include <float.h>

long double ld;
double d1;
double d2;
float f1;
float f2;

/* initializations */

if (d1 > FLT_MAX || d1 < -FLT_MAX) {
  /* Handle error condition */
} else {
  f1 = (float)d1;
}
if (ld > FLT_MAX || ld < -FLT_MAX) {
  /* Handle error condition */
} else {
  f2 = (float)ld;
}
if (ld > DBL_MAX || ld < -DBL_MAX) {
  /* Handle error condition */
} else {
  d2 = (double)ld;
}

Risk

...

Assessment

Failing to check that a floating-point value fits within a demoted type can result in a value too large to be represented by the new type, resulting in undefined behavior.

...