My parse code seems to work fine but after it runs there is a segmentation fault. It isn't any of the other methods, and I just can't figure out why it is doing this. I think it has something to do with query and abs_path though.

Here is my parse method:

char requestLine[strlen(line) + 1];
requestLine[LimitRequestLine] = '\0';

char* methodCopy = strtok(requestLine," ");
char* requestCopy = strtok(NULL," ");
char* httpCopy = strtok(NULL,"\r\n");

char method[strlen(methodCopy)+1];
char request[strlen(requestCopy)+1];
char http[strlen(httpCopy)+1];

method[strlen(method)] = '\0';
request[strlen(request)] = '\0';
http[strlen(http)] = '\0';

if (strcmp(method,"GET") != 0) {
if (strncmp(request,"/",1) != 0) {
if (strchr(request,'"') != NULL) {
if (strcmp(http,"HTTP/1.1") != 0) {

char* absCopy = strtok(request, "?");
char* queryCopy = strtok(NULL,"\0");

if (queryCopy != NULL) {

    abs_path[strlen(abs_path)] = '\0';
    query[strlen(query)] = '\0';
} else {

    abs_path[strlen(abs_path)] = '\0';
    query[0] = '\0';

if (strchr(abs_path,'?') != NULL) {
if (strchr(query,'"') != NULL) {

return true;

EDIT: After using debug50, it seems the problem might be in my indexes method on line 4 strcpy(php, path);. That is where debug50 stops after I get the segmentation fault.

char* php = "";
char* html = "";

strcpy(php, path);
strcat(php, "/index.php");

strcpy(html, path);
strcat(html, "/index.html");

if (access(php,R_OK) != -1) {
    return php;
} else if (access(html,R_OK) != -1) {
    return html;
return NULL;
  • The best tool to use when you "can't figure out why it is doing this" is a debugger. Use debug50 or gdb (see ADDENDUM) to help you narrow it down. Either should help you solve the problem or give more information so you can ask a more specific question. – DinoCoderSaurus Dec 20 '16 at 20:56

When you declare a string literal, as with char* php = "";,it is stored in read-only memory. So it is indeed seg-faulting here strcpy(php, path); because it cannot change read-only memory.

Even if that was not the case, php (and html) are not allocated for any memory. So the strcpy/strcat are stomping on someone else's memory (which could be a seg-fault waiting to happen). They need to be allocated. (Think malloc or calloc).

Note this "hint" from the spec:

this function should dynamically allocate memory on the heap for the returned string.

| improve this answer | |
  • This helped me find the problem and fix it. It led me to find and fix a chain of problems, one of which I am stuck on. I will post a new question for that because it is too off topic for this thread. Thanks for all your help! – Kyza Dec 22 '16 at 16:18

You must log in to answer this question.

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