|
13 | 13 | import cpp
|
14 | 14 | import codingstandards.c.cert
|
15 | 15 |
|
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" } |
20 | 18 | }
|
21 | 19 |
|
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 | + } |
26 | 25 | }
|
27 | 26 |
|
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 |
32 | 48 | }
|
33 | 49 |
|
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 |
35 | 90 | 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 |
0 commit comments