Skip to content

Commit 3778724

Browse files
committed
Incorporate changes for INT36-C
1 parent fb1fd83 commit 3778724

File tree

1 file changed

+40
-66
lines changed

1 file changed

+40
-66
lines changed

c/cert/src/rules/INT36-C/ConvertingAPointerToIntegerOrIntegerToPointer.ql

Lines changed: 40 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -17,85 +17,59 @@ class LiteralZero extends Literal {
1717
LiteralZero() { this.getValue() = "0" }
1818
}
1919

20-
class StdIntIntPtrType extends IntPointerType {
20+
class StdIntIntPtrType extends Type {
2121
StdIntIntPtrType() {
22-
this.getFile().(HeaderFile).getBaseName() = "stdint.h" and
23-
this.getName().regexpMatch("u?intptr_t")
24-
}
25-
}
22+
exists(TypeDeclarationEntry entry |
23+
/*
24+
* Just check if there is a header file,
25+
* because we don't know what header file the declaration might live in
26+
*/
2627

27-
/* 1. Declaring an integer variable to hold a pointer value or the opposite, excluding compliant exceptions */
28-
predicate integerVariableWithPointerValue(Variable var, string message) {
29-
(
30-
// Declaring an integer variable to hold a pointer value
31-
var.getUnderlyingType() instanceof IntType and
32-
var.getAnAssignedValue().getUnderlyingType() instanceof PointerType and
33-
message =
34-
"Integer variable " + var + " is declared as an expression " + var.getAnAssignedValue() +
35-
", which is of a pointer type."
36-
or
37-
// Declaring an pointer variable to hold a integer value
38-
var.getUnderlyingType() instanceof PointerType and
39-
var.getAnAssignedValue().getUnderlyingType() instanceof IntType and
40-
message =
41-
"Pointer variable " + var + " is declared as an expression " + var.getAnAssignedValue() +
42-
", which is of integer type."
43-
) and
44-
/* Compliant exception 1: literal 0 */
45-
not var.getAnAssignedValue() instanceof LiteralZero and
46-
/* Compliant exception 2: variable's declared type is (u)intptr_t */
47-
not var.getUnderlyingType() instanceof StdIntIntPtrType
48-
}
28+
exists(entry.getFile().(HeaderFile)) and
29+
entry.getType() = this and
30+
this.getName().regexpMatch("u?intptr_t")
31+
)
32+
}
4933

50-
/* 2. Assigning an integer variable a pointer a pointer value, excluding literal 0 */
51-
predicate assigningPointerValueToInteger(Assignment assign, string message) {
52-
(
53-
assign.getLValue().getUnderlyingType() instanceof IntType and
54-
assign.getRValue().getUnderlyingType() instanceof PointerType and
55-
message =
56-
"Integer variable " + assign.getLValue() + " is assigned an expression " + assign.getRValue() +
57-
", which is of a pointer type."
58-
or
59-
assign.getLValue().getUnderlyingType() instanceof PointerType and
60-
assign.getRValue().getUnderlyingType() instanceof IntType and
61-
message =
62-
"Pointer variable " + assign.getLValue() + " is assigned an expression " + assign.getRValue() +
63-
", which is of integer type."
64-
) and
65-
/* Compliant exception 1: literal 0 */
66-
not assign.getRValue() instanceof LiteralZero and
67-
/* Compliant exception 2: variable's declared type is (u)intptr_t */
68-
not assign.getLValue().getUnderlyingType() instanceof StdIntIntPtrType
34+
override string toString() {
35+
if this.getName() = "uintptr_t" then result = "uintptr_t" else result = "intptr_t"
36+
}
6937
}
7038

71-
/* 3. Casting a pointer value to integer, excluding literal 0 */
72-
predicate castingPointerToInteger(Cast cast, string message) {
73-
not cast.isCompilerGenerated() and
74-
(
75-
cast.getExpr().getUnderlyingType() instanceof IntType and
76-
cast.getUnderlyingType() instanceof PointerType and
77-
message = "Integer expression " + cast.getExpr() + " is cast to a pointer type."
39+
/**
40+
* Casting a pointer value to integer, excluding literal 0.
41+
* Includes implicit conversions made during declarations or assignments.
42+
*/
43+
predicate conversionBetweenPointerAndInteger(Cast cast, string message) {
44+
/* Ensure that `int` has different size than that of pointers */
45+
exists(IntType intType, PointerType ptrType | intType.getSize() < ptrType.getSize() |
46+
cast.getExpr().getUnderlyingType() = intType and
47+
cast.getUnderlyingType() = ptrType and
48+
if cast.isCompilerGenerated()
49+
then message = "Integer expression " + cast.getExpr() + " is implicitly cast to a pointer type."
50+
else message = "Integer expression " + cast.getExpr() + " is cast to a pointer type."
7851
or
79-
cast.getExpr().getUnderlyingType() instanceof PointerType and
80-
cast.getUnderlyingType() instanceof IntType and
81-
message = "Pointer expression " + cast.getExpr() + " is cast to integer type."
52+
cast.getExpr().getUnderlyingType() = ptrType and
53+
cast.getUnderlyingType() = intType and
54+
if cast.isCompilerGenerated()
55+
then
56+
message = "Pointer expression " + cast.getExpr() + " is implicitly cast to an integer type."
57+
else message = "Pointer expression " + cast.getExpr() + " is cast to an integer type."
8258
) and
8359
/* Compliant exception 1: literal 0 */
8460
not cast.getExpr() instanceof LiteralZero and
8561
/* Compliant exception 2: variable's declared type is (u)intptr_t */
86-
not cast.getUnderlyingType() instanceof StdIntIntPtrType
62+
not (
63+
cast.getType() instanceof StdIntIntPtrType and
64+
cast.getExpr().getType() instanceof VoidPointerType
65+
or
66+
cast.getType() instanceof VoidPointerType and
67+
cast.getExpr().getType() instanceof StdIntIntPtrType
68+
)
8769
}
8870

8971
from Element elem, string message
9072
where
9173
not isExcluded(elem, TypesPackage::convertingAPointerToIntegerOrIntegerToPointerQuery()) and
92-
(
93-
integerVariableWithPointerValue(elem, message)
94-
or
95-
assigningPointerValueToInteger(elem, message)
96-
or
97-
castingPointerToInteger(elem, message)
98-
) and
99-
/* Ensure that `int` has different size than that of pointers */
100-
forall(IntType intType, PointerType ptrType | intType.getSize() != ptrType.getSize())
74+
conversionBetweenPointerAndInteger(elem, message)
10175
select elem, message

0 commit comments

Comments
 (0)