title: Memory leak from lack of free() in this function
body: <p>The following code is used to find the path of an executable for a rudimentary shell in <code>c</code>. As you can see, I dynamically allocate the <code>path</code> variable and then check to see if the path exists (via <code>lstat</code> in a helper function <code>cmd_exists</code>). I then return the path variable. My problem is, this causes a memory leak, as <code>path</code> is never freed. I can't free path before I return its value, and as of right now I can't think of any way to to release the memory that has been allocated. If anyone can help me out I'd appreciate it. Thanks</p>
  
  <pre><code>char * find_path(char * mypath, char * command){
       char * token = strtok(mypath, "#");
       while(token != NULL){
                           /*token size + 1 (for /) + command size*/
            char * path = calloc(strlen(token)+1+strlen(command)+1, sizeof(char));
            strcat(path, token);
            strcat(path, "/");
            strcat(path, command);
            if(cmd_exists(path) == 1){
               return path;
            }
            token = strtok(NULL, "#");
       }
       return NULL;
  }
  </code></pre>