So I've been working on server.c and I have the the lookup, parse, and indexes function finished. With index passing back "index.php", "index.html", or NULL.

It runs through that but when it gets to the htmlspecialchars function (implemented by the staff) the program gets to the line marked here and returns 1.

char* htmlspecialchars(const char* s)
    // ensure s is not NULL
    if (s == NULL)
        return NULL;

    // allocate enough space for an unescaped copy of s
    char* t = malloc(strlen(s) + 1);   // crashes and returns 1
    if (t == NULL)
        return NULL;
    t[0] = '\0';



Not sure what's up and why it has problems processing the information. At the point it crashes my local variables are as follows:

s = 0x6175a3 ".."
t = 0x4013f0 <_start> "1\355I\211\321^H\211\342H\203\344\360PTI\307\300 <@"

There are no other local variables in use, s is a const char* and t is a char*.

In my indexes function I did use strdup(path) to copy the value from the value passed into the indexes function along with strcat() to create the proper strings for the return value.

Any idea why this might be happening or how I can resolve it?

  • Digging a little deeper, it seems as though the code isn't breaking at the s and t variables, it's breaking when it performs the scandir() function and gets a return value of 8. Not sure how it's coming up with that but ends up being a segfault when it frees the directory. – Bob R. Sep 14 '16 at 2:46

Suggest you look into the access function (man access). scandir implies to me that you are reading through the whole directory to look for either of the two specified files. One problem with that approach tends to be that it scans alphabetically, so index.html is found before index.php and that makes it difficult to write the program to spec. Besides that, it makes the program (and the programmer!) do much more work. KISS: Keep It Sweet and Simple.

  • Well I'm using access to test the files already. – Bob R. Sep 14 '16 at 14:02
  • Then I do not understand your reference to scandir in a prior comment. – DinoCoderSaurus Sep 14 '16 at 14:13
  • Ok so scandir was pre-generated code in server.c. The problem with scandir is that returns a value greater than the number of files inside of the directory. Here is my indexes function, perhaps it's what is causing the hiccup. pastebin.com/nXN5CMCi – Bob R. Sep 14 '16 at 15:10
  • Your comment 100% clear the first few times I read it, but essentially the flow of the server.c application is that it parses the request line ensuring it is a GET request, contains a valid request-target, and ensures it is using HTTP/1.1. It then uses the indexes function to see if an index.php or an index.html file exists in the directory, if not it then performs a list function to list the files in the directory. The list functions purpose is to build an html formatted list of items on the page from a namelist struct, to be continued... – Bob R. Sep 14 '16 at 15:48
  • Now that's a 1000 words I understand :) phpPath and htmlPath have no memory allocated. From man strdup Memory for the new string is obtained with malloc(3), and can be freed with free(3). This path = phpPath; is unnecessary Probably not a problem. It's changing what the local path points to, but back in main it will be pointing to the original address. So I'll stick with unnecessary. If you are using gdb or printfs, make sure you inspect the value of index when you get back to main. Because of the allocation problem, it might not be what you expect. – DinoCoderSaurus Sep 14 '16 at 15:50

You must log in to answer this question.

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