Skip to content

Commit 2c36364

Browse files
committed
Implement INT36-C
1 parent 0b242cb commit 2c36364

File tree

2 files changed

+88
-17
lines changed

2 files changed

+88
-17
lines changed

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

Lines changed: 77 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,87 @@
1313
import cpp
1414
import codingstandards.c.cert
1515

16-
/* 1. Declaring an integer variable to hold a pointer value */
17-
predicate integerVariableWithPointerValue(Variable var) {
18-
var.getUnderlyingType() instanceof IntType and
19-
var.getAnAssignedValue().getUnderlyingType() instanceof PointerType
16+
class LiteralZero extends Literal {
17+
LiteralZero() { this.getValue() = "0" }
2018
}
2119

22-
/* 2. Assigning an integer variable a pointer a pointer value */
23-
predicate assigningPointerValueToInteger(Assignment assign) {
24-
assign.getLValue().getUnderlyingType() instanceof IntType and
25-
assign.getRValue().getUnderlyingType() instanceof PointerType
20+
class StdIntIntPtrType extends IntPointerType {
21+
StdIntIntPtrType() {
22+
this.getFile().(HeaderFile).getBaseName() = "stdint.h" and
23+
this.getName().regexpMatch("u?intptr_t")
24+
}
2625
}
2726

28-
/* 3. Casting a pointer value to integer */
29-
predicate castingPointerToInteger(Cast cast) {
30-
cast.getExpr().getUnderlyingType() instanceof PointerType and
31-
cast.getUnderlyingType() instanceof PointerType
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
3248
}
3349

34-
from Variable x
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
69+
}
70+
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."
78+
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."
82+
) and
83+
/* Compliant exception 1: literal 0 */
84+
not cast.getExpr() instanceof LiteralZero and
85+
/* Compliant exception 2: variable's declared type is (u)intptr_t */
86+
not cast.getUnderlyingType() instanceof StdIntIntPtrType
87+
}
88+
89+
from Element elem, string message
3590
where
36-
not isExcluded(x, TypesPackage::convertingAPointerToIntegerOrIntegerToPointerQuery()) and
37-
x.getType() instanceof PointerType
38-
select x, x.getType().getAPrimaryQlClass()
91+
not isExcluded(elem, TypesPackage::convertingAPointerToIntegerOrIntegerToPointerQuery()) and
92+
(
93+
integerVariableWithPointerValue(elem, message)
94+
or
95+
assigningPointerValueToInteger(elem, message)
96+
or
97+
castingPointerToInteger(elem, message)
98+
)
99+
select elem, message
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
No expected results have yet been specified
1+
| test.c:17:8:17:19 | int_pointer3 | Pointer variable int_pointer3 is declared as an expression 28036591, which is of integer type. |
2+
| test.c:19:3:20:16 | ... = ... | Pointer variable int_pointer3 is assigned an expression 28036591, which is of integer type. |
3+
| test.c:21:8:21:19 | int_pointer4 | Pointer variable int_pointer4 is declared as an expression integer1, which is of integer type. |
4+
| test.c:26:8:26:23 | integer_address5 | Pointer variable integer_address5 is declared as an expression 28036591, which is of integer type. |
5+
| test.c:27:7:27:23 | (int *)... | Integer expression 28036591 is cast to a pointer type. |
6+
| test.c:28:8:28:23 | integer_address6 | Pointer variable integer_address6 is declared as an expression integer1, which is of integer type. |
7+
| test.c:29:7:29:21 | (int *)... | Integer expression integer1 is cast to a pointer type. |
8+
| test.c:34:7:34:22 | integer_address8 | Integer variable integer_address8 is declared as an expression & ..., which is of a pointer type. |
9+
| test.c:36:3:36:30 | ... = ... | Integer variable integer_address8 is assigned an expression & ..., which is of a pointer type. |
10+
| test.c:38:7:38:21 | integer_address | Integer variable integer_address is declared as an expression & ..., which is of a pointer type. |
11+
| test.c:39:7:39:20 | (int)... | Pointer expression & ... is cast to integer type. |

0 commit comments

Comments
 (0)