0

As mentioned in the title, the parameter abs_path retains its memory from the previous allocation, resulting in the outputs below. My buffer is reset to NULL on every call to parse() as char buffer[64] = {'\0'};, as you can see in the buffer = line where it contains the expected result.

GET /cat.php HTTP/1.1
buffer = /cat.php
abs_path = /cat.php    


GET / HTTP/1.1
buffer = /
abs_path = /cat.php

// Or alternatively

GET /cat.html HTTP/1.1
buffer = /cat.html
abs_path = /cat.html



GET / HTTP/1.1
buffer = /cat.php
abs_path = /cat.phpl

1 Answer 1

1

Your second example makes it plain what is happening:

GET /cat.html HTTP/1.1
buffer = /cat.html
path = /cat.html


GET / HTTP/1.1
buffer = /cat.php
path = /cat.phpl

You are writing over the contents of the previous buffer without erasing them first. (Hence, the leftover "l" at the end of "/cat.phpl")

This is not supposed to happen.

It's hard to say exactly how to fix it without seeing any of your C source code, but two things spring to mind:

  • Are you remembering to free() everything that you malloc()?
  • When you allocate dynamic memory for your abs_path and buffer, have you tried using calloc() instead of malloc(), to zero out any garbage values remaining in the buffer?

EDIT: Well, there's your problem. You should use malloc() (or better yet, calloc()) and free() for your buffer.

You should not mess with abs_path, which as you say is out in main() -- just declare a buffer in the function scope for parse(), then allocate memory for it, build whatever string you need to build, copy that string to abs_path, and free the buffer memory. (In my solution, I used separate buffers for abs_path and query and made sure to free them as soon as they were not needed.)

Sorry I can't reply in comments (because mobile.)

2
  • I am not using malloc for the buffer, sorry for the miscommunication there. I could probably reset abs_path to NULL, but it is stored in main which I assume I'm not allowed to touch. Other than that I don't see any way I could fix this. Commented Sep 20, 2016 at 15:18
  • It doesn't seem like using calloc() did anything. I've begun rewriting everything to using to separate buffers, though. Commented Sep 20, 2016 at 16:50

You must log in to answer this question.

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