Here is the code for this example (source):
// Allocate insufficient memory for an array
// Created for COMP2521 sanitiser guide
#include <stdlib.h>
#define N 10
int main(void) {
int *array = malloc(N);
for (int i = 0; i < N; i++) {
array[i] = i;
}
free(array);
return 0;
}
Here is the error message:
array[i] = i;
.int *array = malloc(N);
.array
was only 10 bytes.Since an integer is 4 bytes, we would expect an array of 10 integers to be 40 bytes. However, as the error message states, the array was only 10 bytes.
This means that when we try to access an element of the array that is more than 10 bytes into the array (e.g. array[3]
), we are accessing beyond what was allocated, and we get a heap-buffer-overflow
.
Remember that when allocating an array we want to allocate num_items * sizeof(item_type)
. In this case, we forgot to include the sizeof(int)
when allocating array
. We want to malloc N * sizeof(int)
bytes.