1

I am getting some strange behavior associated with the load function that I can't seem to debug. When I visit the root directory, I am getting the error below, which appears to be occurring after I exit the load function:

*** Error in `./server': munmap_chunk(): invalid pointer: 0x00007ffdc8c48980 *** Aborted

If I visit a page inside of the root, such as /hello.html, I instead get a segmentation fault inside of the load function after I printf length to the console (I think the error is occurring inside the function because I put another printf right after the function call and it isn't being printed).

Here is my code:

bool load(FILE* file, BYTE** content, size_t* length)
{

    char* data = malloc(sizeof(char));
    if(data == NULL) 
    {
        return false; 
    }

    int i = 0;
    // read file
    for (int c = fgetc(file); c != EOF; c = fgetc(file))
    {
        data[i] = (char) c;    // stores in dynamically allocated memory
        i++;
        data = (char*) realloc(data, sizeof(char) * (i+1)); // increase buffer
    }

    content = &data; // stores the address of the first of those bytes
    *length = i;     // stores the number of bytes
    printf("Data: %s\n", *content);     // debugging - this works
    printf("Length: %zu\n", *length);   // debugging - this works
    return true;
}

Another idea I have is that there could be an issue with my parse function, but that appears to be working correctly based on my being able to correctly print the right values to the console.

8

That error message is coming from free(content); later in the code. See this post. Specifically (from answer 1, emphasis added):

This happens when the pointer passed to free() is not valid or has been modified somehow.

You have modified the pointer to content here: content = &data;, since &content is what is passed to the load function. Therefore, as per the instructions: "dereferencing content and storing the address of a BYTE at content." you might try *content = data; instead .

You must log in to answer this question.

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