0

I am trying to free my file name (char * pointer) but got an error :

Heap corruption detected: after normal block (#65) at 0x....

The code:

static FILE *initializeIndexFile(char *database, char **indexFileName)
{
    FILE *file1_p;
    *indexFileName = NULL;
    int len = strlen(database);
    *indexFileName = (char *)malloc(len *sizeof(char) + 1);
    strcpy(*indexFileName, database);
    file1_p = fopen(strcat(*indexFileName, ".ind"), "rb");
    if (file1_p == NULL)
        Handle_Failure();
    fclose(file1_p);
    free(*indexFileName);
    return file1_p;
}

Firstly I tought it because the file is still open so I make fclose() calling but still its got the same error.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
motis10
  • 2,484
  • 1
  • 22
  • 46
  • 1
    Standard Warning : Please [do not cast](http://stackoverflow.com/q/605845/2173917) the return value of `malloc()` and family in `C`. – Sourav Ghosh Jun 12 '15 at 07:06
  • Just wanna add, according to your cde, you don't need to get a `char **`, a simple `char *` should be enough here. This may help you debugging some stuff too – Guillaume Munsch Jun 12 '15 at 07:11
  • 1
    @LeZohan68 Unless he wants the indexFileName in caller function for some purpose. – Mohit Jain Jun 12 '15 at 07:11
  • @MohitJain if i'm not wrong, it's already a pointer. The content should be modified. And he wants to malloc over, so ... – Guillaume Munsch Jun 12 '15 at 07:14
  • 1
    @LeZohan68 Yes if he modifies the contents only. If you update a pointer (NULL, malloc etc), you are actually discarding the copy of that pointer and giving new value to your local pointer and the pointer in caller function remains same. – Mohit Jain Jun 12 '15 at 07:15

3 Answers3

2

You code is having issue in the below line

strcat(*indexFileName, ".ind")

the destination buffer at *indexFileName is having insufficient memory to hold the concatenated string. Hence it invokes undefined behaviour.

From the man page of strcat()

... If dest (destination buffer) is not large enough, program behaviour is unpredictable;

So, once it invokes UB, there is no particular behaviour you can predict or expect.

That said,

  1. Please do not cast the return value of malloc() and family in C.

  2. sizeof(char) is guranteed to be 1 by C standard. You son't need to use that.

Solution [From the deleted answer by Mr. Mohit Jain]

Revise your allocation to:

int len = strlen(database) + strlen(".ind");   //allocate enough space to hold
*indexFileName = (char *)malloc(len + 1);      // the final concatenated string
Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

This

*indexFileName = (char *)malloc( len *sizeof(char) + 1);

must be

*indexFileName = (char *)malloc( len *sizeof(char) + 5);

due to the extention adding with

strcat(*indexFileName, ".ind")

LPs
  • 16,045
  • 8
  • 30
  • 61
0

I suppose the problem is in "strcpy(*indexFileName, database);" instruction,

it should be strcpy(indexFileName, database);

Chirag Gangdev
  • 137
  • 2
  • 2
  • 6