0

I'm getting stuck in the last lines of PSET6. Apparently the problem is in the LOAD function. Check50 server2 getting all but 1 lines marked red :( and when running the program I get * Error in `./server': double free or corruption (!prev): 0x0000000001da49c0 * error when opening any file in the web server.

Thanks in advance!

Here is the code for LOAD:

bool load(FILE* file, BYTE** content, size_t* length)
{
    char* contents = malloc(sizeof(char) + 1);
    //contents = NULL;
    int i = 0;
    if(file == NULL)
    {
        return false;
    }

    while(!feof(file))
    {
        contents = realloc(contents, sizeof(char)*(i+1) + 1);
        if(fgetc(file) == EOF)
        {
            break;
        }
        fseek(file,-1,SEEK_CUR);
        contents[i] = fgetc(file);
        i++;
    }

    contents[i] = '\0';

    *content = contents;

    *length = i - 1;
    fclose(file);


    return true;
}

If everything seems to be fine here, it may be the PARSE function. Gonna edit question if necessary.

  • you don't appear to be calling free in this function, so I can't see how it would cause a double free error. saying that, this load function will always fail with php files, but that's not what you were asking. php files are sent via a pipe, so fseek won't work. :) – curiouskiwi Nov 1 '16 at 0:40
  • As @curiouskiwi pointed out, there is no free in this code snippet. You can either post your whole dictionary.c file here or use the segfault diagnoser tool: segfault.stensal.com/test_your_cs50_code, to find out where is the double free. – stensal Mar 16 '19 at 18:55
0

just found out what was going on. The error was in code

fclose(file)

That gave me the double free error. Somewhere later in the code, apparently that file was being memory-freed or closed, which gave me error as I was closing the file before freeing it.

Thanks anyway! :)

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .