0

I am trying to break the request line into separate tokens. gdb shows me segmentation fault on this line

strncpy(method, line, SP1 - haystack);

Interestingly, in gdb, I can not print line, but printf("%s", line); prints line without any issues.

error in GDB

Can someone help me with this?

#include <stdio.h>
#include <string.h>
#include <strings.h>        

int main(void)
{        
    const char* request = "GET /cat.html HTTP/1.1\r\n";           

    const char* haystack = request;
    char* needle = strstr(haystack, "\r\n");            

    char line[needle - haystack + 2 + 1];
    strncpy(line, haystack, needle - haystack + 2);
    line[needle - haystack + 2] = '\0';            

    // log request-line
    printf("%s", line);

    // TODO: validate request-line

    // SP according to RFC means "single space"
    char* SP1 = strchr(line, ' '); // store address of first space from request line
    char* SP2 = strrchr(line, ' '); // store second/last space

    char method[SP1 - haystack + 1]; // address of space1 minus address of haystack-start (+1 gives space of Null Terminator)
    strncpy(method, line, SP1 - haystack);
    method[SP1 - haystack] = '\0';

    char requestTarget[SP2 - (SP1 + 1) + 1];
    strncpy(requestTarget, line, SP2 - (SP1 + 1));
    requestTarget[SP2 - (SP1 + 1)] = '\0';

    char HTTPversion[needle -(SP2 + 1) + 1];
    strncpy(HTTPversion, line, needle -(SP2 + 1));
    HTTPversion[needle - (SP2 + 1)] = '\0';

    printf("%s %s %s\r\n", method, requestTarget, HTTPversion);    
}
1
  • I received a downvote. Would be good to know why, so I can improve my question or know what to do differently next time!
    – Lex
    Aug 18 '15 at 16:58
0

the reason why you can't print line in gdb could be that

Another possible effect of compiler optimizations is to optimize unused variables out of existence, or assign variables to registers (as opposed to memory addresses). Depending on the support for such cases offered by the debug info format used by the compiler, gdb might not be able to display values for such local variables. If that happens, gdb will print a message like this:

 No symbol "foo" in current context.

see https://sourceware.org/gdb/onlinedocs/gdb/Variables.html!

the reason for the segmentation fault is that you're determining the size of method using SP1, which is in terms of line, and haystack, which stores the address of totally different location in memory.

this causes the size of method to be too large which causes a segmentation fault.

4
  • It seems that I haven’t understood something. strchr() is returning a pointer of the first occurrence of the char I am looking for. Say, haystack has address 100, why then SP=strchr(line, ‘ ‘); doesn’t return 103 as address (assuming GET as the first 3 chars in line)? I’ve printed out the values in gdb and as you said it shows totally different locations than needle and haystack. What rule am I overlooking and how can I solve this?
    – Lex
    Aug 18 '15 at 20:48
  • because haystack and line are not the same. they do not point to the same memory location, unlike for example, haystack and request which do point to the same memory location due to the assignment haystack = request.
    – kzidane
    Aug 19 '15 at 5:42
  • hope you could be so patient and look again at my code :-) isolated, everything works great, but in server.c the code fails
    – Lex
    Aug 21 '15 at 9:11
  • hi, @Lex! if you have a different question, probably you should create a new post not edit the previous one. I rollback'd the original question. your most recent edit in the revisions!
    – kzidane
    Aug 21 '15 at 15:56

You must log in to answer this question.

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