Skip to content

Commit 1286958

Browse files
committed
Move flowPath to srcSinkLengthMap and add stack_only.cpp
1 parent ae922a7 commit 1286958

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

cpp/misra/src/rules/RULE-8-7-1/Experimental.ql

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,21 @@ class FatPointer extends TFatPointer {
284284
predicate srcSinkLengthMap(
285285
FatPointer start, FatPointer end, int srcOffset, int sinkOffset, int length
286286
) {
287-
srcOffset = start.getOffset() and
288-
sinkOffset = end.getOffset() and
289-
(
290-
/* Base case: The object is allocated and a fat pointer created. */
291-
length = start.getLength()
292-
or
293-
/* Recursive case: A fat pointer is derived from a fat pointer. */
294-
srcSinkLengthMap(_, start, _, srcOffset, length)
287+
exists(TrackArray::PathNode src, TrackArray::PathNode sink |
288+
TrackArray::flowPath(src, sink) and
289+
/* Reiterate the data flow configuration here. */
290+
src.getNode() = start.getNode() and
291+
sink.getNode().asExpr() = end.getBasePointer()
292+
|
293+
srcOffset = start.getOffset() and
294+
sinkOffset = end.getOffset() and
295+
(
296+
/* Base case: The object is allocated and a fat pointer created. */
297+
length = start.getLength()
298+
or
299+
/* Recursive case: A fat pointer is derived from a fat pointer. */
300+
srcSinkLengthMap(_, start, _, srcOffset, length)
301+
)
295302
)
296303
}
297304

@@ -318,10 +325,6 @@ where
318325
not isExcluded(sink.getNode().asExpr(),
319326
Memory1Package::pointerArithmeticFormsAnInvalidPointerQuery()) and
320327
exists(FatPointer start, FatPointer end, int srcOffset, int sinkOffset, int length |
321-
TrackArray::flowPath(src, sink) and
322-
/* Reiterate the data flow configuration here. */
323-
src.getNode() = start.getNode() and
324-
sink.getNode().asExpr() = end.getBasePointer() and
325328
srcSinkLengthMap(start, end, srcOffset, sinkOffset, length) and
326329
(
327330
srcOffset + sinkOffset < 0 or // Underflow detection
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <cstdlib>
2+
#include <cstring>
3+
#include <ctime>
4+
#include <cwchar>
5+
6+
void stack_allocated_single_dimensional_pointer_arithmetic(int *array) {
7+
/* 1. Pointer formed from performing arithmetic */
8+
int *valid1 = array; // COMPLIANT: pointer is within boundary
9+
int *valid2 = array + 1; // COMPLIANT: pointer is within boundary
10+
int *valid3 = array + 2; // COMPLIANT: pointer is within boundary
11+
int *valid4 =
12+
array + 3; // COMPLIANT: pointer points one beyond the last element
13+
int *invalid1 =
14+
array +
15+
4; // NON_COMPLIANT: pointer points more than one beyond the last element
16+
int *invalid2 = array - 1; // NON_COMPLIANT: pointer is outside boundary
17+
}
18+
19+
void stack_allocated_single_dimensional_array_access(int *array) {
20+
/* 2. Array Access (entails pointer arithmetic) */
21+
int valid1 = array[0]; // COMPLIANT: pointer is within boundary
22+
int valid2 = array[1]; // COMPLIANT: pointer is within boundary
23+
int valid3 = array[2]; // COMPLIANT: pointer is within boundary
24+
int valid4 = array[3]; // COMPLIANT: pointer points one beyond the last
25+
// element, but non-compliant to Rule 4.1.3
26+
int invalid1 = array[4]; // NON_COMPLIANT: pointer points more than one beyond
27+
// the last element
28+
int invalid2 = array[-1]; // NON_COMPLIANT: pointer is outside boundary
29+
}
30+
31+
int main(int argc, char *argv[]) {
32+
/* 1. Single-dimensional array initialized on the stack */
33+
int stack_single_dimensional_array[3] = {0, 1, 2};
34+
35+
stack_allocated_single_dimensional_pointer_arithmetic(
36+
stack_single_dimensional_array);
37+
stack_allocated_single_dimensional_array_access(
38+
stack_single_dimensional_array);
39+
40+
return 0;
41+
}

0 commit comments

Comments
 (0)