I'm struggling to figure out what is wrong with my code. I'm getting a seg fault at the end of my parse function where I am freeing memory.

It fails this in check50:

:( Requesting non-existant file returns error code 404 \ expected output, not standard error of "======= Backtrace: =========\n======= M..."

:( Requesting request-target with " returns error code 400 \ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."

I don't understand why this is happening, can someone please look at my code to try and help?



This char* linecopy = malloc(sizeof(line)); is a memory leak. line is a pointer, which is 8 bytes. memory leaks cause unpredictable results.

Another allocation issue: char* method = malloc(sizeof(char)*3); is (almost) allocated for success (null terminator not accounted for). The check50 test "Method of 'GETabc' ", will use memory not allocated to method. There may be others.

The problem with this if (j == '\"') is probably a typo. j is the loop counter.

This char* findq = strchr(request_target, 'q'); will not give expected results if request-target is /hello.php?name=fizz.

It looks like request_target is not null terminated.

I suspect the linecopy mis-allocation is what is ultimately causing the seg fault because program is stomping on memory that doesn't belong to linecopy. There may be other problems in this parse. Once you fix the allocations continue with gdb and you will get a perfect score.

  • Thanks @DinoCoderSaurus I've fixed the lack of NULL terminations through my code and I've come up with this. pastebin link With GDB it looks like my code works as I intend it to, and valgrind doesn't say I have any leaks. I feel like I've allocated enough memory for the code but I get seg faults at: free(linecopy); and free(method); – clomas Jul 20 '16 at 8:29
  • malloc(sizeof(strlen(line))); strlen(line) is a size_t. sizeof(size_t) is 8. If you want linecopy to be the same length as line, then malloc(strlen(line) + 1) will do. (+1 for null-terminator). My rule of thumb: use sizeof for datatypes, never for variables. Also a problem here char* tempquery = malloc(sizeof(strlen(query) + 1)); sizeof notwithstanding, strlen(query) is "unknown" at this point. RE: valgrind. If you run valgrind, then ^C it will not report errors. If you run valgrind and send a request, then ^C you will have different result. – DinoCoderSaurus Jul 20 '16 at 13:43
  • Argh, I'm finding it hard to grasp the memory allocation but I finally got my code to pass check50 thanks so much @DinoCoderSaurus !! My code for check50 number 2 however doesn't pass anything so I've got a bit of work to do. – clomas Jul 21 '16 at 1:59
  • Thanks you @DinoCoderSaurus, I understand now. I have managed to get my code to pass the first check50, but the second one is throwing errors like I'm getting seg faults. I have no idea how, when I use GDB I can't replicate a set fault at all. I'm assuming that because I've passed check50 my parse and lookup are okay. Are you able to have a look at the rest of my other functions?? I'm pretty sure that query works too from what I can discern from GDB. – clomas Jul 21 '16 at 13:02
  • This is really a new question. You need to include the check50 results and maybe a link to your current code. Since you have conquered the original problem, consider writing an answer that would help other students with the same predicament. You would be able to accept it as the answer after waiting 2 days. The best way to learn is to teach! – DinoCoderSaurus Jul 21 '16 at 13:25

You must log in to answer this question.

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