I've been slowly working through this problem set for a couple weeks now, and I think I've hit a brick wall. Giving just a general overview, I believe at this point that I have successfully completed: validating request-line, extract query from request-target, concatenate root and absolute-path, ensure path exists, ensure path is readable, and extract path's extension. Now I have to complete "respond to client", and there doesn't seem to be much direction given on how to tackle this one. The only reference I have is the "dynamic content" part of the code in server.c. I actually used that part of the code for the "respond to client" for static content to see if it worked (while adding one statement for "type"), and it didn't. I am getting a seg fault as well when I submit a get request for static content, which leads me to believe that it is the "respond to client" problem I have to resolve, and not the previous code that I have completed (hopefully).

In addition, when I ran valgrind on my program I got 2 leaks which I don't even think are my fault. The leaks are the following:

==13895==     in use at exit: 4,123 bytes in 2 blocks
==13895==   total heap usage: 4 allocs, 2 frees, 4,177 bytes allocated
==13895== Searching for pointers to 2 not-freed blocks
==13895== Checked 61,900 bytes
==13895== 27 bytes in 1 blocks are still reachable in loss record 1 of 2
==13895==    at 0x402C324: realloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==13895==    by 0x804A671: parse (server.c:723)
==13895==    by 0x8048F99: main (server.c:122)
==13895== 4,096 bytes in 1 blocks are still reachable in loss record 2 of 2
==13895==    at 0x402A17C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==13895==    by 0x40CC73F: realpath@@GLIBC_2.3 (canonicalize.c:78)
==13895==    by 0x8049F96: start (server.c:783)
==13895==    by 0x8048F4D: main (server.c:107)

Also valgrind tells me that I have 1 error. It's giving me an "invalid write of size 1" error, and it is related to the strcpy function when it is being used to copy from "extension" to "new_extension". I checked my implementation of "extension" to see if it was the proper size, and it is. It is 4 for html, 3 for php etc. The error is the following:

ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==13895== 1 errors in context 1 of 1:
==13895== Invalid write of size 1
==13895==    at 0x402D4EE: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==13895==    by 0x804AD34: lookup (server.c:634)
==13895==    by 0x8049CD0: main (server.c:390)
==13895==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==13895== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Lastly, to wrap it all up, when I test my program out on telnet, it seems to work for any dynamic(php) get request, because I get an OK back and it doesn't seg fault. However, when I test my program out on telnet by submitting a get request for static(html) content it seg faults. This leads me to believe that it's because I am not done correctly finishing "respond to client", although it may be because of something else I did. When I tried testing my program out with google chrome, it seg faults on both static and dynamic get requests. It seems to seg fault on dynamic requests, because it seg faults right after it tries to get "favicon".

Any thoughts from anyone on all of this? I will be more than happy to PM my code if someone is willing to help me out, maybe I am doing something completely wrong that someone can point out. This problem set is driving me nuts, and I just want to finally finish it up!

  • I had some trouble in this one. I ended up using memset for all strings that I defined because otherwise I got garbage in my temporary strings. e.g. memset(pathonly, '\0', sizeof(pathonly));
    – gitterb
    Feb 17, 2015 at 1:20
  • and if you want to send me your code, I can look at it.
    – gitterb
    Feb 17, 2015 at 1:21
  • The segmentation fault is likely to be caused by line 634 in your server.c. And I'm afraid your code allocates memory and never frees it. If you're still stuck, you email me with your code!
    – kzidane
    Feb 17, 2015 at 8:29
  • @gitterb What would be the best way to send it to you (didn't know stack exchange didn't have private messaging). Feb 17, 2015 at 16:27
  • @Kareem, I emailed you the code. Feb 17, 2015 at 16:27

1 Answer 1


As I told you, the segmentation fault is obviously called by line 637 in your code since you're trying to copy the contents of extension into new_extension which is actually NULL (i.e., doesn't have any memory allocated for it) and writing to NULL causing a segmentation fault.

After fixing that error on my side, valgrind stopped complaining about everything else.

  • I am away from my card right now so I can not fix it, but thank you. Feb 18, 2015 at 6:08
  • Do you have any insight on how to complete the "respond to client" part of the problem set for the static content? Any other parts of my code that you think are incorrect? Feb 18, 2015 at 6:14
  • @electronicaneer I would suggest looking at the "responding to client with dynamic content" part and reading the man pages of the different functions as necessary. Also it might be a good idea to use a software like gdb to debug your code! ;)
    – kzidane
    Feb 18, 2015 at 10:06
  • fixing that error in "new_extension" took care of most of my issues, and I was able to finish off the problem set from there. Thank you. Feb 18, 2015 at 19:25

You must log in to answer this question.

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