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.