Skip to content

Commit 74ceace

Browse files
committed
FloatingTypes: Implement FLP34-C
Adds a query for FLP34-C which checks whether a float-to-int conversion is within the bounds of the new type. We check (a) for a bounded range or (b) a suitable guard on the conversion that indicates the float has been considered against the precision of the integer.
1 parent 25c6e77 commit 74ceace

File tree

5 files changed

+312
-0
lines changed

5 files changed

+312
-0
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
# FLP34-C: Ensure that floating-point conversions are within range of the new type
2+
3+
This query implements the CERT-C rule FLP34-C:
4+
5+
> Ensure that floating-point conversions are within range of the new type
6+
7+
8+
## Description
9+
10+
If a floating-point value is to be converted 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 representable in the destination type.
11+
12+
The C Standard, 6.3.1.4, paragraph 1 \[[ISO/IEC 9899:2011](https://wiki.sei.cmu.edu/confluence/display/c/AA.+Bibliography#AA.Bibliography-ISO-IEC9899-2011)\], says,
13+
14+
> 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](https://wiki.sei.cmu.edu/confluence/display/c/BB.+Definitions#BB.Definitions-undefinedbehavior).
15+
16+
17+
Paragraph 2 of the same subclause says,
18+
19+
> 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](https://wiki.sei.cmu.edu/confluence/display/c/BB.+Definitions#BB.Definitions-implementation-definedbehavior) manner. If the value being converted is outside the range of values that can be represented, the behavior is [undefined](https://wiki.sei.cmu.edu/confluence/display/c/BB.+Definitions#BB.Definitions-undefinedbehavior).
20+
21+
22+
And subclause 6.3.1.5, paragraph 1, says,
23+
24+
> 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](https://wiki.sei.cmu.edu/confluence/display/c/BB.+Definitions#BB.Definitions-implementation-definedbehavior) manner. If the value being converted is outside the range of values that can be represented, the behavior is [undefined](https://wiki.sei.cmu.edu/confluence/display/c/BB.+Definitions#BB.Definitions-undefinedbehavior).
25+
26+
27+
See [undefined behaviors 17](https://wiki.sei.cmu.edu/confluence/display/c/CC.+Undefined+Behavior#CC.UndefinedBehavior-ub_17) and [18](https://wiki.sei.cmu.edu/confluence/display/c/CC.+Undefined+Behavior#CC.UndefinedBehavior-ub_18).
28+
29+
This rule does not apply to demotions of floating-point types on [implementations](https://wiki.sei.cmu.edu/confluence/display/c/BB.+Definitions#BB.Definitions-implementation) that support signed infinity, such as IEEE 754, as all values are within range.
30+
31+
## Noncompliant Code Example (float to int)
32+
33+
This noncompliant code example leads to undefined behavior if the integral part of `f_a` cannot be represented as an integer:
34+
35+
```cpp
36+
void func(float f_a) {
37+
int i_a;
38+
39+
/* Undefined if the integral part of f_a cannot be represented. */
40+
i_a = f_a;
41+
}
42+
```
43+
44+
## Compliant Solution (float to int)
45+
46+
This compliant solution tests to ensure that the `float` value will fit within the `int` variable before performing the assignment.
47+
48+
```cpp
49+
#include <float.h>
50+
#include <limits.h>
51+
#include <math.h>
52+
#include <stddef.h>
53+
#include <stdint.h>
54+
55+
extern size_t popcount(uintmax_t); /* See INT35-C */
56+
#define PRECISION(umax_value) popcount(umax_value)
57+
58+
void func(float f_a) {
59+
int i_a;
60+
61+
if (isnan(f_a) ||
62+
PRECISION(INT_MAX) < log2f(fabsf(f_a)) ||
63+
(f_a != 0.0F && fabsf(f_a) < FLT_MIN)) {
64+
/* Handle error */
65+
} else {
66+
i_a = f_a;
67+
}
68+
}
69+
70+
```
71+
72+
## Noncompliant Code Example (Narrowing Conversion)
73+
74+
This noncompliant code example attempts to perform conversions that may result in truncating values outside the range of the destination types:
75+
76+
```cpp
77+
void func(double d_a, long double big_d) {
78+
double d_b = (float)big_d;
79+
float f_a = (float)d_a;
80+
float f_b = (float)big_d;
81+
}
82+
83+
```
84+
As a result of these conversions, it is possible that `d_a` is outside the range of values that can be represented by a float or that `big_d` is outside the range of values that can be represented as either a `float` or a `double`. If this is the case, the result is undefined on implementations that do not support Annex F, "IEC 60559 Floating-Point Arithmetic."
85+
86+
## Compliant Solution (Narrowing Conversion)
87+
88+
This compliant solution checks whether the values to be stored can be represented in the new type:
89+
90+
```cpp
91+
#include <float.h>
92+
#include <math.h>
93+
94+
void func(double d_a, long double big_d) {
95+
double d_b;
96+
float f_a;
97+
float f_b;
98+
99+
if (d_a != 0.0 &&
100+
(isnan(d_a) ||
101+
isgreater(fabs(d_a), FLT_MAX) ||
102+
isless(fabs(d_a), FLT_MIN))) {
103+
/* Handle error */
104+
} else {
105+
f_a = (float)d_a;
106+
}
107+
if (big_d != 0.0 &&
108+
(isnan(big_d) ||
109+
isgreater(fabs(big_d), FLT_MAX) ||
110+
isless(fabs(big_d), FLT_MIN))) {
111+
/* Handle error */
112+
} else {
113+
f_b = (float)big_d;
114+
}
115+
if (big_d != 0.0 &&
116+
(isnan(big_d) ||
117+
isgreater(fabs(big_d), DBL_MAX) ||
118+
isless(fabs(big_d), DBL_MIN))) {
119+
/* Handle error */
120+
} else {
121+
d_b = (double)big_d;
122+
}
123+
}
124+
125+
```
126+
127+
## Risk Assessment
128+
129+
Converting a floating-point value to a floating-point value 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 that is not representable in the destination type and is undefined behavior on implementations that do not support Annex F.
130+
131+
<table> <tbody> <tr> <th> Rule </th> <th> Severity </th> <th> Likelihood </th> <th> Remediation Cost </th> <th> Priority </th> <th> Level </th> </tr> <tr> <td> FLP34-C </td> <td> Low </td> <td> Unlikely </td> <td> Low </td> <td> <strong>P3</strong> </td> <td> <strong>L3</strong> </td> </tr> </tbody> </table>
132+
133+
134+
## Automated Detection
135+
136+
<table> <tbody> <tr> <th> Tool </th> <th> Version </th> <th> Checker </th> <th> Description </th> </tr> <tr> <td> <a> Astrée </a> </td> <td> 22.04 </td> <td> </td> <td> Supported Astrée reports all potential overflows resulting from floating-point conversions. </td> </tr> <tr> <td> <a> Compass/ROSE </a> </td> <td> </td> <td> </td> <td> Can detect some violations of this rule. However, it does not flag implicit casts, only explicit ones </td> </tr> <tr> <td> <a> CodeSonar </a> </td> <td> 7.2p0 </td> <td> <strong>LANG.TYPE.IAT</strong> </td> <td> Inappropriate Assignment Type </td> </tr> <tr> <td> <a> Coverity </a> </td> <td> 2017.07 </td> <td> <strong>MISRA_CAST (needs verification)</strong> </td> <td> 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 <code>float</code> to <code>double</code> , implicitly converting from <code>float</code> to <code>double</code> in a function argument, and so on </td> </tr> <tr> <td> <a> Helix QAC </a> </td> <td> 2022.4 </td> <td> <strong>C4450, C4451, C4452, C4453, C4454, C4462, C4465</strong> <strong>C++3011</strong> </td> <td> </td> </tr> <tr> <td> <a> Klocwork </a> </td> <td> 2022.4 </td> <td> <strong>MISRA.CAST.FLOAT.WIDER</strong> <strong>MISRA.CAST.FLOAT.INT</strong> <strong>MISRA.CAST.INT_FLOAT</strong> <strong>MISRA.CONV.FLOAT</strong> </td> <td> </td> </tr> <tr> <td> <a> LDRA tool suite </a> </td> <td> 9.7.1 </td> <td> <strong>435 S, 93 S</strong> </td> <td> Partially implemented </td> </tr> <tr> <td> <a> Parasoft C/C++test </a> </td> <td> 2022.2 </td> <td> <strong>CERT_C-FLP34-aCERT_C-FLP34-b</strong> </td> <td> Avoid implicit conversions from wider to narrower floating type Avoid implicit conversions of floating point numbers from wider to narrower floating type </td> </tr> <tr> <td> <a> PC-lint Plus </a> </td> <td> 1.4 </td> <td> <strong>735, 736,915, 922,9118, 9227</strong> </td> <td> Partially supported </td> </tr> <tr> <td> <a> Polyspace Bug Finder </a> </td> <td> R2022b </td> <td> <a> CERT C: Rule FLP34-C </a> </td> <td> Checks for float conversion overflow (rule partially covered) </td> </tr> <tr> <td> <a> PRQA QA-C </a> </td> <td> 9.7 </td> <td> <strong>4450, 4451, 4452, 4453,4454, </strong> <strong>4462, </strong> <strong>4465</strong> </td> <td> Partially implemented </td> </tr> <tr> <td> <a> PRQA QA-C++ </a> </td> <td> 4.4 </td> <td> <strong>3011 </strong> </td> <td> </td> </tr> <tr> <td> <a> PVS-Studio </a> </td> <td> 7.23 </td> <td> <strong>V615<a></a></strong> , <strong>V2003<a></a></strong> , <strong><a>V2004</a></strong> </td> <td> </td> </tr> <tr> <td> <a> TrustInSoft Analyzer </a> </td> <td> 1.38 </td> <td> <strong>float_to_int</strong> </td> <td> Exhaustively verified (see <a> one compliant and one non-compliant example </a> ). </td> </tr> </tbody> </table>
137+
138+
139+
## Related Vulnerabilities
140+
141+
Search for [vulnerabilities](https://wiki.sei.cmu.edu/confluence/display/c/BB.+Definitions#BB.Definitions-vulnerability) resulting from the violation of this rule on the [CERT website](https://www.kb.cert.org/vulnotes/bymetric?searchview&query=FIELD+KEYWORDS+contains+FLP34-C).
142+
143+
## Related Guidelines
144+
145+
[Key here](https://wiki.sei.cmu.edu/confluence/display/c/How+this+Coding+Standard+is+Organized#HowthisCodingStandardisOrganized-RelatedGuidelines) (explains table format and definitions)
146+
147+
<table> <tbody> <tr> <th> Taxonomy </th> <th> Taxonomy item </th> <th> Relationship </th> </tr> <tr> <td> <a> CERT Oracle Secure Coding Standard for Java </a> </td> <td> <a> NUM12-J. Ensure conversions of numeric types to narrower types do not result in lost or misinterpreted data </a> </td> <td> Prior to 2018-01-12: CERT: Unspecified Relationship </td> </tr> <tr> <td> <a> ISO/IEC TR 24772:2013 </a> </td> <td> Numeric Conversion Errors \[FLC\] </td> <td> Prior to 2018-01-12: CERT: Unspecified Relationship </td> </tr> <tr> <td> <a> CWE 2.11 </a> </td> <td> <a> CWE-681 </a> , Incorrect Conversion between Numeric Types </td> <td> 2017-06-29: CERT: Rule subset of CWE </td> </tr> <tr> <td> <a> CWE 2.11 </a> </td> <td> <a> CWE-197 </a> </td> <td> 2017-06-14: CERT: Rule subset of CWE </td> </tr> </tbody> </table>
148+
149+
150+
## CERT-CWE Mapping Notes
151+
152+
[Key here](https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pageId=87152408#HowthisCodingStandardisOrganized-CERT-CWEMappingNotes) for mapping notes
153+
154+
**CWE-197 and FLP34-C**
155+
156+
Independent( FLP34-C, INT31-C) FIO34-C = Subset( INT31-C)
157+
158+
CWE-197 = Union( FLP34-C, INT31-C)
159+
160+
**CWE-195 and FLP34-C**
161+
162+
Intersection( CWE-195, FLP34-C) = Ø
163+
164+
Both conditions involve type conversion. However, CWE-195 explicitly focuses on conversions between unsigned vs signed types, whereas FLP34-C focuses on floating-point arithmetic.
165+
166+
**CWE-681 and FLP34-C**
167+
168+
CWE-681 = Union( FLP34-C, INT31-C)
169+
170+
## Bibliography
171+
172+
<table> <tbody> <tr> <td> \[ <a> IEEE 754 2006 </a> \] </td> <td> </td> </tr> <tr> <td> \[ <a> ISO/IEC 9899:2011 </a> \] </td> <td> Subclause 6.3.1.4, "Real Floating and Integer" Subclause 6.3.1.5, "Real Floating Types" </td> </tr> </tbody> </table>
173+
174+
175+
## Implementation notes
176+
177+
None
178+
179+
## References
180+
181+
* CERT-C: [FLP34-C: Ensure that floating-point conversions are within range of the new type](https://wiki.sei.cmu.edu/confluence/display/c)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @id c/cert/unchecked-floating-point-conversion
3+
* @name FLP34-C: Ensure that floating-point conversions are within range of the new type
4+
* @description
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity error
8+
* @tags external/cert/id/flp34-c
9+
* correctness
10+
* external/cert/obligation/rule
11+
*/
12+
13+
import cpp
14+
import codingstandards.c.cert
15+
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
16+
import semmle.code.cpp.controlflow.Guards
17+
18+
/*
19+
* There are three cases to consider under this rule:
20+
* 1) Float-to-int
21+
* 2) Narrowing float-to-float conversions
22+
* 3) Int-to-float
23+
*
24+
* The first results in undefined behaviour if the float is outside the range of the int, and is
25+
* the topic of this query.
26+
*
27+
* The second two cases only cause undefined behaviour if the floating point format does not
28+
* support -inf/+inf. This information is not definitively present in the CodeQL database. The
29+
* macro INFINITY in principle differs in the two cases, but we are unable to distinguish one case
30+
* from the other.
31+
*
32+
* (2) and (3) do not appear to be problems in practice on the hardware targets and compilers we
33+
* support, because they all provide +inf and -inf unconditionally.
34+
*/
35+
36+
/**
37+
* A function whose name is suggestive that it counts the number of bits set.
38+
*/
39+
class PopCount extends Function {
40+
PopCount() { this.getName().toLowerCase().matches("%popc%nt%") }
41+
}
42+
43+
/**
44+
* A macro which is suggestive that it is used to determine the precision of an integer.
45+
*/
46+
class PrecisionMacro extends Macro {
47+
PrecisionMacro() { this.getName().toLowerCase().matches("precision") }
48+
}
49+
50+
bindingset[value]
51+
predicate withinIntegralRange(IntegralType typ, float value) {
52+
exists(float lb, float ub, float limit |
53+
limit = 2.pow(8 * typ.getSize()) and
54+
(
55+
if typ.isUnsigned()
56+
then (
57+
lb = 0 and ub = limit - 1
58+
) else (
59+
lb = -limit / 2 and
60+
ub = (limit / 2) - 1
61+
)
62+
) and
63+
value >= lb and
64+
value <= ub
65+
)
66+
}
67+
68+
from FloatingPointToIntegralConversion c, ArithmeticType underlyingTypeAfter
69+
where
70+
not isExcluded(c, FloatingTypesPackage::uncheckedFloatingPointConversionQuery()) and
71+
underlyingTypeAfter = c.getUnderlyingType() and
72+
not (
73+
// Either the upper or lower bound of the expression is outside the range of the new type
74+
withinIntegralRange(underlyingTypeAfter, [upperBound(c.getExpr()), lowerBound(c.getExpr())])
75+
or
76+
// Heuristic - is there are guard the abs value of the float can fit in the precision of an int?
77+
exists(GuardCondition gc, FunctionCall log2f, FunctionCall fabsf, Expr precision |
78+
// gc.controls(c, false) and
79+
log2f.getTarget().hasGlobalOrStdName("log2f") and
80+
fabsf.getTarget().hasGlobalOrStdName("fabsf") and
81+
log2f.getArgument(0) = fabsf and
82+
// Precision is either a macro expansion or function call
83+
(
84+
precision.(FunctionCall).getTarget() instanceof PopCount
85+
or
86+
precision = any(PrecisionMacro pm).getAnInvocation().getExpr()
87+
) and
88+
gc.ensuresLt(precision, log2f, 0, c.getExpr().getBasicBlock(), false)
89+
)
90+
)
91+
select c, "Conversion of float to integer without appropriate guards avoiding undefined behavior."
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.c:8:11:8:11 | (int)... | Conversion of float to integer without appropriate guards avoiding undefined behavior. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/FLP34-C/UncheckedFloatingPointConversion.ql

c/cert/test/rules/FLP34-C/test.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stddef.h>
2+
#include <stdint.h>
3+
#include <limits.h>
4+
#include <math.h>
5+
#include <float.h>
6+
7+
void test_no_guard(float f) {
8+
int i = f; // NON_COMPLIANT
9+
}
10+
11+
void test_fixed_narrow_range(float f) {
12+
if (f > 0.0f && f < 100.0f) {
13+
int i = f; // COMPLIANT
14+
}
15+
}
16+
17+
/* Returns the number of set bits */
18+
size_t popcount(uintmax_t num) {
19+
size_t precision = 0;
20+
while (num != 0) {
21+
if (num % 2 == 1) {
22+
precision++;
23+
}
24+
num >>= 1;
25+
}
26+
return precision;
27+
}
28+
#define PRECISION(umax_value) popcount(umax_value)
29+
30+
void test_precision_check(float f) {
31+
if (isnan(f) ||
32+
PRECISION(INT_MAX) < log2f(fabsf(f)) ||
33+
(f != 0.0F && fabsf(f) < FLT_MIN)) {
34+
/* Handle error */
35+
} else {
36+
int i= f; // COMPLIANT
37+
}
38+
}

0 commit comments

Comments
 (0)