Versions Compared

Key

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

...

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.

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.

And Section section 6.3.1.5 says,

When a value of real floating 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.

...

This noncompliant code example leads to undefined behavior if the integral part of f1 cannot be represented as an integer.:

Code Block
bgColor#ffcccc
langc
float f1;
int i1;

/* initialize fl */

i1 = f1; /* Undefined if the integral part of f1 > INT_MAX */

...

This noncompliant code example contains conversions that may be outside of the range of the demoted types.:

Code Block
bgColor#FFCCCC
langc
long double ld;
double d1;
double d2;
float f1;
float f2;

/* initializations */

f1 = (float)d1;
f2 = (float)ld;
d2 = (double)ld;

...

This compliant solution checks whether the values to be stored can be represented in the new type.:

Code Block
bgColor#ccccff
langc
#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;
}

...

Tool

Version

Checker

Description

Compass/ROSE

 

 

Can detect some violations of this rule. However, it does not flag implicit casts, only explicit ones.

Coverity

Include Page
Coverity_V
Coverity_V

MISRA_CAST

Can detect instances where implicit float conversion is involved: implicitly converting a complex expression with integer type to floating type, implicitly converting a double expression to narrower float type (may lose precision), implicitly converting a complex expression from float to double, implicitly converting from float to double in a function argument, etc.and so on

Fortify SCA

5.0

 

Can detect violations of this rule with CERT C Rule Pack.

PRQA QA-C
Include Page
PRQA_V
PRQA_V

4450
4451
4452
4453
4454

Partially implemented.

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

...