So I've been fumbling over this for a little while now. When I copy the first i bytes of line to method, the server responds OK when the request_target is /. You can see I have a printf statement right after I copy to method, which confirms that method is indeed GET when request_target is /.

However, when the request_target is /favicon.ico, method prints with some unexpected values that fill the rest of the array after GET: GET22%24last_name%22%3L%20%22LLL��. I can't quite figure out where these values are coming from and how to stop the issue. If I add method[3] = '\0'; right where my printf statement is, then I get a seg fault.

Can anyone give me some hints or explain why this is happening? If you need to see more code to diagnose the problem, I'll be happy to add more of this function.

// get substrings ready
char method[25];
char version[25];

// get pointers ready
const char* version_ptr = NULL;
const char* request_target_ptr = NULL;
const char* qptr = NULL;

// iterate through line to first space
int i;
for (i = 0; line[i] != ' '; i++);

// request target begins after this space
request_target_ptr = &line[i + 1];

// copy everything up to this point to method substring, exclude space
strncpy(method, line, i);
printf("%s\n", method);

// ensure that method is get
if (strcasecmp(method, "get") != 0)
    return false;

// ...
  • Looking through my code now and filling in the arrays within the for loops rather than using strcpy. I'm sure I'll find the seg fault this way. – LeviJames Apr 9 '16 at 20:39

Found the seg fault and it was not in the parse function. To anyone reading this having similar difficulties, I would suggest going through your code in the recommended order, line by line, and ensure you know exactly what is going on with string termination. That might sound obvious but it's an easy place to mess up with this problem set.

You must log in to answer this question.

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