2

Valgrind output:

GET /cs3157/tng/index.html
==760== Invalid read of size 1
==760==    at 0x4C2E7D4: strstr (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==760==    by 0x400E67: HandleTCPClient (http-server.c:101)
==760==    by 0x400D42: main (http-server.c:75)
==760==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

Relevant code:

FILE *input = fdopen(clntSocket, "r");      //socket wrapped with FILE * 
if (input == NULL)
{
    perror("fdopen failed");
    fclose(input);
    return;
}
char request_buffer[100];
char out_buf[BUF_SIZE];
int len, res;

while( fgets(request_buffer, sizeof(request_buffer),input) != NULL)
{
    request_buffer[sizeof(request_buffer)-1] = '\0';   //null terminates buffer
    char *request = request_buffer;
    char *token_separators = "\t \r\n"; // tab, space, new line
    char *method = strtok(request, token_separators);
    char *requestURI = strtok(NULL, token_separators);
    char *httpVersion = strtok(NULL, token_separators);

    if( strstr(method,"GET") != NULL )
    {
            if( requestURI[strlen(requestURI)] == '/' ) 
                    requestURI = strcat(requestURI,"index.html");
            printf("%s %s\n", method, requestURI);

    } 
    memset(request_buffer,'\0',sizeof(request_buffer) );
}

fclose(input);      //close the socket by closing the FILE * wrapper

I read that this error is typically caused by failure to null terminate the String. I thought that my first line after fgets() would prevent that from being a problem. I'm guessing I'm overlooking something. I appreciate any help.

edit: Program ends crashes with a segmentation fault.

Nate Brennand
  • 1,488
  • 1
  • 12
  • 20

2 Answers2

2

It sounds (from Valgrind's report) as if method is NULL. You should step through this code with a debugger to verify that the tokenizing works as intended.

Also, you should declare all those pointers as const char * since they're not intended to be written to. This is of course a minor point, but I try to encourage use of const whenever possible. :)

unwind
  • 391,730
  • 64
  • 469
  • 606
  • This was the case. I added if( method!=NULL && requestURI!=NULL && httpVersion!=NULL ) before the strstr() call and was able to avoid the error. Thanks! – Nate Brennand Nov 28 '12 at 09:35
  • @Twigger Okay, good. I honestly think that this is the answer you should accept, then, since I answered (correctly) before peterph. :) – unwind Nov 28 '12 at 09:36
  • Fair enough, from my perspective I'm working on my school's server and don't have a way to step through the code with a debugger without a huge inconvenience. peterph's answer was more easier to apply and helped me make the connection first. – Nate Brennand Nov 28 '12 at 09:41
1
Address 0x0 is not stack'd, malloc'd or (recently) free'd

Something is wrong here: the program seems to be dereferencing NULL. I'm wondering whether it does segfault in the end - you should check return values of strtok(), as it returns NULL in case no more tokens are found.

peterph
  • 980
  • 6
  • 11
  • After adding if( method!=NULL && requestURI!=NULL && httpVersion!=NULL ) before the strstr() call I was able to avoid the error. Thanks! – Nate Brennand Nov 28 '12 at 09:34