|
| 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