Just after completing Parse i've stucked on load function. By cs50 check i definately understand that something is wrong in reading memory to a buffer and maybe even accsessing it. For my eyes this code looks ok, but it definately wrong so i am asking for help to find those bad places and explanation to what is wrong and with what tools could it be fixed. Hope it will be working soon with your help!
Here is link to check50: https://sandbox.cs50.net/checks/b81df3c25c9c403b906e1ec0ce2fce1a
So, here is my sweet load function:
bool load(FILE* file, BYTE** content, size_t* length)
{
if (file == NULL)
{
return false;
}
*content = NULL;
*length = 0;
BYTE* buffer = malloc(sizeof(BYTE));
if (buffer == NULL)
{
free(buffer);
return false;
}
int len = 0;
while(fread(buffer, 512, 1, file) == 1)
{
*content = realloc(buffer, (len + 1) + sizeof(BYTE));
len++;
}
*length = len;
return true;
}
I will also paste index function, as maybe checker is yelling because of it:
char* indexes(const char* path)
{
char* html = "/index.html";
char* php = "/index.php";
char *alt_html_path = malloc(strlen(path) +strlen(html) +1);
char *alt_php_path = malloc(strlen(path) +strlen(php) +1);
strcpy (alt_html_path, path);
strcat (alt_html_path, html);
strcpy (alt_php_path, path);
strcat (alt_php_path, html);
//if (index.html) return /path/to/a/directory/index.html
if ( access(alt_html_path, F_OK) != -1 )
{
free(alt_php_path);
return alt_html_path;
}
//if (index.php) return /path/to/a/directory/index.php
else if ( access(alt_php_path, F_OK) != -1)
{
free(alt_html_path);
return alt_php_path;
}
else
{
free(alt_php_path);
free(alt_html_path);
return NULL;
}
}