|
| 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 = |
| 17 | + array - 1; // NON_COMPLIANT: pointer is outside boundary [FALSE_NEGATIVE] |
| 18 | +} |
| 19 | + |
| 20 | +void stack_allocated_single_dimensional_array_access(int *array) { |
| 21 | + /* 2. Array Access (entails pointer arithmetic) */ |
| 22 | + int valid1 = array[0]; // COMPLIANT: pointer is within boundary |
| 23 | + int valid2 = array[1]; // COMPLIANT: pointer is within boundary |
| 24 | + int valid3 = array[2]; // COMPLIANT: pointer is within boundary |
| 25 | + int valid4 = array[3]; // COMPLIANT: pointer points one beyond the last |
| 26 | + // element, but non-compliant to Rule 4.1.3 |
| 27 | + int invalid1 = array[4]; // NON_COMPLIANT: pointer points more than one beyond |
| 28 | + // the last element |
| 29 | + int invalid2 = |
| 30 | + array[-1]; // NON_COMPLIANT: pointer is outside boundary [FALSE_NEGATIVE] |
| 31 | +} |
| 32 | + |
| 33 | +void malloc_single_dimensional_pointer_arithmetic(int *array) { // [1, 4] |
| 34 | + /* 1. Pointer formed from performing arithmetic */ |
| 35 | + int *valid1 = array; // COMPLIANT: pointer is within boundary (lower bound: 1) |
| 36 | + int *valid2 = array + 1; // COMPLIANT: pointer points more than one beyond the |
| 37 | + // last element (lower bound: 1) |
| 38 | + int *valid3 = array + 2; // NON_COMPLIANT: pointer points more than one beyond |
| 39 | + // the last element (lower bound: 1) |
| 40 | + int *valid4 = array + 3; // NON_COMPLIANT: pointer points more than one beyond |
| 41 | + // the last element (lower bound: 1) |
| 42 | + int *invalid1 = array + 4; // NON_COMPLIANT: pointer points more than one |
| 43 | + // beyond the last element (lower bound: 1) |
| 44 | + int *invalid2 = array + 5; // NON_COMPLIANT: pointer points more than one |
| 45 | + // beyond the last element (lower bound: 1) |
| 46 | + int *invalid3 = |
| 47 | + array - 1; // NON_COMPLIANT: pointer is outside boundary [FALSE_NEGATIVE] |
| 48 | +} |
| 49 | + |
| 50 | +void malloc_single_dimensional_array_access(int *array) { // [1, 4] |
| 51 | + /* 2. Array Access (entails pointer arithmetic) */ |
| 52 | + int valid1 = |
| 53 | + array[0]; // COMPLIANT: pointer is within boundary (lower bound: 1) |
| 54 | + int valid2 = array[1]; // COMPLIANT: pointer points more than one beyond the |
| 55 | + // last element, but non-compliant to Rule 4.1.3 (lower |
| 56 | + // bound: 1) |
| 57 | + int valid3 = array[2]; // NON_COMPLIANT: pointer points more than one beyond |
| 58 | + // the last element (lower bound: 1) |
| 59 | + int valid4 = array[3]; // NON_COMPLIANT: pointer points more than one beyond |
| 60 | + // the last element (lower bound: 1) |
| 61 | + int invalid1 = array[4]; // NON_COMPLIANT: pointer points more than one beyond |
| 62 | + // the last element (lower bound: 1) |
| 63 | + int invalid2 = array[-1]; // NON_COMPLIANT: pointer is outside boundary |
| 64 | +} |
| 65 | + |
| 66 | +void calloc_single_dimensional_pointer_arithmetic(int *array) { // [2, 5] |
| 67 | + /* 1. Pointer formed from performing arithmetic */ |
| 68 | + int *valid1 = array; // COMPLIANT: pointer is within boundary (lower bound: 2) |
| 69 | + int *valid2 = |
| 70 | + array + 1; // COMPLIANT: pointer is within boundary (lower bound: 2) |
| 71 | + int *valid3 = array + 2; // COMPLIANT: pointer points more than one beyond the |
| 72 | + // last element, but non-compliant to Rule 4.1.3 |
| 73 | + // (lower bound: 2) |
| 74 | + int *valid4 = array + 3; // NON_COMPLIANT: pointer points more than one beyond |
| 75 | + // the last element (lower bound: 2) |
| 76 | + int *invalid1 = array + 4; // NON_COMPLIANT: pointer points more than one |
| 77 | + // beyond the last element (lower bound: 2) |
| 78 | + int *invalid2 = |
| 79 | + array - 1; // NON_COMPLIANT: pointer is outside boundary [FALSE_NEGATIVE] |
| 80 | +} |
| 81 | + |
| 82 | +void calloc_single_dimensional_array_access(int *array) { // [2, 5] |
| 83 | + /* 2. Array Access (entails pointer arithmetic) */ |
| 84 | + int valid1 = array[0]; // COMPLIANT: pointer is within boundary |
| 85 | + int valid2 = array[1]; // COMPLIANT: pointer is within boundary |
| 86 | + int valid3 = array[2]; // COMPLIANT: pointer points more than one beyond the |
| 87 | + // last element, but non-compliant to Rule 4.1.3 |
| 88 | + // (lower bound: 2) |
| 89 | + int valid4 = array[3]; // NON_COMPLIANT: pointer points more than one beyond |
| 90 | + // the last element (lower bound: 2) |
| 91 | + int invalid1 = array[4]; // NON_COMPLIANT: pointer points more than one |
| 92 | + // beyond the last element (lower bound: 2) |
| 93 | + int invalid2 = |
| 94 | + array[-1]; // NON_COMPLIANT: pointer is outside boundary [FALSE_NEGATIVE] |
| 95 | +} |
| 96 | + |
| 97 | +void realloc_single_dimensional_pointer_arithmetic(int *array) { // [3, 6] |
| 98 | + /* 1. Pointer formed from performing arithmetic */ |
| 99 | + int *valid1 = array; // COMPLIANT: pointer is within boundary (lower bound: 3) |
| 100 | + int *valid2 = |
| 101 | + array + 1; // COMPLIANT: pointer is within boundary (lower bound: 3) |
| 102 | + int *valid3 = |
| 103 | + array + 2; // COMPLIANT: pointer is within boundary (lower bound: 3) |
| 104 | + int *valid4 = array + 3; // COMPLIANT: pointer points one beyond the last |
| 105 | + // element (lower bound: 3) |
| 106 | + int *invalid1 = array + 4; // NON_COMPLIANT: pointer points more than one |
| 107 | + // beyond the last element (lower bound: 3) |
| 108 | + int *invalid2 = |
| 109 | + array - 1; // NON_COMPLIANT: pointer is outside boundary [FALSE_NEGATIVE] |
| 110 | +} |
| 111 | + |
| 112 | +void realloc_single_dimensional_array_access(int *array) { // [3, 6] |
| 113 | + /* 2. Array Access (entails pointer arithmetic) */ |
| 114 | + int valid1 = |
| 115 | + array[0]; // COMPLIANT: pointer is within boundary (lower bound: 3) |
| 116 | + int valid2 = |
| 117 | + array[1]; // COMPLIANT: pointer is within boundary (lower bound: 3) |
| 118 | + int valid3 = |
| 119 | + array[2]; // COMPLIANT: pointer is within boundary (lower bound: 3) |
| 120 | + int valid4 = |
| 121 | + array[3]; // COMPLIANT: pointer points one beyond the last |
| 122 | + // element, but non-compliant to Rule 4.1.3 (lower bound: 3) |
| 123 | + int invalid1 = array[4]; // NON_COMPLIANT: pointer points more than one beyond |
| 124 | + // the last element (lower bound: 3) |
| 125 | + int invalid2 = |
| 126 | + array[-1]; // NON_COMPLIANT: pointer is outside boundary [FALSE_NEGATIVE] |
| 127 | +} |
| 128 | + |
| 129 | +void stack_allocated_multi_dimensional_array_access(int array[2][3]) { |
| 130 | + int valid11 = array[0][0]; // COMPLIANT: pointer is within boundary |
| 131 | + int valid12 = array[0][1]; // COMPLIANT: pointer is within boundary |
| 132 | + int valid13 = array[0][2]; // COMPLIANT: pointer points one beyond the last |
| 133 | + // element, but non-compliant to Rule 4.1.3 |
| 134 | + int invalid1 = array[0][3]; // NON_COMPLIANT: pointer points more than one |
| 135 | + // beyond the last element |
| 136 | + |
| 137 | + int valid21 = array[1][0]; // COMPLIANT: pointer is within boundary |
| 138 | + int valid22 = array[1][1]; // COMPLIANT: pointer is within boundary |
| 139 | + int valid23 = array[1][2]; // COMPLIANT: pointer points one beyond the last |
| 140 | + // element, but non-compliant to Rule 4.1.3 |
| 141 | + |
| 142 | + int invalid2 = array[1][3]; // NON_COMPLIANT: pointer points more than one |
| 143 | + // beyond the last element |
| 144 | + |
| 145 | + int valid31 = array[2][0]; // COMPLIANT: pointer points one beyond the last |
| 146 | + // element, but non-compliant to Rule 4.1.3 |
| 147 | + int invalid3 = array[3][0]; // NON_COMPLIANT: pointer points more than one |
| 148 | + // beyond the last element |
| 149 | +} |
| 150 | + |
| 151 | +void stack_allocated_multi_dimensional_pointer_arithmetic(int array[2][3]) { |
| 152 | + int valid111 = *(*(array + 0) + 0); // COMPLIANT: pointer is within boundary |
| 153 | + int valid112 = *( |
| 154 | + *(array + |
| 155 | + 0)); // COMPLIANT: pointer is within boundary (equivalent to the above) |
| 156 | + int valid113 = **array; // COMPLIANT: pointer is within boundary (equivalent |
| 157 | + // to the above) |
| 158 | + int valid121 = *(*(array + 0) + 1); // COMPLIANT: pointer is within boundary |
| 159 | + int valid122 = |
| 160 | + *(*array + |
| 161 | + 1); // COMPLIANT: pointer is within boundary (equivalent to the above) |
| 162 | + int valid131 = |
| 163 | + *(*(array + 0) + 2); // COMPLIANT: pointer points one beyond the last |
| 164 | + // element, but non-compliant to Rule 4.1.3 |
| 165 | + int valid132 = *( |
| 166 | + *array + |
| 167 | + 2); // COMPLIANT: pointer points one beyond the last |
| 168 | + // element, but non-compliant to Rule 4.1.3 (equivalent to the above) |
| 169 | + int invalid11 = *(*(array + 0) + 3); // NON_COMPLIANT: pointer points more |
| 170 | + // than one beyond the last element |
| 171 | + int invalid12 = |
| 172 | + *(*array + 3); // NON_COMPLIANT: pointer points more than |
| 173 | + // one beyond the last element (equivalent to the above) |
| 174 | + |
| 175 | + int valid211 = *(*(array + 1) + 0); // COMPLIANT: pointer is within boundary |
| 176 | + int valid212 = *( |
| 177 | + *(array + |
| 178 | + 1)); // COMPLIANT: pointer is within boundary (equivalent to the above) |
| 179 | + int valid22 = *(*(array + 1) + 1); // COMPLIANT: pointer is within boundary |
| 180 | + int valid23 = |
| 181 | + *(*(array + 1) + 2); // COMPLIANT: pointer points one beyond the last |
| 182 | + // element, but non-compliant to Rule 4.1.3 |
| 183 | + int invalid2 = *(*(array + 1) + 3); // NON_COMPLIANT: pointer points more than |
| 184 | + // one beyond the last element |
| 185 | + |
| 186 | + int valid311 = |
| 187 | + *(*(array + 2) + 0); // COMPLIANT: pointer points one beyond the last |
| 188 | + // element, but non-compliant to Rule 4.1.3 |
| 189 | + int valid312 = *(*( |
| 190 | + array + |
| 191 | + 2)); // COMPLIANT: pointer points one beyond the last |
| 192 | + // element, but non-compliant to Rule 4.1.3 (equivalent to the above) |
| 193 | + int invalid31 = *(*(array + 3) + 0); // NON_COMPLIANT: pointer points more |
| 194 | + // than one beyond the last element |
| 195 | + int invalid32 = |
| 196 | + *(*(array + 3)); // NON_COMPLIANT: pointer points more than |
| 197 | + // one beyond the last element (equivalent to the above) |
| 198 | +} |
| 199 | + |
| 200 | +int main(int argc, char *argv[]) { |
| 201 | + /* 1. Single-dimensional array initialized on the stack */ |
| 202 | + int stack_single_dimensional_array[3] = {0, 1, 2}; |
| 203 | + |
| 204 | + stack_allocated_single_dimensional_pointer_arithmetic( |
| 205 | + stack_single_dimensional_array); |
| 206 | + stack_allocated_single_dimensional_array_access( |
| 207 | + stack_single_dimensional_array); |
| 208 | + |
| 209 | + /* 2. Single-dimensional array initialized on the heap */ |
| 210 | + int num_of_elements_malloc; |
| 211 | + int num_of_elements_calloc; |
| 212 | + int num_of_elements_realloc; |
| 213 | + |
| 214 | + if (argc) { |
| 215 | + num_of_elements_malloc = 1; |
| 216 | + num_of_elements_calloc = 2; |
| 217 | + num_of_elements_realloc = 3; |
| 218 | + } else { |
| 219 | + num_of_elements_malloc = 4; |
| 220 | + num_of_elements_calloc = 5; |
| 221 | + num_of_elements_realloc = 6; |
| 222 | + } |
| 223 | + |
| 224 | + int *single_dimensional_array_malloc = |
| 225 | + (int *)malloc(num_of_elements_malloc * sizeof(int)); |
| 226 | + int *single_dimensional_array_calloc = |
| 227 | + (int *)calloc(num_of_elements_calloc, sizeof(int)); |
| 228 | + |
| 229 | + int *single_dimensional_array_realloc = (int *)realloc( |
| 230 | + single_dimensional_array_malloc, num_of_elements_realloc * sizeof(int)); |
| 231 | + |
| 232 | + malloc_single_dimensional_pointer_arithmetic(single_dimensional_array_malloc); |
| 233 | + malloc_single_dimensional_array_access(single_dimensional_array_malloc); |
| 234 | + |
| 235 | + calloc_single_dimensional_pointer_arithmetic(single_dimensional_array_calloc); |
| 236 | + calloc_single_dimensional_array_access(single_dimensional_array_calloc); |
| 237 | + |
| 238 | + realloc_single_dimensional_pointer_arithmetic( |
| 239 | + single_dimensional_array_realloc); |
| 240 | + realloc_single_dimensional_array_access(single_dimensional_array_realloc); |
| 241 | + |
| 242 | + /* 3. Multi-dimensional array initialized on the stack */ |
| 243 | + int stack_multi_dimensional_array[2][3] = {{1, 2, 3}, {4, 5, 6}}; |
| 244 | + |
| 245 | + /* 4. Multi-dimensional array initialized on the heap */ |
| 246 | + int (*heap_multi_dimensional_array)[3] = |
| 247 | + (int (*)[3])malloc(sizeof(int[2][3])); |
| 248 | + |
| 249 | + stack_allocated_multi_dimensional_array_access(stack_multi_dimensional_array); |
| 250 | + stack_allocated_multi_dimensional_pointer_arithmetic( |
| 251 | + stack_multi_dimensional_array); |
| 252 | + |
| 253 | + return 0; |
| 254 | +} |
0 commit comments