0

I can't figure out this problem, I hope someone can help me.

My server works perfectly only for the first HTTP request, but when I try to request something else, it throws me an error. This is a copy of my command line:

Using /home/ubuntu/workspace/pset6/public for server's root
Listening on port 8080
GET / HTTP/1.1
HTTP/1.1 200 OK
GET /cat.html HTTP/1.1
*** Error in `./server': malloc(): memory corruption: 0x0000000001dd3970 ***
Aborted

I traced the error to the parse function. In the parse function, I used malloc() in one of my variables. I thought the error was because I didn't free the memory, so I freed the memory on the last lines of the parse function and the result was almost the same:

Using /home/ubuntu/workspace/pset6/public for server's root
Listening on port 8080
GET / HTTP/1.1
HTTP/1.1 200 OK
GET /cat.html HTTP/1.1
*** Error in `./server': free(): invalid next size (normal): 0x0000000002101300 ***
Aborted

This is my complete parse function:

bool parse(const char* line, char* abs_path, char* query)
{
    //variables to store the method, the request-target, the HTTP version and a copy of line.
    char* method;
    char* req;
    char* version;
    char* linecpy = malloc(strlen(line));
    //if (linecpy == NULL)
    //  return false;
    strcpy(linecpy, line);

    method = strtok(linecpy, " ");
    req = strtok(NULL, " ");
    version = strtok(NULL, " ");

    //check that line is a valid request
    if (method == NULL || req == NULL || version == NULL)
    {
        if (strchr(method, ' ') != NULL
            || strchr(req, ' ') != NULL
            || strchr(version, ' ') != NULL) 
        {
            error(400);
            return false;
        }

    }

    if (strcmp(method, "GET"))
    {
        error(405);
        return false;
    }

    if (req[0] != '/')
    {
        error(501);
        return false;
    }

    if (strchr(req, '\"') != NULL)
    {
        error(400);
        return false;
    }

    if (strcmp(version, "HTTP/1.1\r\n"))
    {
        error(505);
        return false;
    }

    //if '?' is found, separate request-target into abs_path and query
    if (strstr(req, "?q=") != NULL)
    {
        abs_path = strtok(req, "?");
        query = strtok(NULL, " ");
    }

    strcpy(abs_path, req);
    strcpy(query, "");
    free(linecpy);
    return true;
}
1

This is what line looks like in memory, together with the space you're allocating for linecopy:

enter image description here

This could cause the erratic behavior you're seeing.

Other than that, you're extracting query and abs_pathwrong whenever a ? is present in line, but that would be material for another question.

If this is enough to answer your question, please click the check mark to accept.

| improve this answer | |
  • I have corrected the size for linecpy to linecpy = malloc(strlen(line) + 1) to take into acc the \0, and I corrected a few other malloc calls on my code with that same mistake. Now, I have the same problem: malloc() memory corruption. Using the IDE debugger, the error occurs in the urldecode function, specifically in the line: char* t = calloc(strlen(s) + 1, 1); but that's not one of my functions. Any idea what could be the problem? – Carlos C Nov 28 '16 at 23:11
  • Which was the GET request line that caused that error? – Yuri Laguardia Nov 28 '16 at 23:15
  • GET /cat.html HTTP/1.1 – Carlos C Nov 28 '16 at 23:17
  • You must have changed something other than that linecpy malloc line, because the problem was solved here just by using linecpy = malloc(strlen(line) + 1). Make sure you have saved and recompiled your code! Your code will definitely generate a problem whenever a ? is present in the request line, but should work otherwise for GET /cat.html HTTP/1.1. – Yuri Laguardia Nov 28 '16 at 23:21
  • I'm sorry, I wrote malloc(strlen(line + 1)) instead of malloc(strlen(line) + 1). Now everything works fine :). Thanks for your patience and for helping me! – Carlos C Nov 28 '16 at 23:26

You must log in to answer this question.

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