Skip to content

Commit a5d4127

Browse files
committed
Add Memory1 package files
1 parent 9b5d8b2 commit a5d4127

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import Lambdas
3636
import Literals
3737
import Loops
3838
import Macros
39+
import Memory1
3940
import MoveForward
4041
import Naming
4142
import Null
@@ -95,6 +96,7 @@ newtype TCPPQuery =
9596
TLiteralsPackageQuery(LiteralsQuery q) or
9697
TLoopsPackageQuery(LoopsQuery q) or
9798
TMacrosPackageQuery(MacrosQuery q) or
99+
TMemory1PackageQuery(Memory1Query q) or
98100
TMoveForwardPackageQuery(MoveForwardQuery q) or
99101
TNamingPackageQuery(NamingQuery q) or
100102
TNullPackageQuery(NullQuery q) or
@@ -154,6 +156,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
154156
isLiteralsQueryMetadata(query, queryId, ruleId, category) or
155157
isLoopsQueryMetadata(query, queryId, ruleId, category) or
156158
isMacrosQueryMetadata(query, queryId, ruleId, category) or
159+
isMemory1QueryMetadata(query, queryId, ruleId, category) or
157160
isMoveForwardQueryMetadata(query, queryId, ruleId, category) or
158161
isNamingQueryMetadata(query, queryId, ruleId, category) or
159162
isNullQueryMetadata(query, queryId, ruleId, category) or
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @id cpp/misra/pointer-arithmetic-forms-an-invalid-pointer
3+
* @name RULE-8-7-1: Pointer arithmetic shall not form an invalid pointer.
4+
* @description Pointers obtained as result of performing arithmetic should point to an initialized
5+
* object, or an element right next to the last element of an array.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-8-7-1
10+
* scope/system
11+
* external/misra/enforcement/undecidable
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.cpp.misra
17+
import semmle.code.cpp.dataflow.new.DataFlow
18+
19+
module TrackArrayConfig implements DataFlow::ConfigSig {
20+
predicate isSource(DataFlow::Node node) {
21+
/* 1. Declaring an array-type variable */
22+
none()
23+
or
24+
/* 2. Allocating dynamic memory as an array */
25+
none()
26+
}
27+
28+
predicate isSink(DataFlow::Node node) {
29+
/* 1. Pointer arithmetic */
30+
none()
31+
or
32+
/* 2. Array access */
33+
none()
34+
}
35+
}
36+
37+
module TrackArray = DataFlow::Global<TrackArrayConfig>;
38+
39+
from Expr expr
40+
where
41+
not isExcluded(expr, Memory1Package::pointerArithmeticFormsAnInvalidPointerQuery()) and
42+
none() // TODO
43+
select "TODO", "TODO"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
No expected results have yet been specified
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-8-7-1/PointerArithmeticFormsAnInvalidPointer.ql
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <cstdlib>
2+
3+
void f1(int *array) {
4+
/* 1. Pointer formed from performing arithmetic */
5+
int *valid1 = array; // COMPLIANT: pointer is within boundary
6+
int *valid2 = array + 1; // COMPLIANT: pointer is within boundary
7+
int *valid3 = array + 2; // COMPLIANT: pointer is within boundary
8+
int *valid4 =
9+
array + 3; // COMPLIANT: pointer points one beyond the last element
10+
int *invalid1 =
11+
array +
12+
4; // NON_COMPLIANT: pointer points more than one beyond the last element
13+
int *invalid2 = array - 1; // NON_COMPLIANT: pointer is outside boundary
14+
}
15+
16+
void f2(int *array) {
17+
/* 2. Array Access (entails pointer arithmetic) */
18+
int valid1 = array[0]; // COMPLIANT: pointer is within boundary
19+
int valid2 = array[1]; // COMPLIANT: pointer is within boundary
20+
int valid3 = array[2]; // COMPLIANT: pointer is within boundary
21+
int valid4 = array[3]; // COMPLIANT: pointer points one beyond the last
22+
// element, but non-compliant to Rule 4.1.3
23+
int invalid1 = array[4]; // NON_COMPLIANT: pointer points more than one beyond
24+
// the last element
25+
int invalid2 = array[-1]; // NON_COMPLIANT: pointer is outside boundary
26+
}
27+
28+
void f1_realloc(int *array) {
29+
/* 1. Pointer formed from performing arithmetic */
30+
int *valid1 = array; // COMPLIANT: pointer is within boundary
31+
int *valid2 = array + 1; // COMPLIANT: pointer is within boundary
32+
int *valid3 = array + 2; // COMPLIANT: pointer is within boundary
33+
int *valid4 =
34+
array + 3; // COMPLIANT: pointer points one beyond the last element
35+
int *invalid1 =
36+
array +
37+
4; // NON_COMPLIANT: pointer points more than one beyond the last element
38+
int *invalid2 = array - 1; // NON_COMPLIANT: pointer is outside boundary
39+
}
40+
41+
void f2_realloc(int *array) {
42+
/* 2. Array Access (entails pointer arithmetic) */
43+
int valid1 = array[0]; // COMPLIANT: pointer is within boundary
44+
int valid2 = array[1]; // COMPLIANT: pointer is within boundary
45+
int valid3 = array[2]; // COMPLIANT: pointer points one beyond the last
46+
int invalid1 = array[3]; // NON_COMPLIANT: pointer points one beyond the last
47+
// element, but non-compliant to Rule 4.1.3
48+
int invalid2 = array[4]; // NON_COMPLIANT: pointer points more than one beyond
49+
// the last element
50+
int invalid3 = array[-1]; // NON_COMPLIANT: pointer is outside boundary
51+
}
52+
53+
int main() {
54+
int array[3] = {0, 1, 2};
55+
56+
f1(array);
57+
f2(array);
58+
59+
int num_of_elements = 3;
60+
61+
int* array_malloc = (int*)std::malloc(num_of_elements * sizeof(int));
62+
int* array_calloc = (int*)std::calloc(num_of_elements, sizeof(int));
63+
64+
int new_num_of_elements = 2;
65+
66+
int* array_realloc = (int*)std::realloc(array_malloc, new_num_of_elements * sizeof(int));
67+
68+
f1(array_malloc);
69+
f2(array_malloc);
70+
71+
f1(array_calloc);
72+
f2(array_calloc);
73+
74+
f1_realloc(array_realloc);
75+
f2_realloc(array_realloc);
76+
77+
return 0;
78+
}

0 commit comments

Comments
 (0)