0

I have been trying to use GDB to find errors in my code. I went through the steps @MARS gave me as an answer to one of my questions. However, when I tried to list the values of my variables by using curl i..., I received an infinite loop.

Why am I receiving an infinite loop? How can I use curl i... with my code?

Thanks in advance.

Here are my check50 errors: http://pastebin.com/sac9tkkp

Here is my code:

bool parse(const char* line, char* abs_path, char* query)
{
    const char* copy_line = line;
    char* method_str = "";
    int method_length = strlen(method_str);
    strtok(method_str, copy_line);
    char* request_str = "";
    int request_length = strlen(request_str);
    strtok(request_str, copy_line);
    char* http_ver_str = "";
    int http_ver_str_length = strlen(http_ver_str);
    strtok(http_ver_str, copy_line);
    char* path;
    char* query_str;
    int query_length;
    if (request_str != NULL)
    {
       path = "";
       strtok(path, copy_line);
       query_str = "";
       query_length = strlen(query);
       strtok(query_str, copy_line);
    }
    for (int i = 0; i < method_length; i++)
    {
        char space[2] = " ";
        const char* letter = &method_str[i];
        if (strcmp(letter, space) == 0)
        {
            printf("this is method_str[i]: %c", method_str[i]);
            error(400);
            return false;
        }
    }
    char quote = '"';
    for (int c = 0; c < request_length; c++)
    {
        char space[2] = " ";
        const char* letter2 = &request_str[c];
        if (strcmp(letter2, space) == 0)
        {
            printf("this is request_str[c]: %c", request_str[c]);
            error(400);
            return false;
        }
        if (request_str[c] == quote)
        {
            printf("this is request_str[c]: %c", request_str[c]);
            error(400);
            return false;
        }
        if (c == 0)
        {
            if (request_str[c] != '/')
            {
                error(501);
                return false;
            }
        }
    }
    for (int j = 0; j < http_ver_str_length; j++)
    {
        if (http_ver_str[j] == quote)
        {
            printf("This is http_ver_str[j]: %c", http_ver_str[j]);
            error(400);
            return false;
        }
    }
    if (path[0] != '/')
    {
        printf("This is path[0]: %c", path[0]);
        error(400);
        return false;
    }
    for (int k = 0; k < query_length; k++)
    {
        if (query_str[k] == quote)
        {
            printf("This is query_str[k]: %c", query_str[k]);
            error(400);
            return false;
        }
    }
    if (strcmp(method_str, "GET") != 0)
    {
        error(405);
        return false;
    }
    //if (strcmp(request_str[0], '/') != 0)
    //{
      //  error(501);
        //return false;
    //}
    if (strcmp(http_ver_str, "HTTP/1.1") != 0)
    {
        error(505);
        return false;
    }
    abs_path = path;
    query = query_str;
    return true;
}
1
  • You can publish your parse code, or that function that you think is causing you problems, surely we will be happy to help
    – MARS
    Dec 15 '16 at 20:34
1

We have a serious problem at the very beginning.

char* method_str = "";

I guess you want to write a blank, so write it for real. The lack of white space can cause segfault, or a strange behavior of the program. For example after using GDB (it can even be used in your program) we have the following result:

(gdb) p request_str
$2 = 0x438f46 ""
(gdb) p request_length
$3 = 32767

When it is supposed to be 1. How to use GDB in your case? Once the program is locked, the terminal in which we use curl -i, simply type Ctrl + c, The terminal on which we are using GDB can continue to debug the program. Finally if we use strtok several times in the same string, after the first time the first argument must be NULL, see man strtok. I hope this helps to get started

0

You must log in to answer this question.

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