0

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***
1

Looks like a small memory leak in loookup. Maybe you're declaring a variable to store extension, but it's not allocated to include a null-terminator?

The indexes function looks fine. I think index is the victim of a memory leak, not the cause. Based on the valgrind results, it looks like a memory leak in load. If load is putting bytes into memory it doesn't own, and that memory happens to belong to index, well, that could seg-fault. And those big ugly valgrind errors pointing to transfer (which calls load) and load can't be ignored.

If you get a segmentation error, valgrind should too. You have to send the same sequence of requests to server running under valgrind to provoke it.

But wait there's more! Review the specification of indexes. Order matters.

3
  • Thanks a lot! I will check load() and lookup() again and look for memory errors. The weird thing is that i run valgrind with the same requests and under the same circumstances. What i understood from other fora is that valgrind creates a little bit different environment and that's why you can get away with some small bugs that normally would crash your program. But that the errors in valgrind probably point to these bugs.
    – Jorn
    Sep 9 '16 at 7:51
  • I can't find anything in load(). I have shared it too within the question now. Really don't know where to look..Lookup also looks and functions normal. The segmentation error only occurs when the indexes function is used. When opening a .jpg or a .php directly everything is in order..
    – Jorn
    Sep 9 '16 at 8:12
  • I found the mistake. I've probably by accident changed something in the main code that I didn't know about. I downloaded server.c again and implemented the 4 functions and now it works perfectly! Thanks a lot for the help.
    – Jorn
    Sep 9 '16 at 8:23

You must log in to answer this question.

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