-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathLossyPointerCast.ql
More file actions
29 lines (26 loc) · 933 Bytes
/
LossyPointerCast.ql
File metadata and controls
29 lines (26 loc) · 933 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* @name Lossy pointer cast
* @description A pointer type is converted to a smaller integer type. This may
* lead to loss of information in the variable and is highly
* non-portable.
* @kind problem
* @problem.severity warning
* @precision high
* @id cpp/lossy-pointer-cast
* @tags reliability
* correctness
* types
*/
import cpp
predicate lossyPointerCast(Expr e, PointerType pt, IntegralType it) {
not it instanceof BoolType and
e.getConversion().getType().getUnderlyingType() = it and
e.getType().getUnderlyingType() = pt and
it.getSize() < pt.getSize() and
not e.isInMacroExpansion() and
// low bits of pointers are sometimes used to store flags
not exists(BitwiseAndExpr a | a.getAnOperand() = e)
}
from Expr e, PointerType pt, IntegralType it
where lossyPointerCast(e, pt, it)
select e, "Converted from " + pt.getName() + " to smaller type " + it.getName()