Hi,

using the new valgrind testing feature (yet not completely discussed), I found
and fixed several memory leaks.

Also, I found that parsing the proxy URL did not include the iri (retr.c:
764). It seemed to me as it just has been forgotten. I added the iri to
url_parse() there. We have no test with an IDN proxy - maybe we should have
one.

As I already wrote, 24 perl tests didn't survive valgrind memory testing.
18 of it will be fixed by this patch.

6 others have a problem with libidn (illegal memory access). I'll investigate
this after this email.

Tim
From ce60b2c383972f90a88ecf2425444c5552b67b4d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tim Rühsen?= <[email protected]>
Date: Thu, 9 Oct 2014 12:17:07 +0200
Subject: [PATCH] fixing several memory leaks

---
 src/ChangeLog | 11 +++++++++++
 src/ftp.c     |  1 +
 src/http.c    |  3 ++-
 src/init.c    | 14 ++++++++------
 src/main.c    |  2 ++
 src/retr.c    |  4 +++-
 src/url.c     |  1 +
 7 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 2c20679..9df40ec 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,14 @@
+2014-10-09  Tim Ruehsen  <[email protected]>
+
+	* ftp.c (ftp_loop_internal): Fix memory leak
+	* http.c (gethttp): Fix two memory leaks
+	* init.c (cleanup): Fix compile errors and warnings
+	* init.c (cleanup): Free more opt variables
+	* main.c (main): Fix two memory leaks
+	* retr.c (retrieve_url): Fix two memory leaks
+	* retr.c (retrieve_url): Add iri to call to url_parse()
+	* url.c (url_parse): Fix memory leak
+
 2014-10-06  Tim Ruehsen  <[email protected]>

 	* url.c (url_parse): little code cleanup
diff --git a/src/ftp.c b/src/ftp.c
index 2d54333..07131d4 100644
--- a/src/ftp.c
+++ b/src/ftp.c
@@ -1560,6 +1560,7 @@ ftp_loop_internal (struct url *u, struct fileinfo *f, ccon *con, char **local_fi
   else
     {
       /* URL-derived file.  Consider "-O file" name. */
+      xfree (con->target);
       con->target = url_file_name (u, NULL);
       if (!opt.output_document)
         locf = con->target;
diff --git a/src/http.c b/src/http.c
index 4b99c17..5ac69d0 100644
--- a/src/http.c
+++ b/src/http.c
@@ -1756,7 +1756,7 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy,
   hs->res = -1;
   hs->rderrmsg = NULL;
   hs->newloc = NULL;
-  hs->remote_time = NULL;
+  xfree(hs->remote_time); hs->remote_time = NULL;
   hs->error = NULL;
   hs->message = NULL;

@@ -1868,6 +1868,7 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy,
                 {
                   logprintf (LOG_NOTQUIET, _("BODY data file %s missing: %s\n"),
                              quote (opt.body_file), strerror (errno));
+                  request_free (req);
                   return FILEBADFILE;
                 }
             }
diff --git a/src/init.c b/src/init.c
index 93e95f8..9e0fdc4 100644
--- a/src/init.c
+++ b/src/init.c
@@ -69,6 +69,7 @@ as that of the covered work.  */
 #include "retr.h"               /* for output_stream */
 #include "warc.h"               /* for warc_close */
 #include "spider.h"             /* for spider_cleanup */
+#include "html-url.h"           /* for cleanup_html_url */

 #ifdef TESTING
 #include "test.h"
@@ -1729,10 +1730,7 @@ cleanup (void)
   spider_cleanup ();
   host_cleanup ();
   log_cleanup ();
-  netrc_cleanup (netrc_list);
-
-  for (i = 0; i < nurl; i++)
-    xfree (url[i]);
+  netrc_cleanup ();

   xfree_null (opt.choose_config);
   xfree_null (opt.lfilename);
@@ -1741,8 +1739,8 @@ cleanup (void)
   xfree_null (opt.output_document);
   free_vec (opt.accepts);
   free_vec (opt.rejects);
-  free_vec (opt.excludes);
-  free_vec (opt.includes);
+  free_vec ((char **)opt.excludes);
+  free_vec ((char **)opt.includes);
   free_vec (opt.domains);
   free_vec (opt.follow_tags);
   free_vec (opt.ignore_tags);
@@ -1774,6 +1772,10 @@ cleanup (void)
   xfree_null (opt.passwd);
   xfree_null (opt.base_href);
   xfree_null (opt.method);
+  xfree_null (opt.post_file_name);
+  xfree_null (opt.post_data);
+  xfree_null (opt.body_data);
+  xfree_null (opt.body_file);

 #endif /* DEBUG_MALLOC */
 }
diff --git a/src/main.c b/src/main.c
index 1ada822..4fefe04 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1455,11 +1455,13 @@ for details.\n\n"));
         if (opt.post_data)
           {
             setoptval ("bodydata", opt.post_data, "body-data");
+            xfree(opt.post_data);
             opt.post_data = NULL;
           }
         else
           {
             setoptval ("bodyfile", opt.post_file_name, "body-file");
+            xfree(opt.post_file_name);
             opt.post_file_name = NULL;
           }
     }
diff --git a/src/retr.c b/src/retr.c
index 71b5c28..ff5f5b3 100644
--- a/src/retr.c
+++ b/src/retr.c
@@ -761,7 +761,7 @@ retrieve_url (struct url * orig_parsed, const char *origurl, char **file,
       pi->utf8_encode = false;

       /* Parse the proxy URL.  */
-      proxy_url = url_parse (proxy, &up_error_code, NULL, true);
+      proxy_url = url_parse (proxy, &up_error_code, pi, true);
       if (!proxy_url)
         {
           char *error = url_error (proxy, up_error_code);
@@ -782,6 +782,7 @@ retrieve_url (struct url * orig_parsed, const char *origurl, char **file,
           result = PROXERR;
           goto bail;
         }
+      iri_free(pi);
       free (proxy);
     }

@@ -930,6 +931,7 @@ retrieve_url (struct url * orig_parsed, const char *origurl, char **file,
       if (u)
         {
           DEBUGP (("[IRI fallbacking to non-utf8 for %s\n", quote (url)));
+          xfree (url);
           url = xstrdup (u->url);
           iri_fallbacked = 1;
           goto redirected;
diff --git a/src/url.c b/src/url.c
index 0551c58..3e1f664 100644
--- a/src/url.c
+++ b/src/url.c
@@ -705,6 +705,7 @@ url_parse (const char *url, int *error, struct iri *iri, bool percent_encode)
         new_url = NULL;
       else
         {
+          xfree_null (iri->orig_url);
           iri->orig_url = xstrdup (url);
           url_encoded = reencode_escapes (new_url);
           if (url_encoded != new_url)
--
2.1.1

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to