0

I get a double free or corruption error message on my server.c, although I am not freeing any thing in my functions. I also tested my functions and they are all working properly supposedly. Can any one Check my code and Tell me if something is wrong ?

parse function

bool parse(const char* line, char* abs_path, char* query)
{
    // my function set a pointer to the beginning of the 
    // request target the counts the characters of the full 
    // request target and the characters of the absolute path only
    // than copies the required strings according to the counted
    // characters
    if (n_que != 0)
    {
        strncpy(abs_path, req_tar, n_que - 2);
        strncpy(query, q_cur, (n_abs - n_que) + 1);
    }
    else
    {
        strncpy(abs_path, req_tar, n_abs);
        query[0] = '\0';
    }

    return true;
}

load function

bool load(FILE* file, BYTE** content, size_t* length)
{
     // checks for a null file pointer
    if(file == NULL)
    {
        return false;
    }
    int n_bytes = 0; 
    BYTE in_bytes = 0;
    BYTE* ptr_bytes; 

    // stores the current byte in a temp memory 
    // reads the file byte by byte and stores every new byte in 
    // temp memory in_bytes than reallocates the ptr_bytes to a larger
    // memory
}

EDITED

1

I'm not sure that this is the whole problem, or even related, but something jumped out at me very quickly. Look at the following from line 4 of lookup():

const char* type = NULL;

This creates a pointer to a char type and sets it to NULL, meaning that there is no space allocated to it. You later try to assign a string to it, but there's nowhere to put that string. I suspect this is causing serious issues. You might do better with something like this:

char type[20];

But the problem here is that it would be the wrong return type. Hmmm....

Remember too that when a char array or string is created in memory, the size cannot be altered, so be sure to allocate enough space to start with.

Next, `text = "some string" doesn't work. You should be using strcpy() to copy a string, or a related function. Finally, the const keyword means that a string cannot be changed once created, so...!

Here's a hint: const char* css = "text/css";

Don't know if this will solve your issue, but it might get you going.

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

1
  • Thanks a lot for your reply! but you suggestion did not work unfortunately. My function was already returning the correct text and I checked with gdb. All the return values were correct. However, I tried your suggestion and it did not work, so I guess that was not the issue. – A.Emad Sep 3 '16 at 10:25
0

After debugging my code like a million times, I finally found the bugs in my code.

In the parse function, the query and abs_path array of chars are not reinitialized to NULL every time parse function runs. In other words, if abs_path had the value "/cat.html" on the first time, the second time abs_path will have the same value, which is /cat.html. As a result, whenever I try to update the value to a shorter string, like cat.jpg for example, the final result is "cat.jpgl". I solved this problem by adding a null terminator after the intended word each time the function runs.

if (n_que != 0)
{
    strncpy(abs_path, req_tar, n_que - 2);
    abs_path[n_que - 2] = '\0';
    strncpy(query, q_cur, (n_abs - n_que) + 1);
    query[(n_abs - n_que) + 1] = '\0';
}
else
{
    strncpy(abs_path, req_tar, n_abs);
    abs_path[n_abs] = '\0';
    query[0] = '\0';
}

In load function I had to initialize BYTE* ptr_bytes to NUL.

BYTE* ptr_bytes = NULL; 

Lookup and indexes functions work just fine !

You must log in to answer this question.

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