Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cbe69f2

Browse files
committed
CPP: Fix false positive.
1 parent e26c709 commit cbe69f2

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

cpp/ql/src/Likely Bugs/Conversion/LossyFunctionResultCast.ql

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,30 @@
1010
*/
1111
import cpp
1212

13+
predicate whitelist(string fName) {
14+
fName = "ceil" or
15+
fName = "ceilf" or
16+
fName = "ceill" or
17+
fName = "floor" or
18+
fName = "floorf" or
19+
fName = "floorl" or
20+
fName = "nearbyint" or
21+
fName = "nearbyintf" or
22+
fName = "nearbyintl" or
23+
fName = "rint" or
24+
fName = "rintf" or
25+
fName = "rintl" or
26+
fName = "round" or
27+
fName = "roundf" or
28+
fName = "roundl" or
29+
fName = "trunc" or
30+
fName = "truncf" or
31+
fName = "truncl"
32+
}
33+
1334
from FunctionCall c, FloatingPointType t1, IntegralType t2
1435
where t1 = c.getTarget().getType().getUnderlyingType() and
1536
t2 = c.getActualType() and
1637
c.hasImplicitConversion() and
17-
not c.getTarget().getName() = "ceil" and
18-
not c.getTarget().getName() = "floor" and
19-
not c.getTarget().getName() = "round"
38+
not whitelist(c.getTarget().getName())
2039
select c, "Return value of type " + t1.toString() + " is implicitly converted to " + t2.toString() + " here."

cpp/ql/test/query-tests/Likely Bugs/Conversion/LossyFunctionResultCast/ImplicitDowncastFromBitfield.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,3 @@
44
| test.cpp:40:13:40:21 | call to getDouble | Return value of type double is implicitly converted to int here. |
55
| test.cpp:43:6:43:12 | call to getMyLD | Return value of type long double is implicitly converted to bool here. |
66
| test.cpp:45:13:45:19 | call to getMyLD | Return value of type long double is implicitly converted to int here. |
7-
| test.cpp:78:6:78:11 | call to roundf | Return value of type float is implicitly converted to bool here. |
8-
| test.cpp:80:13:80:18 | call to roundf | Return value of type float is implicitly converted to int here. |

cpp/ql/test/query-tests/Likely Bugs/Conversion/LossyFunctionResultCast/test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ void test1()
7575

7676
// explicit rounding
7777

78-
if (roundf(getFloat())) // [FALSE POSITIVE]
78+
if (roundf(getFloat()))
7979
{
80-
setPosInt(roundf(getFloat())); // [FALSE POSITIVE]
80+
setPosInt(roundf(getFloat()));
8181
setPosFloat(roundf(getFloat()));
8282
}
8383
if (round(getDouble()))

0 commit comments

Comments
 (0)