...
Footnote 160 says a cast may be used to remove extra range and precision from the return expression. This means that a predictable program must have casts on all floating-point function calls (except where the function directly feeds an operator-like assignment that implies the conversion). With type-generic math (tgmath)
, the programmer has to reason through the tgmath
resolution rules to determine which casts to apply. These are significant obstacles to writing predictable code.
NOTE: Note that WG14 voted to include the following text in the C Standard [ISO/IEC 9899:2011]. It impacts only implementations that implement the optional Annex F, "IEC 60559 Floating-Point Arithmetic."
...
Code Block | ||||
---|---|---|---|---|
| ||||
float calcPercentage(float value) { return (float)(value * 0.1f); } void floatRoutine(void) { float value = 99.0f; long double percentage; percentage = calcPercentage(value); } |
Compliant Code Example (
...
Alternative)
Unfortunately, not all compilers honor casts. In this case, the range and precision must be forced by assignment to a variable of the correct type. This compliant solution forces the assignment by type-qualifying result
as volatile and assigning the result of the floating-point operation to result
before returning it:
...
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
FLP37-C | lowLow | unlikelyUnlikely | mediumMedium | P2 | L3 |
Related Vulnerabilities
...