0

i been stuck with these 2 errors for days which i don't really know what causes them..tried debugging parse and as far as i can tell abs_path is working as it should(although i'm not sure it's written in the right way!)..here is my code for parse and lookup..thx in advance https://gist.github.com/cb99b7dffebbbd54b879ec7deaea28f1enter image description here

0

1 Answer 1

0

Scope. Pointers. While debugging, if you are inspecting abs_path while in parse it may very well be right. Remember that main will use abs_path, so you need to inspect it there too, when you return from parse. That will tell a very different story. One problem is here

abs_path = realloc(abs_path, sizeof(LimitRequestLine + y));
abs_path = absl;

abs_path is allocated around line 183 in server thusly char abs_path[LimitRequestLine + 1];. There it gets an address. When you realloc the local abs_path in parse, it will get a new address. (and then abs_path = abs1 will give it still yet another new address). Alas, when you return to main, it will be looking at the original address. Think strcpy. (And query too!)

Fixing this problem may provoke other problems. And this may not be the only problem. Keep at the debugging and you will have success.

Addendum

On more careful review, I see that the realloc is conditional. There's a serious problem with the condition, to wit abs_path = realloc(abs_path, sizeof(LimitRequestLine + y)). LimitRequestLine + y will be an int, sizeof(int) is 8. It's a seg fault waiting to happen. You should not be testing that condition. Since line is limited to LimitRequestLine bytes, and abs_path is a subset of line, it can never be bigger. If control ever reaches the realloc, program will seg-fault. (which perhaps it did in your first iteration, since check50 gave \ expected output, not an exit code of 0). Which also means you either got seg faults when you submitted a request from browser or curl OR you never sent a request to server. check50 step should come after you have a reasonable expectation that your program will pass. If you are getting seg faults, you should expect check50 to fail, so it is not ready to submit. If you have never even run server (and sent a request like cat.jpg or hello.html), it is definitely not ready to submit.

3
  • i actually didn't have any segfault..of course i wouldn't have used check50 if it gives me segfault..i debugged and ran it without any..also i was getting these 2 errors even before i wrote realloc..in the specs it says "You may assume that the memory to which abs_path points will be at least of length LimitRequestLine + 1" and i don't really understand this line which why i used realloc in case of the memory is more than LimitRequestLine + 1(or thats what i meant to do)..now i removed all reallocs and set abs_path = absl outside any function in parse and still same errors Commented Aug 24, 2016 at 13:41
  • If I understand correctly you are still doing abs_path = absl somewhere. You cannot "set" abs_path to something, it will get a new address. You need to fill it with some string function. something like strcpy(abs_path,absl) Commented Aug 25, 2016 at 2:57
  • thank you..i can't believe i wasted all these days over that silly mistake..it works now Commented Aug 25, 2016 at 13:46

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