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

Skip to content

Commit f983391

Browse files
committed
CPP: Add exception for pow.
1 parent 11013b0 commit f983391

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,21 @@ predicate whitelist(string fName) {
3131
fName = "truncl"
3232
}
3333

34+
predicate whitelistPow(FunctionCall fc) {
35+
(
36+
fc.getTarget().getName() = "pow" or
37+
fc.getTarget().getName() = "powf" or
38+
fc.getTarget().getName() = "powl"
39+
) and exists(float value |
40+
value = fc.getArgument(0).getValue().toFloat() and
41+
(value.floor() - value).abs() < 0.001
42+
)
43+
}
44+
3445
from FunctionCall c, FloatingPointType t1, IntegralType t2
3546
where t1 = c.getTarget().getType().getUnderlyingType() and
3647
t2 = c.getActualType() and
3748
c.hasImplicitConversion() and
38-
not whitelist(c.getTarget().getName())
49+
not whitelist(c.getTarget().getName()) and
50+
not whitelistPow(c)
3951
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,8 +4,6 @@
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:97:10:97:12 | call to pow | Return value of type double is implicitly converted to int here. |
8-
| test.cpp:99:10:99:12 | call to pow | Return value of type double is implicitly converted to int here. |
97
| test.cpp:101:10:101:12 | call to pow | Return value of type double is implicitly converted to int here. |
108
| test.cpp:103:10:103:12 | call to pow | Return value of type double is implicitly converted to int here. |
119
| test.cpp:105:10:105:12 | call to pow | Return value of type double 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
@@ -94,9 +94,9 @@ int test2(double v, double w, int n)
9494
switch (n)
9595
{
9696
case 1:
97-
return pow(2, v); // GOOD [FALSE POSITIVE]
97+
return pow(2, v); // GOOD
9898
case 2:
99-
return pow(10, v); // GOOD [FALSE POSITIVE]
99+
return pow(10, v); // GOOD
100100
case 3:
101101
return pow(2.5, v); // BAD
102102
case 4:

0 commit comments

Comments
 (0)