Skip to content

Commit d2cefd2

Browse files
committed
False positive fix regarding common type check idioms.
1 parent 1dd488b commit d2cefd2

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

cpp/ql/src/Microsoft/Likely Bugs/SizeOfMisuse/ArgumentIsSizeofOrOperation.ql

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,46 @@
1111
import cpp
1212
import SizeOfTypeUtils
1313

14+
predicate isIgnorableBinaryOperation(BinaryOperation op) {
15+
// FP case: precompilation type checking idiom of the form:
16+
// sizeof((type *)0 == (ptr))
17+
op instanceof EqualityOperation and
18+
exists(Literal zeroOperand, Expr other, Type t |
19+
other = op.getAnOperand() and
20+
other != zeroOperand and
21+
zeroOperand = op.getAnOperand() and
22+
zeroOperand.getValue().toInt() = 0 and
23+
zeroOperand.getImplicitlyConverted().hasExplicitConversion() and
24+
zeroOperand.getExplicitlyConverted().getUnspecifiedType() = t and
25+
// often 'NULL' is defined as (void *)0, ignore these cases
26+
not t instanceof VoidPointerType and
27+
(
28+
// Apparently derived types, eg., functoin pointers, aren't PointerType
29+
// according to codeql, so special casing them out here.
30+
other.getUnspecifiedType() instanceof DerivedType
31+
or
32+
other.getUnspecifiedType() instanceof PointerType
33+
)
34+
)
35+
}
36+
37+
class CandidateOperation extends Operation {
38+
CandidateOperation() {
39+
// For now only considering binary operations
40+
// TODO: Unary operations may be of interest but need special care
41+
// as pointer deref, and address-of are unary operations.
42+
// It is therefore more likely to get false positives if unary operations are included.
43+
// To be considered in the future.
44+
this instanceof BinaryOperation and
45+
not isIgnorableBinaryOperation(this)
46+
}
47+
}
48+
1449
from CandidateSizeofCall sizeofExpr, string message, Expr op
1550
where
1651
exists(string tmpMsg |
1752
(
18-
op instanceof BinaryOperation and tmpMsg = "binary operator"
53+
op instanceof CandidateOperation and tmpMsg = "binary operator"
1954
or
2055
op instanceof SizeofOperator and tmpMsg = "sizeof"
2156
) and

0 commit comments

Comments
 (0)