Commit 868488b5 authored by Daniel Stenberg's avatar Daniel Stenberg
Browse files

memory leak cleanup campaign

parent 7f77a061
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -620,7 +620,7 @@ CURLcode curl_transfer(CURL *curl)
{
  CURLcode res;
  struct UrlData *data = curl;
  struct connectdata *c_connect;
  struct connectdata *c_connect=NULL;

  pgrsStartNow(data);

+2 −1
Original line number Diff line number Diff line
@@ -390,6 +390,7 @@ CURLcode http(struct connectdata *conn)

    if(co) {
      int count=0;
      struct Cookie *store=co;
      /* now loop through all cookies that matched */
      while(co) {
        if(co->value && strlen(co->value)) {
@@ -405,7 +406,7 @@ CURLcode http(struct connectdata *conn)
      if(count) {
        add_buffer(req_buffer, "\r\n", 2);
      }
      cookie_freelist(co); /* free the cookie list */
      cookie_freelist(store); /* free the cookie list */
      co=NULL;
    }

+45 −20
Original line number Diff line number Diff line
@@ -685,26 +685,7 @@ CURLcode curl_disconnect(CURLconnect *c_connect)
  return CURLE_OK;
}


/*
 * NAME curl_connect()
 *
 * DESCRIPTION
 *
 * Connects to the peer server and performs the initial setup. This function
 * writes a connect handle to its second argument that is a unique handle for
 * this connect. This allows multiple connects from the same handle returned
 * by curl_open().
 *
 * EXAMPLE
 *
 * CURLCode result;
 * CURL curl;
 * CURLconnect connect;
 * result = curl_connect(curl, &connect);
 */

CURLcode curl_connect(CURL *curl, CURLconnect **in_connect)
static CURLcode _connect(CURL *curl, CURLconnect **in_connect)
{
  char *tmp;
  char *buf;
@@ -1537,6 +1518,50 @@ CURLcode curl_connect(CURL *curl, CURLconnect **in_connect)
  return CURLE_OK;
}

CURLcode curl_connect(CURL *curl, CURLconnect **in_connect)
{
  CURLcode code;
  struct connectdata *conn;

  /* call the stuff that needs to be called */
  code = _connect(curl, in_connect);

  if(CURLE_OK != code) {
    /* We're not allowed to return failure with memory left allocated
       in the connectdata struct, free those here */
    conn = (struct connectdata *)*in_connect;
    if(conn) {
      if(conn->hostent_buf)
        free(conn->hostent_buf);
      free(conn);
      *in_connect=NULL;
    }
  }
  return code;
}


/*
 * NAME curl_connect()
 *
 * DESCRIPTION
 *
 * Connects to the peer server and performs the initial setup. This function
 * writes a connect handle to its second argument that is a unique handle for
 * this connect. This allows multiple connects from the same handle returned
 * by curl_open().
 *
 * EXAMPLE
 *
 * CURLCode result;
 * CURL curl;
 * CURLconnect connect;
 * result = curl_connect(curl, &connect);
 */




CURLcode curl_done(CURLconnect *c_connect)
{
  struct connectdata *conn = c_connect;
+274 −232
Original line number Diff line number Diff line
@@ -365,6 +365,7 @@ static char *my_get_token(const char *line);
static void GetStr(char **string,
		   char *value)
{
  fprintf(stderr, "called\n");
  if(*string)
    free(*string);
  if(value && *value)
@@ -1278,6 +1279,8 @@ int main(int argc, char *argv[])
  int infilesize=-1; /* -1 means unknown */
  bool stillflags=TRUE;

  bool allocuseragent=FALSE;

  CURL *curl;
  int res;
  int i;
@@ -1388,6 +1391,8 @@ int main(int argc, char *argv[])
             CURL_NAME "/" CURL_VERSION " (" OS ") " "%s", curl_version());
    config.useragent= useragent;
  }
  else
    allocuseragent = TRUE;
#if 0
  fprintf(stderr, "URL: %s PROXY: %s\n", url, config.proxy?config.proxy:"none");
#endif
@@ -1398,7 +1403,9 @@ int main(int argc, char *argv[])
  if(res != CURLE_OK)
    return res;

  outfiles = config.outfile;		/* save outfile pattern befor expansion */
  /* save outfile pattern befor expansion */
  outfiles = strdup(config.outfile);

  if (!outfiles && !config.remotefile && urlnum > 1) {
#ifdef CURL_SEPARATORS
    /* multiple files extracted to stdout, insert separators! */
@@ -1412,15 +1419,10 @@ int main(int argc, char *argv[])
#endif
  }
  for (i = 0; (url = next_url(urls)); ++i) {
    if (outfiles)
      config.outfile = strdup(outfiles);

#if 0
  if(config.outfile && config.infile) {
    helpf("you can't both upload and download!\n");
    return CURLE_FAILED_INIT;
    if (outfiles) {
      free(config.outfile);
      config.outfile = outfiles;
    }
#endif
 
    if (config.outfile || config.remotefile) {
      /* 
@@ -1428,21 +1430,25 @@ int main(int argc, char *argv[])
       * decided we want to use the remote file name.
       */
      
    if(config.remotefile) {
      if(!config.outfile && config.remotefile) {
        /* Find and get the remote file name */
        config.outfile=strstr(url, "://");
        if(config.outfile)
          config.outfile+=3;
        else
          config.outfile=url;
      config.outfile = strrchr(config.outfile, '/');
        config.outfile = strdup(strrchr(config.outfile, '/'));
        if(!config.outfile || !strlen(++config.outfile)) {
          helpf("Remote file name has no length!\n");
          return CURLE_WRITE_ERROR;
        }
      }
    else	/* fill '#1' ... '#9' terms from URL pattern */
      else {
	/* fill '#1' ... '#9' terms from URL pattern */
        char *outfile = config.outfile;
        config.outfile = match_url(config.outfile, *urls);
        free(outfile);
      }
      
      if((0 == config.resume_from) && config.use_resume) {
        /* we're told to continue where we are now, then we get the size of the
@@ -1483,8 +1489,9 @@ int main(int argc, char *argv[])
        ptr=url;
      ptr = strrchr(ptr, '/');
      if(!ptr || !strlen(++ptr)) {
      /* The URL has no file name part, add the local file name. In order
         to be able to do so, we have to create a new URL in another buffer.*/
        /* The URL has no file name part, add the local file name. In order to
           be able to do so, we have to create a new URL in another buffer.*/

        urlbuffer=(char *)malloc(strlen(url) + strlen(config.infile) + 3);
        if(!urlbuffer) {
          helpf("out of memory\n");
@@ -1515,8 +1522,7 @@ int main(int argc, char *argv[])
    }
    if(config.headerfile) {
      /* open file for output: */
    if(strcmp(config.headerfile,"-"))
    {
      if(strcmp(config.headerfile,"-")) {
        heads.filename = config.headerfile;
        headerfilep=NULL;
      }
@@ -1527,13 +1533,14 @@ int main(int argc, char *argv[])
    
    if(outs.stream && isatty(fileno(outs.stream)) &&
       !(config.conf&(CONF_UPLOAD|CONF_HTTPPOST)))
    /* we send the output to a tty and it isn't an upload operation, therefore
       we switch off the progress meter */
      /* we send the output to a tty and it isn't an upload operation,
         therefore we switch off the progress meter */
      config.conf |= CONF_NOPROGRESS;
    

    if (urlnum > 1) {
    fprintf(stderr, "\n[%d/%d]: %s --> %s\n", i+1, urlnum, url, config.outfile ? config.outfile : "<stdout>");
      fprintf(stderr, "\n[%d/%d]: %s --> %s\n",
              i+1, urlnum, url, config.outfile ? config.outfile : "<stdout>");
      if (separator) {
#ifdef CURL_SEPARATORS
        printf("%s%s\n", CURLseparator, url);
@@ -1574,10 +1581,12 @@ int main(int argc, char *argv[])
      curl_easy_setopt(curl, CURLOPT_HEADER, config.conf&CONF_HEADER);
      curl_easy_setopt(curl, CURLOPT_NOPROGRESS, config.conf&CONF_NOPROGRESS);
      curl_easy_setopt(curl, CURLOPT_NOBODY, config.conf&CONF_NOBODY);
    curl_easy_setopt(curl, CURLOPT_FAILONERROR, config.conf&CONF_FAILONERROR);
      curl_easy_setopt(curl, CURLOPT_FAILONERROR,
                       config.conf&CONF_FAILONERROR);
      curl_easy_setopt(curl, CURLOPT_UPLOAD, config.conf&CONF_UPLOAD);
      curl_easy_setopt(curl, CURLOPT_POST, config.conf&CONF_POST);
    curl_easy_setopt(curl, CURLOPT_FTPLISTONLY, config.conf&CONF_FTPLISTONLY);
      curl_easy_setopt(curl, CURLOPT_FTPLISTONLY,
                       config.conf&CONF_FTPLISTONLY);
      curl_easy_setopt(curl, CURLOPT_FTPAPPEND, config.conf&CONF_FTPAPPEND);
      curl_easy_setopt(curl, CURLOPT_NETRC, config.conf&CONF_NETRC);
      curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,
@@ -1596,7 +1605,8 @@ int main(int argc, char *argv[])
      curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, config.postfieldsize);

      curl_easy_setopt(curl, CURLOPT_REFERER, config.referer);
    curl_easy_setopt(curl, CURLOPT_AUTOREFERER, config.conf&CONF_AUTO_REFERER);
      curl_easy_setopt(curl, CURLOPT_AUTOREFERER,
                       config.conf&CONF_AUTO_REFERER);
      curl_easy_setopt(curl, CURLOPT_USERAGENT, config.useragent);
      curl_easy_setopt(curl, CURLOPT_FTPPORT, config.ftpport);
      curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, config.low_speed_limit);
@@ -1611,7 +1621,8 @@ int main(int argc, char *argv[])
      curl_easy_setopt(curl, CURLOPT_CRLF, config.crlf);
      curl_easy_setopt(curl, CURLOPT_QUOTE, config.quote);
      curl_easy_setopt(curl, CURLOPT_POSTQUOTE, config.postquote);
    curl_easy_setopt(curl, CURLOPT_WRITEHEADER, config.headerfile?&heads:NULL);
      curl_easy_setopt(curl, CURLOPT_WRITEHEADER,
                       config.headerfile?&heads:NULL);
      curl_easy_setopt(curl, CURLOPT_COOKIEFILE, config.cookiefile);
      curl_easy_setopt(curl, CURLOPT_SSLVERSION, config.ssl_version);
      curl_easy_setopt(curl, CURLOPT_TIMECONDITION, config.timecond);
@@ -1667,19 +1678,50 @@ int main(int argc, char *argv[])
    if(headerfilep)
      fclose(headerfilep);
    
  if(config.url)
    free(config.url);

    if(url)
      free(url);
  if(config.outfile && !config.remotefile)
    free(config.outfile);

  }
#ifdef MIME_SEPARATORS
  if (separator)
    printf("--%s--\n", MIMEseparator);
#endif

  if(config.url)
    free(config.url);
  if(config.userpwd)
    free(config.userpwd);
  if(config.postfields)
    free(config.postfields);
  if(config.proxy)
    free(config.proxy);
  if(config.proxyuserpwd)
    free(config.proxyuserpwd);
  if(config.cookie)
    free(config.cookie);
  if(config.cookiefile)
    free(config.cookiefile);
  if(config.krb4level)
    free(config.krb4level);
  if(config.headerfile)
    free(config.headerfile);
  if(config.outfile)
    free(config.outfile);
  if(config.infile)
    free(config.infile);
  if(config.range)
    free(config.range);
  if(config.customrequest)
    free(config.customrequest);
  if(config.writeout)
    free(config.writeout);

  if(config.httppost)
    curl_formfree(config.httppost);

  if(allocuseragent)
    free(config.useragent);

  /* cleanup memory used for URL globbing patterns */
  glob_cleanup(urls);

@@ -1687,7 +1729,7 @@ int main(int argc, char *argv[])
  curl_slist_free_all(config.postquote); /*  */
  curl_slist_free_all(config.headers); /*  */

  return(res);
  return res;
}

static char *my_get_line(FILE *fp)