C++: Remove potential FPs from cpp/integer-multiplication-cast-to-long in BMN

This commit is contained in:
Calum Grant
2025-01-23 19:45:44 +00:00
parent d3e469f989
commit 60076dc8a8
3 changed files with 12 additions and 5 deletions

View File

@@ -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() + "'."

View File

@@ -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
}

View File

@@ -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'. |