Skip to content

Commit 4732526

Browse files
authored
Merge pull request #221 from jbj/IntMultToLong-char
C++: Suppress IntMultToLong alert on char-typed numbers
2 parents 7f56be6 + a56376a commit 4732526

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

cpp/ql/src/Likely Bugs/Arithmetic/IntMultToLong.ql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@ import semmle.code.cpp.controlflow.SSA
2121
/**
2222
* Holds if `e` is either:
2323
* - a constant
24+
* - a char-typed expression, meaning it's a small number
2425
* - an array access to an array of constants
2526
* - flows from one of the above
2627
* In these cases the value of `e` is likely to be small and
2728
* controlled, so we consider it less likely to cause an overflow.
2829
*/
29-
predicate effectivelyConstant(Expr e) {
30+
predicate likelySmall(Expr e) {
3031
e.isConstant() or
32+
e.getType().getSize() <= 1 or
3133
e.(ArrayExpr).getArrayBase().getType().(ArrayType).getBaseType().isConst() or
3234
exists(SsaDefinition def, Variable v |
3335
def.getAUse(v) = e and
34-
effectivelyConstant(def.getDefiningValue(v))
36+
likelySmall(def.getDefiningValue(v))
3537
)
3638
}
3739

@@ -56,7 +58,7 @@ int getEffectiveMulOperands(MulExpr me) {
5658
result = count(Expr op |
5759
op = getMulOperand*(me) and
5860
not op instanceof MulExpr and
59-
not effectivelyConstant(op)
61+
not likelySmall(op)
6062
)
6163
}
6264

cpp/ql/test/query-tests/Likely Bugs/Arithmetic/IntMultToLong/IntMultToLong.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,7 @@ void use_printf(float f, double d)
8888
// ^ there's a float -> double varargs promotion here, but it's unlikely that the author anticipates requiring a double
8989
printf("%f", d * d); // safe
9090
}
91+
92+
size_t three_chars(unsigned char a, unsigned char b, unsigned char c) {
93+
return a * b * c; // at most 16581375
94+
}

0 commit comments

Comments
 (0)