This patch removes xfree_null() and replaces it by xfree().
xfree() automatically nullifies the argument after freeing to reduce dangling
pointer problems like double frees.

I also cleaned up code like
        if (p)
          xfree (p);

and
        xfree(p);
        p = NULL;

Both cases reduce to a call to xfree(p).

check and distcheck tested, also valgrind tested.

Please review and comment.

Tim
From ede5a27de1e2e5447ec31a918af789e657b53b1b Mon Sep 17 00:00:00 2001
From: Tim Ruehsen <tim.rueh...@gmx.de>
Date: Sat, 29 Nov 2014 17:54:20 +0100
Subject: [PATCH] Replaced xfree_null() by xfree() and nullify argument after
 freeing.

---
 src/ChangeLog   |   9 +++++
 src/connect.c   |   3 +-
 src/cookies.c   |  12 +++---
 src/ftp-basic.c |   2 +-
 src/ftp-ls.c    |   6 +--
 src/ftp.c       |  12 +++---
 src/hash.c      |   9 +++--
 src/host.c      |   3 +-
 src/html-url.c  |   7 ++--
 src/http.c      | 119 +++++++++++++++++++++++++-------------------------------
 src/init.c      |  86 ++++++++++++++++++++--------------------
 src/iri.c       |   8 ++--
 src/log.c       |  11 ++----
 src/main.c      |   8 ++--
 src/mswindows.c |   4 +-
 src/netrc.c     |  14 +++----
 src/openssl.c   |   4 +-
 src/recur.c     |   6 +--
 src/res.c       |   3 +-
 src/retr.c      |  21 +++++-----
 src/url.c       |  16 ++++----
 src/utils.h     |   7 +---
 src/warc.c      |   6 +--
 23 files changed, 176 insertions(+), 200 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index ac2542c..920d822 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,12 @@
+2014-11-29  Tim Ruehsen <tim.rueh...@gmx.de>
+
+	* utils.h: xfree() sets argument to NULL after freeing,
+	removed xfree_null()
+	* connect.c, cookies.c, ftp-basic.c, ftp-ls.c, ftp.c hash.c,
+	host.c, html-url.c, http.c, init.c, iri.c, log.c, main.c,
+	mswindows.c, netrc.c, openssl.c, recur.c, res.c, retr.c,
+	url.c, warc.c: Replaced xfree_null() by xfree()
+
 2014-11-28  Tim Ruehsen <tim.rueh...@gmx.de>

 	* main.c: Fix length of program_argstring,
diff --git a/src/connect.c b/src/connect.c
index bc8d133..727d6a6 100644
--- a/src/connect.c
+++ b/src/connect.c
@@ -284,8 +284,7 @@ connect_to_ip (const ip_address *ip, int port, const char *print)
           logprintf (LOG_VERBOSE, _("Connecting to %s|%s|:%d... "),
                      str ? str : escnonprint_uri (print), txt_addr, port);

-          if (str)
-              xfree (str);
+          xfree (str);
         }
       else
         {
diff --git a/src/cookies.c b/src/cookies.c
index 96af412..d4e0222 100644
--- a/src/cookies.c
+++ b/src/cookies.c
@@ -153,10 +153,10 @@ cookie_expired_p (const struct cookie *c)
 static void
 delete_cookie (struct cookie *cookie)
 {
-  xfree_null (cookie->domain);
-  xfree_null (cookie->path);
-  xfree_null (cookie->attr);
-  xfree_null (cookie->value);
+  xfree (cookie->domain);
+  xfree (cookie->path);
+  xfree (cookie->attr);
+  xfree (cookie->value);
   xfree (cookie);
 }

@@ -376,7 +376,7 @@ parse_set_cookie (const char *set_cookie, bool silent)
         {
           if (!TOKEN_NON_EMPTY (value))
             goto error;
-          xfree_null (cookie->domain);
+          xfree (cookie->domain);
           /* Strictly speaking, we should set cookie->domain_exact if the
              domain doesn't begin with a dot.  But many sites set the
              domain to "foo.com" and expect "subhost.foo.com" to get the
@@ -389,7 +389,7 @@ parse_set_cookie (const char *set_cookie, bool silent)
         {
           if (!TOKEN_NON_EMPTY (value))
             goto error;
-          xfree_null (cookie->path);
+          xfree (cookie->path);
           cookie->path = strdupdelim (value.b, value.e);
         }
       else if (TOKEN_IS (name, "expires"))
diff --git a/src/ftp-basic.c b/src/ftp-basic.c
index 2f5765e..f9b9ad2 100644
--- a/src/ftp-basic.c
+++ b/src/ftp-basic.c
@@ -1136,7 +1136,7 @@ ftp_pwd (int csock, char **pwd)
     goto err;

   /* Has the `pwd' been already allocated?  Free! */
-  xfree_null (*pwd);
+  xfree (*pwd);

   *pwd = xstrdup (request);

diff --git a/src/ftp-ls.c b/src/ftp-ls.c
index e129191..399d1b4 100644
--- a/src/ftp-ls.c
+++ b/src/ftp-ls.c
@@ -363,8 +363,8 @@ ftp_parse_unix_ls (const char *file, int ignore_perms)
       if (error || ignore)
         {
           DEBUGP (("Skipping.\n"));
-          xfree_null (cur.name);
-          xfree_null (cur.linkto);
+          xfree (cur.name);
+          xfree (cur.linkto);
           continue;
         }

@@ -1089,7 +1089,7 @@ ftp_index (const char *file, struct url *u, struct fileinfo *f)
       else
         upwd = concat_strings (tmpu, "@", (char *) 0);
       xfree (tmpu);
-      xfree_null (tmpp);
+      xfree (tmpp);
     }
   else
     upwd = xstrdup ("");
diff --git a/src/ftp.c b/src/ftp.c
index e57c21c..9ea8819 100644
--- a/src/ftp.c
+++ b/src/ftp.c
@@ -449,7 +449,7 @@ Error in server response, closing control connection.\n"));
           return err;
         case FTPSRVERR :
           /* PWD unsupported -- assume "/". */
-          xfree_null (con->id);
+          xfree (con->id);
           con->id = xstrdup ("/");
           break;
         case FTPOK:
@@ -1703,7 +1703,7 @@ ftp_loop_internal (struct url *u, struct fileinfo *f, ccon *con, char **local_fi
           if (err == FOPEN_EXCL_ERR)
             {
               /* Re-determine the file name. */
-              xfree_null (con->target);
+              xfree (con->target);
               con->target = url_file_name (u, NULL);
               locf = con->target;
             }
@@ -2473,10 +2473,8 @@ ftp_loop (struct url *u, char **local_file, int *dt, struct url *proxy,
   /* If a connection was left, quench it.  */
   if (con.csock != -1)
     fd_close (con.csock);
-  xfree_null (con.id);
-  con.id = NULL;
-  xfree_null (con.target);
-  con.target = NULL;
+  xfree (con.id);
+  xfree (con.target);
   return res;
 }

@@ -2490,7 +2488,7 @@ delelement (struct fileinfo *f, struct fileinfo **start)
   struct fileinfo *next = f->next;

   xfree (f->name);
-  xfree_null (f->linkto);
+  xfree (f->linkto);
   xfree (f);

   if (next)
diff --git a/src/hash.c b/src/hash.c
index 9514d23..cd9cfeb 100644
--- a/src/hash.c
+++ b/src/hash.c
@@ -46,10 +46,11 @@ as that of the covered work.  */
 # include "utils.h"
 #else
 /* Make do without them. */
-# define xnew(x) xmalloc (sizeof (x))
-# define xnew_array(type, x) xmalloc (sizeof (type) * (x))
-# define xmalloc malloc
-# define xfree free
+# define xnew(type) (xmalloc (sizeof (type)))
+# define xnew0(type) (xcalloc (1, sizeof (type)))
+# define xnew_array(type, len) (xmalloc ((len) * sizeof (type)))
+# define xfree(p) do { free ((void *) (p)); p = NULL; } while (0)
+
 # ifndef countof
 #  define countof(x) (sizeof (x) / sizeof ((x)[0]))
 # endif
diff --git a/src/host.c b/src/host.c
index 14380e5..d0dbe50 100644
--- a/src/host.c
+++ b/src/host.c
@@ -747,8 +747,7 @@ lookup_host (const char *host, int flags)
       logprintf (LOG_VERBOSE, _("Resolving %s... "),
                  quotearg_style (escape_quoting_style, str ? str : host));

-      if (str)
-        xfree (str);
+      xfree (str);
     }

 #ifdef ENABLE_IPV6
diff --git a/src/html-url.c b/src/html-url.c
index fb4cad1..07c29bb 100644
--- a/src/html-url.c
+++ b/src/html-url.c
@@ -488,8 +488,7 @@ tag_handle_base (int tagid _GL_UNUSED, struct taginfo *tag, struct map_context *
   base_urlpos->ignore_when_downloading = 1;
   base_urlpos->link_base_p = 1;

-  if (ctx->base)
-    xfree (ctx->base);
+  xfree (ctx->base);
   if (ctx->parent_base)
     ctx->base = uri_merge (ctx->parent_base, newbase);
   else
@@ -630,7 +629,7 @@ tag_handle_meta (int tagid _GL_UNUSED, struct taginfo *tag, struct map_context *
       if (!mcharset)
         return;

-      xfree_null (meta_charset);
+      xfree (meta_charset);
       meta_charset = mcharset;
     }
   else if (name && 0 == c_strcasecmp (name, "robots"))
@@ -758,7 +757,7 @@ get_urls_html (const char *file, const char *url, bool *meta_disallow_follow,
   if (meta_disallow_follow)
     *meta_disallow_follow = ctx.nofollow;

-  xfree_null (ctx.base);
+  xfree (ctx.base);
   wget_read_file_free (fm);
   return ctx.head;
 }
diff --git a/src/http.c b/src/http.c
index c219fcf..1a6cd39 100644
--- a/src/http.c
+++ b/src/http.c
@@ -236,7 +236,7 @@ request_set_header (struct request *req, const char *name, const char *value,
       /* A NULL value is a no-op; if freeing the name is requested,
          free it now to avoid leaks.  */
       if (release_policy == rel_name || release_policy == rel_both)
-        xfree ((void *)name);
+        xfree (name);
       return;
     }

@@ -387,10 +387,10 @@ static void
 request_free (struct request *req)
 {
   int i;
-  xfree_null (req->arg);
+  xfree (req->arg);
   for (i = 0; i < req->hcount; i++)
     release_header (&req->headers[i]);
-  xfree_null (req->headers);
+  xfree (req->headers);
   xfree (req);
 }

@@ -818,7 +818,7 @@ resp_status (const struct response *resp, char **message)
 static void
 resp_free (struct response *resp)
 {
-  xfree_null (resp->headers);
+  xfree (resp->headers);
   xfree (resp);
 }

@@ -948,7 +948,7 @@ skip_short_body (int fd, wgint contlen, bool chunked)
               if (remaining_chunk_size == 0)
                 {
                   line = fd_read_line (fd);
-                  xfree_null (line);
+                  xfree (line);
                   break;
                 }
             }
@@ -1475,18 +1475,13 @@ struct http_stat
 static void
 free_hstat (struct http_stat *hs)
 {
-  xfree_null (hs->newloc);
-  xfree_null (hs->remote_time);
-  xfree_null (hs->error);
-  xfree_null (hs->rderrmsg);
-  xfree_null (hs->local_file);
-  xfree_null (hs->orig_file_name);
-  xfree_null (hs->message);
-
-  /* Guard against being called twice. */
-  hs->newloc = NULL;
-  hs->remote_time = NULL;
-  hs->error = NULL;
+  xfree (hs->newloc);
+  xfree (hs->remote_time);
+  xfree (hs->error);
+  xfree (hs->rderrmsg);
+  xfree (hs->local_file);
+  xfree (hs->orig_file_name);
+  xfree (hs->message);
 }

 static void
@@ -1756,7 +1751,7 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy,
   hs->res = -1;
   hs->rderrmsg = NULL;
   hs->newloc = NULL;
-  xfree(hs->remote_time); hs->remote_time = NULL;
+  xfree(hs->remote_time);
   hs->error = NULL;
   hs->message = NULL;

@@ -2078,7 +2073,7 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy,
               request_free (req);
               return HERR;
             }
-          xfree_null(hs->message);
+          xfree(hs->message);
           hs->message = xstrdup (message);
           resp_free (resp);
           xfree (head);
@@ -2087,11 +2082,11 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy,
             failed_tunnel:
               logprintf (LOG_NOTQUIET, _("Proxy tunneling failed: %s"),
                          message ? quotearg_style (escape_quoting_style, message) : "?");
-              xfree_null (message);
+              xfree (message);
               request_free (req);
               return CONSSLERR;
             }
-          xfree_null (message);
+          xfree (message);

           /* SOCK is now *really* connected to u->host, so update CONN
              to reflect this.  That way register_persistent will
@@ -2260,7 +2255,7 @@ read_header:
       goto read_header;
     }

-  xfree_null(hs->message);
+  xfree(hs->message);
   hs->message = xstrdup (message);
   if (!opt.server_response)
     logprintf (LOG_VERBOSE, "%2d %s\n", statcode,
@@ -2350,13 +2345,13 @@ read_header:
                                     u->url, warc_timestamp_str,
                                     warc_request_uuid, warc_ip, type,
                                     statcode, head);
-          xfree_null (type);
+          xfree (type);

           if (_err != RETRFINISHED || hs->res < 0)
             {
               CLOSE_INVALIDATE (sock);
               request_free (req);
-              xfree_null (message);
+              xfree (message);
               resp_free (resp);
               xfree (head);
               return _err;
@@ -2477,12 +2472,11 @@ read_header:
                     }

                   xfree (pth);
-                  xfree_null (message);
+                  xfree (message);
                   resp_free (resp);
                   xfree (head);
                   xfree (auth_stat);
                   xfree (hs->message);
-                  hs->message = NULL;
                   goto retry_with_auth;
                 }
               else
@@ -2497,7 +2491,7 @@ read_header:
             }
         }
       request_free (req);
-      xfree_null (message);
+      xfree (message);
       resp_free (resp);
       xfree (head);
       if (auth_err == RETROK)
@@ -2521,8 +2515,7 @@ read_header:
       CLOSE_FINISH (sock);
       request_free (req);
       xfree (hs->message);
-      hs->message = NULL;
-      xfree_null (message);
+      xfree (message);
       resp_free (resp);
       xfree (head);

@@ -2553,7 +2546,7 @@ read_header:
           hs->local_file = url_file_name (u, local_file);
         }

-      xfree_null (local_file);
+      xfree (local_file);
     }

   /* TODO: perform this check only once. */
@@ -2568,7 +2561,7 @@ read_header:
           request_free (req);
           resp_free (resp);
           xfree (head);
-          xfree_null (message);
+          xfree (message);
           return RETRUNNEEDED;
         }
       else if (!ALLOW_CLOBBER)
@@ -2651,7 +2644,7 @@ read_header:
     hs->error = xstrdup (_("(no description)"));
   else
     hs->error = xstrdup (message);
-  xfree_null (message);
+  xfree (message);

   type = resp_header_strdup (resp, "Content-Type");
   if (type)
@@ -2672,7 +2665,7 @@ read_header:
               tmp = parse_charset (tmp2);
               if (tmp)
                 set_content_encoding (iri, tmp);
-              xfree_null(tmp);
+              xfree(tmp);
             }
         }
     }
@@ -2705,7 +2698,7 @@ read_header:
       hs->restval = 0;

       CLOSE_FINISH (sock);
-      xfree_null (type);
+      xfree (type);
       xfree (head);

       return RETRFINISHED;
@@ -2746,7 +2739,7 @@ read_header:
               if (_err != RETRFINISHED || hs->res < 0)
                 {
                   CLOSE_INVALIDATE (sock);
-                  xfree_null (type);
+                  xfree (type);
                   xfree (head);
                   return _err;
                 }
@@ -2763,7 +2756,7 @@ read_header:
                 CLOSE_INVALIDATE (sock);
             }

-          xfree_null (type);
+          xfree (type);
           xfree (head);
           /* From RFC2616: The status codes 303 and 307 have
              been added for servers that wish to make unambiguously
@@ -2846,7 +2839,7 @@ read_header:
       hs->res = 0;
       /* Mark as successfully retrieved. */
       *dt |= RETROKF;
-      xfree_null (type);
+      xfree (type);
       if (statcode == HTTP_STATUS_RANGE_NOT_SATISFIABLE)
         CLOSE_FINISH (sock);
       else
@@ -2860,7 +2853,7 @@ read_header:
     {
       /* The Range request was somehow misunderstood by the server.
          Bail out.  */
-      xfree_null (type);
+      xfree (type);
       CLOSE_INVALIDATE (sock);
       xfree (head);
       return RANGEERR;
@@ -2927,7 +2920,7 @@ read_header:
             {
               CLOSE_INVALIDATE (sock);
               xfree (head);
-              xfree_null (type);
+              xfree (type);
               return _err;
             }
           else
@@ -2952,7 +2945,7 @@ read_header:
         }

       xfree (head);
-      xfree_null (type);
+      xfree (type);
       return RETRFINISHED;
     }

@@ -2993,7 +2986,7 @@ read_header:
                              strerror (errno));
                   CLOSE_INVALIDATE (sock);
                   xfree (head);
-                  xfree_null (type);
+                  xfree (type);
                   return UNLINKERR;
                 }
             }
@@ -3021,7 +3014,7 @@ read_header:
                          hs->local_file);
               CLOSE_INVALIDATE (sock);
               xfree (head);
-              xfree_null (type);
+              xfree (type);
               return FOPEN_EXCL_ERR;
             }
         }
@@ -3030,7 +3023,7 @@ read_header:
           logprintf (LOG_NOTQUIET, "%s: %s\n", hs->local_file, strerror (errno));
           CLOSE_INVALIDATE (sock);
           xfree (head);
-          xfree_null (type);
+          xfree (type);
           return FOPENERR;
         }
     }
@@ -3050,7 +3043,7 @@ read_header:

   /* Now we no longer need to store the response header. */
   xfree (head);
-  xfree_null (type);
+  xfree (type);

   if (hs->res >= 0)
     CLOSE_FINISH (sock);
@@ -3370,7 +3363,7 @@ Remote file does not exist -- broken link!!!\n"));
             }
           logputs (LOG_VERBOSE, "\n");
           ret = WRONGCODE;
-          xfree_null (hurl);
+          xfree (hurl);
           goto exit;
         }

@@ -3494,10 +3487,8 @@ Remote file exists.\n\n"));
               got_name = true;
               *dt &= ~HEAD_ONLY;
               count = 0;          /* the retrieve count for HEAD is reset */
-              xfree_null (hstat.message);
-              xfree_null (hstat.error);
-              hstat.message = NULL;
-              hstat.error = NULL;
+              xfree (hstat.message);
+              xfree (hstat.error);
               continue;
             } /* send_head_first */
         } /* !got_head */
@@ -3646,7 +3637,7 @@ Remote file exists.\n\n"));
 exit:
   if (ret == RETROK && local_file)
     {
-      xfree_null (*local_file);
+      xfree (*local_file);
       *local_file = xstrdup (hstat.local_file);
     }
   free_hstat (&hstat);
@@ -3866,23 +3857,21 @@ digest_authentication_encode (const char *au, const char *user,
   if (qop != NULL && strcmp(qop,"auth"))
     {
       logprintf (LOG_NOTQUIET, _("Unsupported quality of protection '%s'.\n"), qop);
-      xfree_null (qop); /* force freeing mem and return */
-      qop = NULL;
+      xfree (qop); /* force freeing mem and return */
     }
   else if (algorithm != NULL && strcmp (algorithm,"MD5") && strcmp (algorithm,"MD5-sess"))
     {
       logprintf (LOG_NOTQUIET, _("Unsupported algorithm '%s'.\n"), algorithm);
-      xfree_null (qop); /* force freeing mem and return */
-      qop = NULL;
+      xfree (qop); /* force freeing mem and return */
     }

   if (!realm || !nonce || !user || !passwd || !path || !method || !qop)
     {
-      xfree_null (realm);
-      xfree_null (opaque);
-      xfree_null (nonce);
-      xfree_null (qop);
-      xfree_null (algorithm);
+      xfree (realm);
+      xfree (opaque);
+      xfree (nonce);
+      xfree (qop);
+      xfree (algorithm);
       if (!qop)
         *auth_err = UNKNOWNATTR;
       else
@@ -4009,11 +3998,11 @@ digest_authentication_encode (const char *au, const char *user,
       }
   }

-  xfree_null (realm);
-  xfree_null (opaque);
-  xfree_null (nonce);
-  xfree_null (qop);
-  xfree_null (algorithm);
+  xfree (realm);
+  xfree (opaque);
+  xfree (nonce);
+  xfree (qop);
+  xfree (algorithm);

   return res;
 }
@@ -4108,7 +4097,7 @@ save_cookies (void)
 void
 http_cleanup (void)
 {
-  xfree_null (pconn.host);
+  xfree (pconn.host);
   if (wget_cookie_jar)
     cookie_jar_delete (wget_cookie_jar);
 }
diff --git a/src/init.c b/src/init.c
index 2bcf03a..088a6e9 100644
--- a/src/init.c
+++ b/src/init.c
@@ -527,7 +527,7 @@ wgetrc_user_file_name (void)
   home = home_dir ();
   if (home)
     file = aprintf ("%s/.wgetrc", home);
-  xfree_null (home);
+  xfree (home);
 #endif /* def __VMS [else] */

   if (!file)
@@ -562,8 +562,7 @@ wgetrc_file_name (void)
   if (!file)
     {
       char *home = home_dir ();
-      xfree_null (file);
-      file = NULL;
+      xfree (file);
       home = ws_mypath ();
       if (home)
         {
@@ -571,7 +570,6 @@ wgetrc_file_name (void)
           if (!file_exists_p (file))
             {
               xfree (file);
-              file = NULL;
             }
           xfree (home);
         }
@@ -645,8 +643,8 @@ run_wgetrc (const char *file)
         default:
           abort ();
         }
-      xfree_null (com);
-      xfree_null (val);
+      xfree (com);
+      xfree (val);
       ++ln;
     }
   xfree (line);
@@ -982,7 +980,7 @@ cmd_string (const char *com _GL_UNUSED, const char *val, void *place)
 {
   char **pstring = (char **)place;

-  xfree_null (*pstring);
+  xfree (*pstring);
   *pstring = xstrdup (val);
   return true;
 }
@@ -993,7 +991,7 @@ cmd_string_uppercase (const char *com _GL_UNUSED, const char *val, void *place)
 {
   char *q, **pstring;
   pstring = (char **)place;
-  xfree_null (*pstring);
+  xfree (*pstring);

   *pstring = xmalloc (strlen (val) + 1);

@@ -1013,7 +1011,7 @@ cmd_file (const char *com _GL_UNUSED, const char *val, void *place)
 {
   char **pstring = (char **)place;

-  xfree_null (*pstring);
+  xfree (*pstring);

   /* #### If VAL is empty, perhaps should set *PLACE to NULL.  */

@@ -1418,7 +1416,7 @@ cmd_spec_progress (const char *com, const char *val, void *place_ignored _GL_UNU
                exec_name, com, quote (val));
       return false;
     }
-  xfree_null (opt.progress_type);
+  xfree (opt.progress_type);

   /* Don't call set_progress_implementation here.  It will be called
      in main when it becomes clear what the log output is.  */
@@ -1569,7 +1567,7 @@ cmd_spec_useragent (const char *com, const char *val, void *place_ignored _GL_UN
                exec_name, com, quote (val));
       return false;
     }
-  xfree_null (opt.useragent);
+  xfree (opt.useragent);
   opt.useragent = xstrdup (val);
   return true;
 }
@@ -1769,11 +1767,11 @@ cleanup (void)
   log_cleanup ();
   netrc_cleanup ();

-  xfree_null (opt.choose_config);
-  xfree_null (opt.lfilename);
-  xfree_null (opt.dir_prefix);
-  xfree_null (opt.input_filename);
-  xfree_null (opt.output_document);
+  xfree (opt.choose_config);
+  xfree (opt.lfilename);
+  xfree (opt.dir_prefix);
+  xfree (opt.input_filename);
+  xfree (opt.output_document);
   free_vec (opt.accepts);
   free_vec (opt.rejects);
   free_vec ((char **)opt.excludes);
@@ -1781,39 +1779,39 @@ cleanup (void)
   free_vec (opt.domains);
   free_vec (opt.follow_tags);
   free_vec (opt.ignore_tags);
-  xfree_null (opt.progress_type);
-  xfree_null (opt.ftp_user);
-  xfree_null (opt.ftp_passwd);
-  xfree_null (opt.ftp_proxy);
-  xfree_null (opt.https_proxy);
-  xfree_null (opt.http_proxy);
+  xfree (opt.progress_type);
+  xfree (opt.ftp_user);
+  xfree (opt.ftp_passwd);
+  xfree (opt.ftp_proxy);
+  xfree (opt.https_proxy);
+  xfree (opt.http_proxy);
   free_vec (opt.no_proxy);
-  xfree_null (opt.useragent);
-  xfree_null (opt.referer);
-  xfree_null (opt.http_user);
-  xfree_null (opt.http_passwd);
+  xfree (opt.useragent);
+  xfree (opt.referer);
+  xfree (opt.http_user);
+  xfree (opt.http_passwd);
   free_vec (opt.user_headers);
   free_vec (opt.warc_user_headers);
 # ifdef HAVE_SSL
-  xfree_null (opt.cert_file);
-  xfree_null (opt.private_key);
-  xfree_null (opt.ca_directory);
-  xfree_null (opt.ca_cert);
-  xfree_null (opt.crl_file);
-  xfree_null (opt.random_file);
-  xfree_null (opt.egd_file);
+  xfree (opt.cert_file);
+  xfree (opt.private_key);
+  xfree (opt.ca_directory);
+  xfree (opt.ca_cert);
+  xfree (opt.crl_file);
+  xfree (opt.random_file);
+  xfree (opt.egd_file);
 # endif
-  xfree_null (opt.bind_address);
-  xfree_null (opt.cookies_input);
-  xfree_null (opt.cookies_output);
-  xfree_null (opt.user);
-  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);
+  xfree (opt.bind_address);
+  xfree (opt.cookies_input);
+  xfree (opt.cookies_output);
+  xfree (opt.user);
+  xfree (opt.passwd);
+  xfree (opt.base_href);
+  xfree (opt.method);
+  xfree (opt.post_file_name);
+  xfree (opt.post_data);
+  xfree (opt.body_data);
+  xfree (opt.body_file);

 #endif /* DEBUG_MALLOC */
 }
diff --git a/src/iri.c b/src/iri.c
index 75ba3e0..849b101 100644
--- a/src/iri.c
+++ b/src/iri.c
@@ -295,7 +295,7 @@ remote_to_utf8 (struct iri *iri, const char *str, const char **new)
   /* Test if something was converted */
   if (*new && !strcmp (str, *new))
     {
-      xfree ((char *) *new);
+      xfree (*new);
       return false;
     }

@@ -329,9 +329,9 @@ struct iri *iri_dup (const struct iri *src)
 void
 iri_free (struct iri *i)
 {
-  xfree_null (i->uri_encoding);
-  xfree_null (i->content_encoding);
-  xfree_null (i->orig_url);
+  xfree (i->uri_encoding);
+  xfree (i->content_encoding);
+  xfree (i->orig_url);
   xfree (i);
 }

diff --git a/src/log.c b/src/log.c
index 9404feb..d47c422 100644
--- a/src/log.c
+++ b/src/log.c
@@ -156,11 +156,7 @@ static void
 free_log_line (int num)
 {
   struct log_ln *ln = log_lines + num;
-  if (ln->malloced_line)
-    {
-      xfree (ln->malloced_line);
-      ln->malloced_line = NULL;
-    }
+  xfree (ln->malloced_line);
   ln->content = NULL;
 }

@@ -451,8 +447,7 @@ log_vprintf_internal (struct logvprintf_state *state, const char *fmt,
   FPUTS (write_ptr, fp);
   if (warcfp != NULL)
     FPUTS (write_ptr, warcfp);
-  if (state->bigmsg)
-    xfree (state->bigmsg);
+  xfree (state->bigmsg);

  flush:
   if (flush_log_p)
@@ -847,7 +842,7 @@ log_cleanup (void)
 {
   size_t i;
   for (i = 0; i < countof (ring); i++)
-    xfree_null (ring[i].buffer);
+    xfree (ring[i].buffer);
 }

 /* When SIGHUP or SIGUSR1 are received, the output is redirected
diff --git a/src/main.c b/src/main.c
index 8212382..99c2819 100644
--- a/src/main.c
+++ b/src/main.c
@@ -889,7 +889,7 @@ format_and_print_line (const char *prefix, const char *line,
         }
       if (printf ("%s ", token) < 0)
         {
-          xfree_null (line_dup);
+          xfree (line_dup);
           return -1;
         }
       remaining_chars -= strlen (token) + 1;  /* account for " " */
@@ -1481,13 +1481,11 @@ for details.\n\n"));
           {
             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;
           }
     }

@@ -1698,8 +1696,8 @@ outputting to a regular file.\n"));
               if (unlink (filename))
                 logprintf (LOG_NOTQUIET, "unlink: %s\n", strerror (errno));
             }
-          xfree_null (redirected_URL);
-          xfree_null (filename);
+          xfree (redirected_URL);
+          xfree (filename);
           url_free (url_parsed);
         }
       iri_free (iri);
diff --git a/src/mswindows.c b/src/mswindows.c
index bb57609..64f1dc4 100644
--- a/src/mswindows.c
+++ b/src/mswindows.c
@@ -367,8 +367,8 @@ static int old_percentage = -1;
 void
 ws_changetitle (const char *url)
 {
-  xfree_null (title_buf);
-  xfree_null (curr_url);
+  xfree (title_buf);
+  xfree (curr_url);
   title_buf = xmalloc (strlen (url) + 20);
   curr_url = xstrdup (url);
   old_percentage = -1;
diff --git a/src/netrc.c b/src/netrc.c
index dec9e2a..824fde3 100644
--- a/src/netrc.c
+++ b/src/netrc.c
@@ -163,7 +163,7 @@ search_netrc (const char *host, const char **acc, const char **passwd,

 /* Normally, these functions would be defined by your package.  */
 # define xmalloc malloc
-# define xfree free
+# define xfree(p) do { free ((void *) (p)); p = NULL; } while (0)
 # define xstrdup strdup

 # define xrealloc realloc
@@ -183,9 +183,9 @@ maybe_add_to_list (acc_t **newentry, acc_t **list)
   if (a && ! a->acc)
     {
       /* Free any allocated space.  */
-      xfree_null (a->host);
-      xfree_null (a->acc);
-      xfree_null (a->passwd);
+      xfree (a->host);
+      xfree (a->acc);
+      xfree (a->passwd);
     }
   else
     {
@@ -424,9 +424,9 @@ free_netrc(acc_t *l)
   while (l)
     {
       t = l->next;
-      xfree_null (l->acc);
-      xfree_null (l->passwd);
-      xfree_null (l->host);
+      xfree (l->acc);
+      xfree (l->passwd);
+      xfree (l->host);
       xfree (l);
       l = t;
     }
diff --git a/src/openssl.c b/src/openssl.c
index 1ad6bcc..38c6ac4 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -416,7 +416,7 @@ openssl_errstr (int fd _GL_UNUSED, void *arg)
     return NULL;

   /* Get rid of previous contents of ctx->last_error, if any.  */
-  xfree_null (ctx->last_error);
+  xfree (ctx->last_error);

   /* Iterate over OpenSSL's error stack and accumulate errors in the
      last_error buffer, separated by "; ".  This is better than using
@@ -460,7 +460,7 @@ openssl_close (int fd, void *arg)

   SSL_shutdown (conn);
   SSL_free (conn);
-  xfree_null (ctx->last_error);
+  xfree (ctx->last_error);
   xfree (ctx);

   close (fd);
diff --git a/src/recur.c b/src/recur.c
index 33d5a14..b6b9dc6 100644
--- a/src/recur.c
+++ b/src/recur.c
@@ -461,8 +461,8 @@ retrieve_tree (struct url *start_url_parsed, struct iri *pi)
         }

       xfree (url);
-      xfree_null (referer);
-      xfree_null (file);
+      xfree (referer);
+      xfree (file);
       iri_free (i);
     }

@@ -478,7 +478,7 @@ retrieve_tree (struct url *start_url_parsed, struct iri *pi)
       {
         iri_free (d6);
         xfree (d1);
-        xfree_null (d2);
+        xfree (d2);
       }
   }
   url_queue_delete (queue);
diff --git a/src/res.c b/src/res.c
index f5a8fd8..19890c6 100644
--- a/src/res.c
+++ b/src/res.c
@@ -404,7 +404,7 @@ free_specs (struct robot_specs *specs)
   int i;
   for (i = 0; i < specs->count; i++)
     xfree (specs->paths[i].path);
-  xfree_null (specs->paths);
+  xfree (specs->paths);
   xfree (specs);
 }

@@ -579,7 +579,6 @@ res_retrieve_file (const char *url, char **file, struct iri *iri)
          allocated the file name, deallocate is here so that the
          caller doesn't have to worry about it.  */
       xfree (*file);
-      *file = NULL;
     }
   return err == RETROK;
 }
diff --git a/src/retr.c b/src/retr.c
index e7bed93..1bcaae7 100644
--- a/src/retr.c
+++ b/src/retr.c
@@ -550,7 +550,7 @@ fd_read_hunk (int fd, hunk_terminator_t terminator, long sizehint, long maxsize)
       rdlen = fd_read (fd, hunk + tail, remain, 0);
       if (rdlen < 0)
         {
-          xfree_null (hunk);
+          xfree (hunk);
           return NULL;
         }
       tail += rdlen;
@@ -839,8 +839,7 @@ retrieve_url (struct url * orig_parsed, const char *origurl, char **file,

       assert (mynewloc != NULL);

-      if (local_file)
-        xfree (local_file);
+      xfree (local_file);

       /* The HTTP specs only allow absolute URLs to appear in
          redirects, but a ton of boneheaded webservers and CGIs out
@@ -854,8 +853,7 @@ retrieve_url (struct url * orig_parsed, const char *origurl, char **file,
          the content encoding. */
       iri->utf8_encode = opt.enable_iri;
       set_content_encoding (iri, NULL);
-      xfree_null (iri->orig_url);
-      iri->orig_url = NULL;
+      xfree (iri->orig_url);

       /* Now, see if this new location makes sense. */
       newloc_parsed = url_parse (mynewloc, &up_error_code, iri, true);
@@ -964,7 +962,7 @@ retrieve_url (struct url * orig_parsed, const char *origurl, char **file,
   if (file)
     *file = local_file ? local_file : NULL;
   else
-    xfree_null (local_file);
+    xfree (local_file);

   if (orig_parsed != u)
     {
@@ -1048,8 +1046,7 @@ retrieve_from_file (const char *file, bool html, int *count)

       /* Reset UTF-8 encode status */
       iri->utf8_encode = opt.enable_iri;
-      xfree_null (iri->orig_url);
-      iri->orig_url = NULL;
+      xfree (iri->orig_url);

       input_file = url_file;
     }
@@ -1059,7 +1056,7 @@ retrieve_from_file (const char *file, bool html, int *count)
   url_list = (html ? get_urls_html (input_file, NULL, NULL, iri)
               : get_urls_file (input_file));

-  xfree_null (url_file);
+  xfree (url_file);

   for (cur_url = url_list; cur_url; cur_url = cur_url->next, ++*count)
     {
@@ -1114,8 +1111,8 @@ Removing file due to --delete-after in retrieve_from_file():\n"));
           dt &= ~RETROKF;
         }

-      xfree_null (new_file);
-      xfree_null (filename);
+      xfree (new_file);
+      xfree (filename);
       iri_free (tmpiri);
     }

@@ -1191,7 +1188,7 @@ free_urlpos (struct urlpos *l)
       struct urlpos *next = l->next;
       if (l->url)
         url_free (l->url);
-      xfree_null (l->local_name);
+      xfree (l->local_name);
       xfree (l);
       l = next;
     }
diff --git a/src/url.c b/src/url.c
index f2ed395..bc91d4e 100644
--- a/src/url.c
+++ b/src/url.c
@@ -706,7 +706,7 @@ url_parse (const char *url, int *error, struct iri *iri, bool percent_encode)
         new_url = NULL;
       else
         {
-          xfree_null (iri->orig_url);
+          xfree (iri->orig_url);
           iri->orig_url = xstrdup (url);
           url_encoded = reencode_escapes (new_url);
           if (url_encoded != new_url)
@@ -917,7 +917,7 @@ url_parse (const char *url, int *error, struct iri *iri, bool percent_encode)
       u->url = url_string (u, URL_AUTH_SHOW);

       if (url_encoded != url)
-        xfree ((char *) url_encoded);
+        xfree (url_encoded);
     }
   else
     {
@@ -932,7 +932,7 @@ url_parse (const char *url, int *error, struct iri *iri, bool percent_encode)
  error:
   /* Cleanup in case of error: */
   if (url_encoded && url_encoded != url)
-    xfree ((char *) url_encoded);
+    xfree (url_encoded);

   /* Transmit the error code to the caller, if the caller wants to
      know.  */
@@ -1177,11 +1177,11 @@ url_free (struct url *url)
   xfree (url->path);
   xfree (url->url);

-  xfree_null (url->params);
-  xfree_null (url->query);
-  xfree_null (url->fragment);
-  xfree_null (url->user);
-  xfree_null (url->passwd);
+  xfree (url->params);
+  xfree (url->query);
+  xfree (url->fragment);
+  xfree (url->user);
+  xfree (url->passwd);

   xfree (url->dir);
   xfree (url->file);
diff --git a/src/utils.h b/src/utils.h
index 35deabb..c9512fd 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -47,12 +47,7 @@ as that of the covered work.  */

 #define alloca_array(type, size) ((type *) alloca ((size) * sizeof (type)))

-#define xfree free
-/* Free P if it is non-NULL.  C requires free() to behaves this way by
-   default, but Wget's code is historically careful not to pass NULL
-   to free.  This allows us to assert p!=NULL in xfree to check
-   additional errors.  (But we currently don't do that!)  */
-#define xfree_null(p) if (!(p)) ; else xfree (p)
+#define xfree(p) do { free ((void *) (p)); p = NULL; } while (0)

 struct hash_table;

diff --git a/src/warc.c b/src/warc.c
index 314e863..e2ad09b 100644
--- a/src/warc.c
+++ b/src/warc.c
@@ -939,9 +939,9 @@ warc_process_cdx_line (char *lineptr, int field_num_original_url,
     }
   else
     {
-      xfree_null(checksum);
-      xfree_null(original_url);
-      xfree_null(record_id);
+      xfree(checksum);
+      xfree(original_url);
+      xfree(record_id);
     }
 }

--
2.1.3

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

Reply via email to