Skip to content

Commit a44af85

Browse files
committed
FLP34-C: Support all forms of fabs/log2
1 parent 2813292 commit a44af85

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

c/cert/src/rules/FLP34-C/UncheckedFloatingPointConversion.ql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,18 @@ where
7575
withinIntegralRange(underlyingTypeAfter, [upperBound(c.getExpr()), lowerBound(c.getExpr())])
7676
or
7777
// Heuristic - is there are guard the abs value of the float can fit in the precision of an int?
78-
exists(GuardCondition gc, FunctionCall log2f, FunctionCall fabsf, Expr precision |
78+
exists(GuardCondition gc, FunctionCall log2, FunctionCall fabs, Expr precision |
7979
// gc.controls(c, false) and
80-
log2f.getTarget().hasGlobalOrStdName("log2f") and
81-
fabsf.getTarget().hasGlobalOrStdName("fabsf") and
82-
log2f.getArgument(0) = fabsf and
80+
log2.getTarget().hasGlobalOrStdName("log2" + ["", "l", "f"]) and
81+
fabs.getTarget().hasGlobalOrStdName("fabs" + ["", "l", "f"]) and
82+
log2.getArgument(0) = fabs and
8383
// Precision is either a macro expansion or function call
8484
(
8585
precision.(FunctionCall).getTarget() instanceof PopCount
8686
or
8787
precision = any(PrecisionMacro pm).getAnInvocation().getExpr()
8888
) and
89-
gc.ensuresLt(precision, log2f, 0, c.getExpr().getBasicBlock(), false)
89+
gc.ensuresLt(precision, log2, 0, c.getExpr().getBasicBlock(), false)
9090
)
9191
)
9292
select c, "Conversion of float to integer without appropriate guards avoiding undefined behavior."

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,28 @@ size_t popcount(uintmax_t num) {
2828
#define PRECISION(umax_value) popcount(umax_value)
2929

3030
void test_precision_check(float f) {
31+
if (isnan(f) || PRECISION(INT_MAX) < log2(fabs(f)) ||
32+
(f != 0.0F && fabs(f) < FLT_MIN)) {
33+
/* Handle error */
34+
} else {
35+
int i = f; // COMPLIANT
36+
}
37+
}
38+
39+
void test_precision_check_double(double f) {
3140
if (isnan(f) || PRECISION(INT_MAX) < log2f(fabsf(f)) ||
3241
(f != 0.0F && fabsf(f) < FLT_MIN)) {
3342
/* Handle error */
3443
} else {
3544
int i = f; // COMPLIANT
3645
}
46+
}
47+
48+
void test_precision_check_long_double(long double f) {
49+
if (isnan(f) || PRECISION(INT_MAX) < log2l(fabsl(f)) ||
50+
(f != 0.0F && fabsl(f) < FLT_MIN)) {
51+
/* Handle error */
52+
} else {
53+
int i = f; // COMPLIANT
54+
}
3755
}

0 commit comments

Comments
 (0)