This free here isn't valid (since there clearly isn't a buffer), to do realloc properly there needs to be two of them like:

buffer2 = realloc(buffer)
if(!buffer2){ free(buffer); handle out of memory error; }