sanitisers-guide

Back to attempted double-free

Simple double free

The Code

Here is the code for this example (source):

// Created for COMP2521 UNSW sanitisers guide
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *zeroes = calloc(10, sizeof(int));
    free(zeroes);
    free(zeroes);
}

This code allocates an array of zeroes, then frees this array, then attempts to free it again.

The Error

Here is the error message:

error message

The Problem

From the error message, we can see that the error comes from trying to free the zeroes() array twice - once on line 7 and then again on line 8.

The Fix

Remove one of the free()s.