query-database
query-database copied to clipboard
Add undesired /dangerous type conversion queries
Add a query undesired /dangerous type conversion
for example : "int" to "short"
int foo(int iPram)
{
int iBig = 0;
short sSmall = 0;
iBig = iPram;
sSmall = (short)iBig;
return sSmall;
}
Here are two queries that I use for similar patterns.
For finding mismatched casts, similar to the one in the example:
cpg.call.name("<operator>.cast").filter(
call =>
call.argument.order(1).code != call.argument.order(2).evalType).l
For finding mismatched assignments without a cast:
cpg.call.name("<operator>.assignment").filter(
call => {
def lhs = call.argument.order(1).isIdentifier
def rhs = call.argument.order(2).isIdentifier
if (lhs.size > 0 && rhs.size > 0) lhs.head.typeFullName != rhs.head.typeFullName
else false
}).l