|
| 1 | +#include <cstdlib> |
| 2 | +#include <cstring> |
| 3 | +#include <ctime> |
| 4 | +#include <cwchar> |
| 5 | + |
| 6 | +void stack_allocated_multi_dimensional_array_access(int array[2][3]) { |
| 7 | + int valid11 = array[0][0]; // COMPLIANT: pointer is within boundary |
| 8 | + int valid12 = array[0][1]; // COMPLIANT: pointer is within boundary |
| 9 | + int valid13 = array[0][2]; // COMPLIANT: pointer is within boundary |
| 10 | + int valid14 = array[0][3]; // COMPLIANT: pointer points one beyond the last |
| 11 | + // element, but non-compliant to Rule 4.1.3 |
| 12 | + int invalid1 = array[0][4]; // NON_COMPLIANT: pointer points more than one |
| 13 | + // beyond the last element |
| 14 | + |
| 15 | + int valid21 = array[1][0]; // COMPLIANT: pointer is within boundary |
| 16 | + int valid22 = array[1][1]; // COMPLIANT: pointer is within boundary |
| 17 | + int valid23 = array[1][2]; // COMPLIANT: pointer is within boundary |
| 18 | + int valid24 = array[1][3]; // COMPLIANT: pointer points one beyond the last |
| 19 | + // element, but non-compliant to Rule 4.1.3 |
| 20 | + int invalid2 = array[1][4]; // NON_COMPLIANT: pointer points more than one |
| 21 | + // beyond the last element |
| 22 | + |
| 23 | + int valid31 = array[2][0]; // COMPLIANT: pointer points one beyond the last |
| 24 | + // element, but non-compliant to Rule 4.1.3 |
| 25 | + |
| 26 | + int invalid3 = array[3][0]; // NON_COMPLIANT: pointer points more than one |
| 27 | + // beyond the last element |
| 28 | +} |
| 29 | + |
| 30 | +void stack_allocated_multi_dimensional_pointer_arithmetic(int array[2][3]) { |
| 31 | + int valid111 = *(*(array + 0) + 0); // COMPLIANT: pointer is within boundary |
| 32 | + int valid112 = *( |
| 33 | + *(array + |
| 34 | + 0)); // COMPLIANT: pointer is within boundary (equivalent to the above) |
| 35 | + int valid113 = **array; // COMPLIANT: pointer is within boundary (equivalent |
| 36 | + // to the above) |
| 37 | + int valid121 = *(*(array + 0) + 1); // COMPLIANT: pointer is within boundary |
| 38 | + int valid122 = |
| 39 | + *(*array + |
| 40 | + 1); // COMPLIANT: pointer is within boundary (equivalent to the above) |
| 41 | + int valid131 = *(*(array + 0) + 2); // COMPLIANT: pointer is within boundary |
| 42 | + int valid132 = *(*array + 2); // COMPLIANT: pointer is within boundary |
| 43 | + int valid141 = *(*(array + 0) + 3); // COMPLIANT: pointer points to one beyond |
| 44 | + // the last element |
| 45 | + int valid142 = *(*array + 3); // COMPLIANT: pointer points to one beyond the |
| 46 | + // last element (equivalent to the above) |
| 47 | + int invalid11 = *(*(array + 0) + 4); // NON_COMPLIANT: pointer points more |
| 48 | + // than one beyond the last element |
| 49 | + int invalid12 = |
| 50 | + *(*array + 4); // NON_COMPLIANT: pointer points more than |
| 51 | + // one beyond the last element (equivalent to the above) |
| 52 | + |
| 53 | + int valid211 = *(*(array + 1) + 0); // COMPLIANT: pointer is within boundary |
| 54 | + int valid212 = *( |
| 55 | + *(array + |
| 56 | + 1)); // COMPLIANT: pointer is within boundary (equivalent to the above) |
| 57 | + int valid22 = *(*(array + 1) + 1); // COMPLIANT: pointer is within boundary |
| 58 | + int valid23 = *(*(array + 1) + 2); // COMPLIANT: pointer points one beyond the |
| 59 | + // last element |
| 60 | + int valid24 = |
| 61 | + *(*(array + 1) + |
| 62 | + 3); // COMPLIANT: pointer points to one beyond the last element |
| 63 | + int invalid2 = *(*(array + 1) + 4); // NON_COMPLIANT: pointer points more than |
| 64 | + // one beyond the last element |
| 65 | + |
| 66 | + int valid311 = *(*(array + 2) + 0); // COMPLIANT: pointer points one beyond |
| 67 | + // the last element |
| 68 | + int valid312 = *(*(array + 2)); // COMPLIANT: pointer points one beyond the |
| 69 | + // last element (equivalent to the above) |
| 70 | + int invalid31 = *(*(array + 3) + 0); // NON_COMPLIANT: pointer points more |
| 71 | + // than one beyond the last element |
| 72 | + int invalid32 = |
| 73 | + *(*(array + 3)); // NON_COMPLIANT: pointer points more than |
| 74 | + // one beyond the last element (equivalent to the above) |
| 75 | +} |
| 76 | + |
| 77 | +int main(int argc, char *argv[]) { |
| 78 | + int stack_multi_dimensional_array[2][3] = {{1, 2, 3}, {4, 5, 6}}; |
| 79 | + |
| 80 | + stack_allocated_multi_dimensional_array_access(stack_multi_dimensional_array); |
| 81 | + stack_allocated_multi_dimensional_pointer_arithmetic( |
| 82 | + stack_multi_dimensional_array); |
| 83 | + |
| 84 | + return 0; |
| 85 | +} |
0 commit comments