Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Updated references from C11->C23

...

The C Standard, 6.3.1.4, paragraph 1 [ISO/IEC 9899:20112024], says,

When a finite value of real decimal floating type is converted to an integer type other than _Boolbool, 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"invalid" floating-point exception shall be raised and the result of the conversion is unspecified.

Paragraph 2 of the same subclause says,

When a value of integer type is converted to a real standard 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 subclause 6.3.1.5, paragraph 1+2, 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.

When a value of real floating type is converted to a standard floating type, if 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. 

See undefined behaviors 17 and 18.

...

Overflow when converting between floating point data types

Overflow when converting between integer types

Overflow when converting between unsigned integer types

3011 

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V

Supported

:

Astrée reports all potential overflows resulting from floating-point conversions.

Compass/ROSE



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

CodeSonar
Include Page
CodeSonar_V
CodeSonar_V

LANG.TYPE.IAT

Inappropriate Assignment Type

Coverity

Include Page
Coverity_V
Coverity_V

MISRA_CAST (needs verification)

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, and so on

Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C4450, C4451, C4452, C4453, C4454, C4462, C4465

C++3011


Klocwork

Include Page
Klocwork_V
Klocwork_V

MISRA.CAST.FLOAT.WIDER
MISRA.CAST.FLOAT.INT
MISRA.CAST.INT_FLOAT
MISRA.CONV.FLOAT


LDRA tool suite
Include Page
LDRA_V
LDRA_V
435 S, 93 SPartially implemented
Parasoft C/C++test
Include Page
Parasoft_V
Parasoft_V
CERT_C-FLP34-a
CERT_C-FLP34-b

Avoid implicit conversions from wider to narrower types

Polyspace Bug Finder
Include Page
Polyspace Bug Finder_VPolyspace Bug Finder_V

Float conversion overflow

Integer conversion overflow

Unsigned integer conversion overflow

floating type
Avoid implicit conversions of floating point numbers from wider to narrower floating type

PC-lint Plus

Include Page
PC-lint Plus_V
PC-lint Plus_V

735, 736,
915, 922,
9118, 9227

Partially supported

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rule FLP34-C

Checks for float conversion overflow (rule partially covered)

PRQA QA-C
Include Page
PRQA QA-C_vPRQA QA-C_v

4450, 4451,
4452, 4453,
4454,
4462, 4465

Partially implementedPRQA QA-C++
Include Page
cplusplus:PRQA QA-C++_Vcplusplus:PRQA QA-C++_V
PVS-Studio

Include Page
PVS-Studio_V
PVS-Studio_V

V615, V2003, V2004
TrustInSoft Analyzer

Include Page
TrustInSoft Analyzer_V
TrustInSoft Analyzer_V

float_to_int

Exhaustively verified (see one compliant and one non-compliant example).

Related Vulnerabilities

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

...

[IEEE 754 2006]
[ISO/IEC 9899:20112024]Subclause 6.3.1.4, "Real Floating and Integer"
Subclause 6.3.1.5, "Real Floating Types"

...