0

When I add the below two blocks I get a segfault when I submit a request from telnet. Actually, I can add just the first and things run fine but upon the second (or even moving the first up above other code) gives the segfault. I assume it has to do with assigning something too large to an array but I can't find it.

// TODO: extract query from request-target
        // Extract request-target
        char requestTarget[] = "";
        int rtLength = strrchr(line, ' ') - strchr(line, ' ');
        strncpy(requestTarget, strchr(line, ' ') + sizeof(char), rtLength);
        requestTarget[rtLength] = '\0';
        printf("This is the request-target: %s\n", requestTarget);
        printf("This is the size of rquestTarget: %d\n", strlen(requestTarget));            

        // Extract query from line
        char query[] = "";
        int qLength = strrchr(line, ' ') - strchr(line, '?');
        strncpy(query, strchr(line, '?') + sizeof(char), qLength);
        query[qLength] = '\0';
        printf("This is the query: %s\n", query);
        printf("This is the size of query: %d\n", strlen(query));   
1
  • Looking at GDB, the segfault isn't triggered until reset() invoked.
    – Norcim133
    Dec 31, 2015 at 3:07

1 Answer 1

1

Remember that you can't change the length of a string array once it has been created in c. Now look at the following:

    char requestTarget[] = "";
    char query[] = "";

This code creates a couple of really short strings that you later try to copy a lot more data into. This is almost certainly causing the seg faults.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • Not only did that fix the problem, it was a really good reminder for me.
    – Norcim133
    Dec 31, 2015 at 3:23
  • Cliff B thank you so much for that comment "Remember that you can't change the length of a string array once it has been created in c" You solved a totally different but related problem for me.
    – Shauny
    Feb 15, 2016 at 13:17

You must log in to answer this question.

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