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

Skip to content

Commit 7a877bf

Browse files
committed
CPP: Add exception for wrapped whitelisted functions.
1 parent f983391 commit 7a877bf

3 files changed

Lines changed: 17 additions & 4 deletions

File tree

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @precision medium
99
* @tags correctness
1010
*/
11+
1112
import cpp
1213

1314
predicate whitelist(string fName) {
@@ -42,10 +43,23 @@ predicate whitelistPow(FunctionCall fc) {
4243
)
4344
}
4445

46+
predicate whiteListWrapped(FunctionCall fc) {
47+
whitelist(fc.getTarget().getName()) or
48+
whitelistPow(fc) or
49+
exists(ReturnStmt rs |
50+
rs.getEnclosingFunction() = fc.getTarget() and
51+
whiteListWrapped(rs.getExpr())
52+
) or
53+
exists(ReturnStmt rs, Variable v |
54+
rs.getEnclosingFunction() = fc.getTarget() and
55+
rs.getExpr().(VariableAccess).getTarget() = v and
56+
whiteListWrapped(v.getAnAssignedValue())
57+
)
58+
}
59+
4560
from FunctionCall c, FloatingPointType t1, IntegralType t2
4661
where t1 = c.getTarget().getType().getUnderlyingType() and
4762
t2 = c.getActualType() and
4863
c.hasImplicitConversion() and
49-
not whitelist(c.getTarget().getName()) and
50-
not whitelistPow(c)
64+
not whiteListWrapped(c)
5165
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@
77
| test.cpp:101:10:101:12 | call to pow | Return value of type double is implicitly converted to int here. |
88
| test.cpp:103:10:103:12 | call to pow | Return value of type double is implicitly converted to int here. |
99
| test.cpp:105:10:105:12 | call to pow | Return value of type double is implicitly converted to int here. |
10-
| test.cpp:118:10:118:16 | call to myRound | Return value of type double is implicitly converted to int here. |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,5 @@ double myRound(double v)
115115

116116
void test3()
117117
{
118-
int i = myRound(1.5); // GOOD [FALSE POSITIVE]
118+
int i = myRound(1.5); // GOOD
119119
}

0 commit comments

Comments
 (0)