0

I used check50 to check the parse function, it works well. But when I am trying to check all the functions, there is always a memory corruption. I tried to print the variables in lookup, it seems that the memory corruption happens in the malloc of token( I cannot print token in the terminal window). Is there anything wrong with lookup function? Or maybe the problem lies in other functions? Could any one help me solve the problem ?

const char* lookup(const char* path)
{
    printf("path in lookup %s\n",path);
    char *last = NULL;
    last = strrchr(path, '.');
    if (last == NULL) {
        return NULL;
    }
    else
    {
        char* token = malloc(strlen(last) + 1);
        strcpy(token,last);
        token[strlen(last)]='\0';
        printf("token %s\n",token);
        if(strcasecmp(token,".css") == 0) 
        {
            free(token);
            free(last);
            return "text/css";

        }
        else if(strcasecmp(token,".html") == 0) 
        {
            printf("token %s\n",token);
            free(token);
            free(last);
            return "text/html";
        }
        else if (strcasecmp(token,".gif") == 0) 
        {
            free(token);
            free(last);
            return "image/gif";
        }
        else if(strcasecmp(token,".ico") == 0) 
        {
            free(token);
            free(last);
            return "image/x-icon";
        }
        else if (strcasecmp(token,".jpg") == 0) 
        {
            free(token);
            free(last);
            return "image/jpeg";
        }
        else if (strcasecmp(token,".js") == 0) 
        {
            free(token);
            free(last);
            return "text/javascript";

        }
        else if(strcasecmp(token,".php") == 0) 
        {
            free(token);
            free(last);
            return "text/x-php";

        }
        else if (strcasecmp(token,".png") == 0) 
        {
            free(token);
            free(last);
            return "image/png";
        }
        else {
            free(token);
            free(last);
            return NULL;
        }
    }
}
1
  • I set a break point to the end of the parse function and then print out the values of the pointers in the parse function. When I am trying to print abs_path, it shows $5 = "/", '\000' <repeats 5527 times>...What does this mean?
    – Charlotte
    Dec 3 '16 at 10:23
0

Subtraction. Sometimes removing code is a better approach than adding code.

First, I think program is crashing at free(last);. last is never malloc'd (it's a pointer to someone else's memory) so it cannot (must not?) be free'd.

Let's look at all the work program is doing, and see where some things can be removed:

       char* token = malloc(strlen(last) + 1);
        strcpy(token,last);
        token[strlen(last)]='\0';
        printf("token %s\n",token);
        if(strcasecmp(token,".css") == 0) 
        {
            free(token);
            free(last);
            return "text/css";

        }
  • This token[strlen(last)]='\0'; is unnecessary. From man strcpy we see:

The strcpy() function copies the string pointed to by src, including the terminating null byte ('\0'),

  • Since token is a copy of last, why not do this if(strcasecmp(last,".css") == 0) instead of if(strcasecmp(token,".css") == 0)? That makes token unnecessary, so we can get rid of

    char* token = malloc(strlen(last) + 1);
    strcpy(token,last);
    
  • Since token is gone, this free(token); is unnecessary.

1
  • Thanks! Get it. Sometimes less is more^^
    – Charlotte
    Dec 4 '16 at 21:43

You must log in to answer this question.

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