For pset6, implementing a server, almost everything works except the indexing function. I get a segmentation error after the use of this function when trying to free(index). This is the returned variable from the function (in this case path../test/index.html)
This is the function:
char* indexes(const char* path)
{
//create string for default file extensions
const char* html = "index.html";
const char* php = "index.php";
int pathsize = strlen(path)+1;
//malloc space for default filenames
char* htmlindex= malloc(pathsize+11);
char* phpindex= malloc(pathsize+10);
//concatenate path and default file extension
memcpy(htmlindex, path, pathsize);
strcat(htmlindex, html);
memcpy(phpindex, path, pathsize);
strcat(phpindex, php);
//check for existence of default files
if (access(htmlindex, F_OK) == 0)
{
printf("FOK htmlindex acces\n");
free(phpindex);
return htmlindex;
}
if (access(phpindex, F_OK) == 0)
{
printf("FOK phpindex acces\n");
free(htmlindex);
return phpindex;
}
else
{
free(htmlindex);
free(phpindex);
return NULL;
}
}
This is a part of main where free(index) comes in.
// use path/index.php or path/index.html, if present, instead of directory's path
char* index = indexes(path);
if (index != NULL)
{
free(path);
path = index;
free(index);
//problem free(index)!!!!!!
}
// list contents of directory
else
{
list(path);
continue;
}
}
This is load()
/**
* Loads a file into memory dynamically allocated on heap.
* Stores address thereof in *content and length thereof in *length.
*/
bool load(FILE* file, BYTE** content, size_t* length)
{
//declaring memory to copy the content to, to later realloc
char* data = malloc(sizeof(char));
if (!data)
return false;
int indexer = 0;
int tempsize = 1;
for (int c = fgetc(file); c != EOF; c = fgetc(file))
{
data =realloc(data, sizeof(char)*(tempsize+1));
tempsize++;
data[indexer] = c;
indexer++;
}
data[indexer]='\0';
*content = data;
*length = tempsize;
return true;
}
When I run with valgrind the segmentation error doesn't occur and everything works in order. Valgrind does give some errors almost all seeming to point towards free(index) (server.c:239):
==5486== Invalid read of size 1
==5486== at 0x4C2DA44: rindex (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5486== by 0x40310E: lookup (server.c:681)
==5486== by 0x401C12: main (server.c:252)
==5486== Address 0x55059c0 is 0 bytes inside a block of size 53 free'd
==5486== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5486== by 0x401BE1: main (server.c:239)
==5486==
==5486== Invalid read of size 1
==5486== at 0x4C2EAA4: strcasecmp (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5486== by 0x403125: lookup (server.c:685)
==5486== by 0x401C12: main (server.c:252)
==5486== Address 0x55059ee is 46 bytes inside a block of size 53 free'd
==5486== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5486== by 0x401BE1: main (server.c:239)
==5486==
==5486== Invalid read of size 1
==5486== at 0x4C2EAA4: strcasecmp (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5486== by 0x403156: lookup (server.c:689)
==5486== by 0x401C12: main (server.c:252)
==5486== Address 0x55059ee is 46 bytes inside a block of size 53 free'd
==5486== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5486== by 0x401BE1: main (server.c:239)
==5486==
==5486== Syscall param access(pathname) points to unaddressable byte(s)
==5486== at 0x52288C7: access (syscall-template.S:81)
==5486== by 0x40351F: transfer (server.c:1103)
==5486== by 0x401C8F: main (server.c:268)
==5486== Address 0x55059c0 is 0 bytes inside a block of size 53 free'd
==5486== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5486== by 0x401BE1: main (server.c:239)
==5486==
==5486== Syscall param open(filename) points to unaddressable byte(s)
==5486== at 0x5228620: __open_nocancel (syscall-template.S:81)
==5486== by 0x51B6FC7: _IO_file_fopen@@GLIBC_2.2.5 (fileops.c:228)
==5486== by 0x51AB4A3: __fopen_internal (iofopen.c:90)
==5486== by 0x40354C: transfer (server.c:1110)
==5486== by 0x401C8F: main (server.c:268)
==5486== Address 0x55059c0 is 0 bytes inside a block of size 53 free'd
==5486== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5486== by 0x401BE1: main (server.c:239)
==5486==
==5486== Syscall param write(buf) points to uninitialised byte(s)
==5486== at 0x5228870: __write_nocancel (syscall-template.S:81)
==5486== by 0x403942: respond (server.c:982)
==5486== by 0x403638: transfer (server.c:1140)
==5486== by 0x401C8F: main (server.c:268)
==5486== Address 0x55139ff is 271 bytes inside a block of size 272 alloc'd
==5486== at 0x4C2CE8E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5486== by 0x403E1F: load (server.c:661)
==5486== by 0x40357E: transfer (server.c:1121)
==5486== by 0x401C8F: main (server.c:268)***strong text***