mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
C++: Remove potential FPs from cpp/integer-multiplication-cast-to-long in BMN
This commit is contained in:
@@ -177,6 +177,12 @@ predicate overflows(MulExpr me, Type t) {
|
||||
)
|
||||
}
|
||||
|
||||
predicate buildModeNoneIntLongConversion(IntType argType, LongType resultType) {
|
||||
exists(argType) and
|
||||
exists(resultType) and
|
||||
exists(Compilation c | c.buildModeNone())
|
||||
}
|
||||
|
||||
from MulExpr me, Type t1, Type t2
|
||||
where
|
||||
t1 = me.getType().getUnderlyingType() and
|
||||
@@ -218,7 +224,10 @@ where
|
||||
// only report if we cannot prove that the result of the
|
||||
// multiplication will be less (resp. greater) than the
|
||||
// maximum (resp. minimum) number we can compute.
|
||||
overflows(me, t1)
|
||||
overflows(me, t1) and
|
||||
// In build mode none, many conversions from integer to long are caused by incorrect types,
|
||||
// so exclude those results
|
||||
not buildModeNoneIntLongConversion(t1, t2)
|
||||
select me,
|
||||
"Multiplication result may overflow '" + me.getType().toString() + "' before it is converted to '"
|
||||
+ me.getFullyConverted().getType().toString() + "'."
|
||||
|
||||
@@ -5,6 +5,6 @@ int f();
|
||||
void test() {
|
||||
int i = f();
|
||||
unsigned u = i;
|
||||
long j = i * i; // BAD (FP)
|
||||
unsigned long k = u * u; // BAD (FP)
|
||||
long j = i * i; // GOOD: build mode none
|
||||
unsigned long k = u * u; // GOOD: build mode none
|
||||
}
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
| IntMulToLong.c:8:12:8:16 | ... * ... | Multiplication result may overflow 'int' before it is converted to 'long'. |
|
||||
| IntMulToLong.c:9:21:9:25 | ... * ... | Multiplication result may overflow 'unsigned int' before it is converted to 'unsigned long'. |
|
||||
|
||||
Reference in New Issue
Block a user