Replace free() with mh_free0()
authorm@mmmmarascio.xyz <m@mmmmarascio.xyz>
Tue, 22 Mar 2016 03:28:44 +0000 (20:28 -0700)
committerPhilipp Takacs <philipp@bureaucracy.de>
Fri, 25 Mar 2016 22:26:18 +0000 (23:26 +0100)
mh_free0() free's the memmory and set's the pointer to NULL after
the free. This avoids doublefree and dereferenc of a non valid poiter.

52 files changed:
h/utils.h
sbr/addrsbr.c
sbr/context_del.c
sbr/context_read.c
sbr/context_replace.c
sbr/crawl_folders.c
sbr/encode_rfc2047.c
sbr/fmt_compile.c
sbr/fmt_new.c
sbr/fmt_rfc2047.c
sbr/folder_free.c
sbr/folder_read.c
sbr/folder_realloc.c
sbr/lock_file.c
sbr/m_draft.c
sbr/mf.c
sbr/readconfig.c
sbr/seq_read.c
sbr/seq_setprev.c
sbr/seq_setunseen.c
sbr/utils.c
uip/ali.c
uip/aliasbr.c
uip/anno.c
uip/ap.c
uip/burst.c
uip/distsbr.c
uip/folder.c
uip/forw.c
uip/inc.c
uip/mhbuild.c
uip/mhfree.c
uip/mhl.c
uip/mhlist.c
uip/mhlistsbr.c
uip/mhmisc.c
uip/mhparse.c
uip/mhshow.c
uip/mhshowsbr.c
uip/mhstore.c
uip/mhtest.c
uip/new.c
uip/pick.c
uip/rcvdist.c
uip/refile.c
uip/repl.c
uip/rmf.c
uip/scan.c
uip/slocal.c
uip/sortm.c
uip/spost.c
uip/whom.c

index 35d0833..60cc34c 100644 (file)
--- a/h/utils.h
+++ b/h/utils.h
@@ -4,6 +4,7 @@
 
 void *mh_xrealloc(void *, size_t);
 void *mh_xcalloc(size_t, size_t);
+void mh_free0(void *);
 char *pwd(void);
 char *add(char *, char *);
 void create_folder(char *, int, void (*)(int));
index dc3924b..b2e1176 100644 (file)
@@ -170,21 +170,21 @@ mnfree(struct mailname *mp)
                return;
 
        if (mp->m_text)
-               free(mp->m_text);
+               mh_free0(&(mp->m_text));
        if (mp->m_pers)
-               free(mp->m_pers);
+               mh_free0(&(mp->m_pers));
        if (mp->m_mbox)
-               free(mp->m_mbox);
+               mh_free0(&(mp->m_mbox));
        if (mp->m_host)
-               free(mp->m_host);
+               mh_free0(&(mp->m_host));
        if (mp->m_path)
-               free(mp->m_path);
+               mh_free0(&(mp->m_path));
        if (mp->m_gname)
-               free(mp->m_gname);
+               mh_free0(&(mp->m_gname));
        if (mp->m_note)
-               free(mp->m_note);
+               mh_free0(&(mp->m_note));
 
-       free((char *) mp);
+       mh_free0(&mp);
 }
 
 
@@ -436,7 +436,7 @@ error:
        while (first->m_next != NULL && first->m_next != next) {
                mp = first->m_next;
                first->m_next = mp->m_next;
-               free(mp);
+               mh_free0(&mp);
        }
        first->m_next = next;
        return -1;
index 7d068b5..9661050 100644 (file)
@@ -7,6 +7,7 @@
 */
 
 #include <h/mh.h>
+#include <h/utils.h>
 
 /*
 ** Delete a key/value pair from the context/profile list.
@@ -26,10 +27,10 @@ context_del(char *key)
                                pp->n_next = np->n_next;
                        else
                                m_defs = np->n_next;
-                       free(np->n_name);
+                       mh_free0(&(np->n_name));
                        if (np->n_field)
-                               free(np->n_field);
-                       free((char *) np);
+                               mh_free0(&(np->n_field));
+                       mh_free0(&np);
                        ctxflags |= CTXMOD;
                        return 0;
                }
index 5b7c777..4475ca0 100644 (file)
@@ -25,6 +25,7 @@
 */
 
 #include <h/mh.h>    /* mh internals */
+#include <h/utils.h> /* mh_free0() */
 #include <errno.h>   /* system call errors */
 #include <pwd.h>     /* structure for getpwuid() results */
 #include <unistd.h>
@@ -126,7 +127,7 @@ context_read(void)
                if (!getanswer(cp)) {
                        adios(EX_NOPERM, NULL, "Unable to access the mail storage directory `%s'", nd);
                }
-               free(cp);
+               mh_free0(&cp);
                if (!makedir(nd)) {
                        adios(EX_CANTCREAT, nd, "unable to create");
                }
index 0a31bba..1b39b27 100644 (file)
@@ -40,7 +40,7 @@ context_replace(char *key, char *value)
                                if (!np->n_context)
                                        admonish(NULL, "bug: context_replace(key=\"%s\",value=\"%s\")", key, value);
                                if (np->n_field)
-                                       free(np->n_field);
+                                       mh_free0(&(np->n_field));
                                np->n_field = getcpy(value);
                                ctxflags |= CTXMOD;
                        }
index 2e1a094..3841b97 100644 (file)
@@ -102,12 +102,12 @@ add_children(char *name, struct crawl_context *crawl)
                        /* add_folder saves child in the list, don't free it */
                        add_folder(child, crawl);
                } else {
-                       free(child);
+                       mh_free0(&child);
                }
        }
 
        closedir(dd);
-       free(prefix);
+       mh_free0(&prefix);
 }
 
 static void
@@ -153,6 +153,6 @@ crawl_folders(char *dir, crawl_callback_t *callback, void *baton)
        ** Note that we "leak" the folder names, on the assumption that the
        ** caller is using them.
        */
-       free(crawl->folders);
-       free(crawl);
+       mh_free0(&(crawl->folders));
+       mh_free0(&crawl);
 }
index 844f0c4..4e4cbef 100644 (file)
@@ -299,7 +299,7 @@ field_encode_quoted(const char *name, char **value, const char *charset,
        }
        *q = '\0';
 
-       free(*value);
+       mh_free0(value);
        *value = output;
 
        return 0;
@@ -367,7 +367,7 @@ unfold_header(char **value, int len)
        }
        *p = '\0';
 
-       free(*value);
+       mh_free0(value);
        *value = str;
 }
 
@@ -574,8 +574,7 @@ do_reformat:
 
                output = add(cp, output);
                column += len;
-               free(cp);
-               cp = NULL;
+               mh_free0(&cp);
        }
 
        /*
@@ -588,17 +587,17 @@ do_reformat:
 
        output = add("\n", output);
 
-       free(*value);
+       mh_free0(value);
        *value = output;
        output = NULL;
 
 out:
 
        if (tmpbuf) {
-               free(tmpbuf);
+               mh_free0(&tmpbuf);
        }
        if (output) {
-               free(output);
+               mh_free0(&output);
        }
 
        return errflag > 0;
index 85ed7d7..f4d92d2 100644 (file)
@@ -308,7 +308,7 @@ fmt_compile(char *fstring, struct format **fmt)
        size_t i;
 
        if (format_string)
-               free(format_string);
+               mh_free0(&format_string);
        format_string = getcpy(fstring);
        usr_fstring = fstring;
 
index 874129a..6dcc70b 100644 (file)
@@ -30,7 +30,7 @@ new_fs(char *form, char *def_form)
        FILE *fp;
 
        if (formats) {
-               free(formats);
+               mh_free0(&formats);
        }
 
        if (form) {
index 0932e34..4145139 100644 (file)
@@ -337,7 +337,7 @@ decode_rfc2047(char *str, char *dst, size_t dstlen)
                                if (!dstlen || !savedstlen)
                                        goto buffull;
                                dstlen = savedstlen;
-                               free(convbuf);
+                               mh_free0(&convbuf);
                        }
 #endif
 
index 2930254..8da713e 100644 (file)
@@ -7,7 +7,7 @@
 */
 
 #include <h/mh.h>
-
+#include <h/utils.h>
 
 void
 folder_free(struct msgs *mp)
@@ -18,12 +18,12 @@ folder_free(struct msgs *mp)
                return;
 
        if (mp->foldpath)
-               free(mp->foldpath);
+               mh_free0(&(mp->foldpath));
 
        /* free the sequence names */
        for (i = 0; mp->msgattrs[i]; i++)
-               free(mp->msgattrs[i]);
+               mh_free0(&(mp->msgattrs[i]));
 
-       free(mp->msgstats);  /* free message status area */
-       free(mp);  /* free main folder structure */
+       mh_free0(&(mp->msgstats));  /* free message status area */
+       mh_free0(&mp);  /* free main folder structure */
 }
index 1c68d0d..1eeee00 100644 (file)
@@ -35,12 +35,12 @@ folder_read(char *name)
 
        name = getcpy(toabsdir(name));
        if (!(dd = opendir(name))) {
-               free(name);
+               mh_free0(&name);
                return NULL;
        }
 
        if (stat(name, &st) == -1) {
-               free(name);
+               mh_free0(&name);
                closedir(dd);
                return NULL;
        }
@@ -146,7 +146,7 @@ folder_read(char *name)
        for (msgnum = 0; msgnum < mp->nummsg; msgnum++)
                set_exists(mp, mi[msgnum]);
 
-       free(mi);  /* We don't need this anymore */
+       mh_free0(&mi);  /* We don't need this anymore */
 
        /*
        ** Read and initialize the sequence information.
index bfdc44f..64a6640 100644 (file)
@@ -62,7 +62,7 @@ folder_realloc(struct msgs *mp, int lo, int hi)
                                tmpstats[msgnum - lo] = mp->msgstats[msgnum - mp->lowoff];
                        }
                }
-               free(mp->msgstats);
+               mh_free0(&(mp->msgstats));
                mp->msgstats = tmpstats;
        }
 
index 07302e5..957bc4e 100644 (file)
@@ -557,8 +557,8 @@ timerOFF(int fd)
                        else
                                pp->l_next = lp->l_next;
 
-                       free(lp->l_lock);
-                       free(lp);
+                       mh_free0(&(lp->l_lock));
+                       mh_free0(&lp);
                }
        }
 
index 549e89b..b8d385f 100644 (file)
@@ -28,7 +28,7 @@ m_draft(char *which)
        if (!(mp = folder_read(folder))) {
                adios(EX_IOERR, NULL, "unable to read folder %s", folder);
        }
-       free(folder);
+       mh_free0(&folder);
 
        /*
        ** Make sure we have enough message status space for all
index 7c6f004..c86d4d5 100644 (file)
--- a/sbr/mf.c
+++ b/sbr/mf.c
@@ -168,33 +168,31 @@ getadrx(char *addrs)
        struct adrx *adrxp = &adrxs2;
 
        if (pers)
-               free(pers);
+               mh_free0(&pers);
        if (mbox)
-               free(mbox);
+               mh_free0(&mbox);
        if (host)
-               free(host);
+               mh_free0(&host);
        if (path)
-               free(path);
+               mh_free0(&path);
        if (grp)
-               free(grp);
+               mh_free0(&grp);
        if (note)
-               free(note);
-       pers = mbox = host = path = grp = note = NULL;
+               mh_free0(&note);
        err[0] = 0;
 
        if (dp == NULL) {
                dp = cp = getcpy(addrs ? addrs : "");
                glevel = 0;
        } else if (cp == NULL) {
-               free(dp);
-               dp = NULL;
+               mh_free0(&dp);
                return NULL;
        }
 
        switch (parse_address()) {
        case DONE:
-               free(dp);
-               dp = cp = NULL;
+               mh_free0(&dp);
+               cp = NULL;
                return NULL;
 
        case OK:
@@ -272,8 +270,7 @@ again: ;
                }
        case LX_COMA:
                if (note) {
-                       free(note);
-                       note = NULL;
+                       mh_free0(&note);
                }
                goto again;
 
@@ -501,8 +498,7 @@ domain(char *buffer)
 
                case LX_AT:  /* sigh (0) */
                        mbox = add(host, add("%", mbox));
-                       free(host);
-                       host = NULL;
+                       mh_free0(&host);
                        continue;
 
                default:
index c99b7cd..ec97e1e 100644 (file)
@@ -64,7 +64,7 @@ readconfig(struct node **npp, FILE *ib, char *file, int ctx)
                                        cp = add(field, cp);
                                }
                                np->n_field = trimcpy(cp);
-                               free(cp);
+                               mh_free0(&cp);
                        } else {
                                np->n_field = trimcpy(field);
                        }
index b932cad..1ceb421 100644 (file)
@@ -87,7 +87,7 @@ seq_public(struct msgs *mp)
                                        cp = add(field, cp);
                                }
                                seq_init(mp, getcpy(name), trimcpy(cp));
-                               free(cp);
+                               mh_free0(&cp);
                        } else {
                                seq_init(mp, getcpy(name), trimcpy(field));
                        }
@@ -181,8 +181,8 @@ seq_init(struct msgs *mp, char *name, char *field)
 
        /* Return error, if too many sequences */
        if (i >= NUMATTRS) {
-               free(name);
-               free(field);
+               mh_free0(&name);
+               mh_free0(&field);
                return -1;
        }
 
@@ -191,7 +191,7 @@ seq_init(struct msgs *mp, char *name, char *field)
        ** name string.  Else add it to the list of sequence names.
        */
        if (mp->msgattrs[i]) {
-               free(name);
+               mh_free0(&name);
        } else {
                mp->msgattrs[i] = name;
                mp->msgattrs[i + 1] = NULL;
@@ -228,6 +228,6 @@ seq_init(struct msgs *mp, char *name, char *field)
                }
        }
 
-       free(field);  /* free string containing message ranges */
+       mh_free0(&field);  /* free string containing message ranges */
        return i;
 }
index c66c132..aecbea6 100644 (file)
@@ -7,6 +7,7 @@
 */
 
 #include <h/mh.h>
+#include <h/utils.h>
 
 /*
 ** Add all the messages currently SELECTED to
@@ -28,7 +29,7 @@ seq_setprev(struct msgs *mp)
        if ((cp = context_find(psequence))) {
                dp = getcpy(cp);
                if (!(ap = brkstring(dp, " ", "\n")) || !*ap) {
-                       free(dp);
+                       mh_free0(&dp);
                        return;
                }
        } else {
@@ -39,5 +40,5 @@ seq_setprev(struct msgs *mp)
        for (; *ap; ap++)
                seq_addsel(mp, *ap, -1, 1);
 
-       free(dp);
+       mh_free0(&dp);
 }
index e628393..73bd83d 100644 (file)
@@ -8,6 +8,7 @@
 */
 
 #include <h/mh.h>
+#include <h/utils.h> /* mh_free0() */
 
 /*
 ** We scan through the folder and act upon all messages
@@ -34,7 +35,7 @@ seq_setunseen(struct msgs *mp, int doadd)
        }
        if (!(ap = brkstring(dp, " ", "\n")) || !*ap) {
                /* contains no sequence name, i.e. we're finished */
-               free(dp);
+               mh_free0(&dp);
                return;
        }
 
@@ -61,5 +62,5 @@ seq_setunseen(struct msgs *mp, int doadd)
                }
        }
 
-       free(dp);
+       mh_free0(&dp);
 }
index 2744581..f61456d 100644 (file)
@@ -65,6 +65,18 @@ mh_xcalloc(size_t nmemb, size_t size)
 }
 
 /*
+** Free a pointer and set it to NULL.
+*/
+void
+mh_free0(void * ptr)
+{
+       void ** p;
+       p = ptr;
+       free(*p);
+       *p = NULL;
+}
+
+/*
 ** Return the present working directory, if the current directory does not
 ** exist, or is too long, make / the pwd.
 */
@@ -119,7 +131,7 @@ add(char *s2, char *s1)
        /* Copy s1 and free it */
        if (s1) {
                memcpy(cp, s1, len1);
-               free(s1);
+               mh_free0(&s1);
        }
 
        /* Copy s2 */
@@ -155,7 +167,7 @@ create_folder(char *folder, int autocreate, void (*done_callback)(int))
                        if (!getanswer(cp)) {
                                done_callback(EX_CANTCREAT);
                        }
-                       free(cp);
+                       mh_free0(&cp);
                } else if (autocreate == -1) {
                        /* do not create, so exit */
                        done_callback(EX_CANTCREAT);
index dc2a341..d6a480c 100644 (file)
--- a/uip/ali.c
+++ b/uip/ali.c
@@ -133,7 +133,7 @@ main(int argc, char **argv)
                        }
                }
                if (dp) {
-                       free(dp);
+                       mh_free0(&dp);
                }
        }
 
@@ -244,5 +244,5 @@ print_usr(char *s, int list, int norm)
        print_aka(vp ? vp : s, list, 0);
 
        if (vp)
-               free(vp);
+               mh_free0(&vp);
 }
index 1fbcc7a..1ed527f 100644 (file)
@@ -81,8 +81,8 @@ akresult(struct aka *ak)
                if (cp) {
                        dp = cp;
                        cp = concat(cp, ",", pp, NULL);
-                       free(dp);
-                       free(pp);
+                       mh_free0(&dp);
+                       mh_free0(&pp);
                } else
                        cp = pp;
        }
index 66e62c5..81ad042 100644 (file)
@@ -342,7 +342,7 @@ annolist(char *file, unsigned char *comp, int number)
 
        } while (*field && *field != '-');
 
-       free(field);
+       mh_free0(&field);
        fclose(fp);
 
        return;
@@ -542,7 +542,7 @@ dodel(int fd, unsigned char *comp, char *text, FILE *tmp, int number)
 
        } while (*field && *field != '-');
 
-       free(field);
+       mh_free0(&field);
 
        fflush(tmp);
        fflush(fp); /* The underlying fd will be closed by lkclose() */
index 5b9677f..041abf1 100644 (file)
--- a/uip/ap.c
+++ b/uip/ap.c
@@ -161,11 +161,11 @@ process(char *arg, int norm)
                fmt_scan(fmt, buffer, BUFSIZ, dat);
                fputs(buffer, stdout);
 
-               free(p->pq_text);
+               mh_free0(&(p->pq_text));
                if (p->pq_error)
-                       free(p->pq_error);
+                       mh_free0(&(p->pq_error));
                q = p->pq_next;
-               free((char *) p);
+               mh_free0(&p);
        }
 
        return status;
index 119a936..eb2fb7a 100644 (file)
@@ -136,7 +136,7 @@ main(int argc, char **argv)
                }
        }
 
-       free(smsgs);
+       mh_free0(&smsgs);
        context_replace(curfolder, folder);
 
        /*
index d1b9474..a600b77 100644 (file)
@@ -118,7 +118,7 @@ process: ;
                }
                return NOTOK;
        }
-       free(resent);
+       mh_free0(&resent);
 
        if (txtfd != NOTOK) {
                lseek(txtfd, (off_t) 0, SEEK_SET);  /* msgnam not accurate */
index b4758cf..c8b3c60 100644 (file)
@@ -263,7 +263,7 @@ main(int argc, char **argv)
                        }
                        for (cp = getcpy(getcurfol()); *ap; ap++)
                                cp = add(*ap, add(" ", cp));
-                       free(dp);
+                       mh_free0(&dp);
                        context_replace(stack, cp);  /* update folder stack */
                } else {
                        /* update folder stack */
@@ -294,7 +294,7 @@ main(int argc, char **argv)
                        /* delete folder stack entry from context */
                        context_del(stack);
                }
-               free(dp);
+               mh_free0(&dp);
        }
        if (pushsw || popsw) {
                cp = toabsdir(argfolder);
@@ -313,7 +313,7 @@ main(int argc, char **argv)
                        dp = getcpy(cp);
                        for (ap = brkstring(dp, " ", "\n"); *ap; ap++)
                                printf(" %s", *ap);
-                       free(dp);
+                       mh_free0(&dp);
                }
                printf("\n");
 
index 243c918..5b577bd 100644 (file)
@@ -336,7 +336,7 @@ build_form(char *form, char *digest, int volume, int issue)
        line = mh_xcalloc(fmtsize, sizeof(char));
        fmt_scan(fmt, line, fmtsize, dat);
        fputs(line, tmp);
-       free(line);
+       mh_free0(&line);
        if (fclose(tmp))
                adios(EX_IOERR, tmpfil, "error writing");
 
index 368bce3..cbbec1b 100644 (file)
--- a/uip/inc.c
+++ b/uip/inc.c
@@ -431,7 +431,7 @@ main(int argc, char **argv)
                */
                break;
        }
-       free(maildir_copy);
+       mh_free0(&maildir_copy);
 
        if (incerr < 0) {  /* error */
                if (locked) {
index ca7c5bf..4ff06ef 100644 (file)
@@ -807,7 +807,7 @@ use_forw:
                                if (!*cp)
                                        adios(EX_DATAERR, NULL, "empty pipe command for #%s directive", ci->ci_type);
                                cp = getcpy(cp);
-                               free(ci->ci_magic);
+                               mh_free0(&(ci->ci_magic));
                                ci->ci_magic = cp;
                        } else {
                                /* record filename of decoded contents */
@@ -885,7 +885,7 @@ use_forw:
                                if (!m_convert(mp, cp))
                                        exit(EX_USAGE);
                }
-               free(folder);
+               mh_free0(&folder);
                free_ctinfo(ct);
 
                /*
index c873066..5378c35 100644 (file)
@@ -8,6 +8,7 @@
 */
 
 #include <h/mh.h>
+#include <h/utils.h>
 #include <errno.h>
 #include <h/mime.h>
 #include <h/mhparse.h>
@@ -49,13 +50,13 @@ free_content(CT ct)
        free_header(ct);
 
        if (ct->c_partno)
-               free(ct->c_partno);
+               mh_free0(&(ct->c_partno));
 
        if (ct->c_vrsn)
-               free(ct->c_vrsn);
+               mh_free0(&(ct->c_vrsn));
 
        if (ct->c_ctline)
-               free(ct->c_ctline);
+               mh_free0(&(ct->c_ctline));
 
        free_ctinfo(ct);
 
@@ -80,39 +81,39 @@ free_content(CT ct)
        }
 
        if (ct->c_charset)
-               free(ct->c_charset);
+               mh_free0(&(ct->c_charset));
        if (ct->c_showproc)
-               free(ct->c_showproc);
+               mh_free0(&(ct->c_showproc));
        if (ct->c_storeproc)
-               free(ct->c_storeproc);
+               mh_free0(&(ct->c_storeproc));
 
        if (ct->c_celine)
-               free(ct->c_celine);
+               mh_free0(&(ct->c_celine));
 
        /* free structures for content encodings */
        free_encoding(ct, 1);
 
        if (ct->c_id)
-               free(ct->c_id);
+               mh_free0(&(ct->c_id));
        if (ct->c_descr)
-               free(ct->c_descr);
+               mh_free0(&(ct->c_descr));
        if (ct->c_dispo)
-               free(ct->c_dispo);
+               mh_free0(&(ct->c_dispo));
 
        if (ct->c_file) {
                if (ct->c_unlink)
                        unlink(ct->c_file);
-               free(ct->c_file);
+               mh_free0(&(ct->c_file));
        }
        if (ct->c_fp)
                fclose(ct->c_fp);
 
        if (ct->c_storage)
-               free(ct->c_storage);
+               mh_free0(&(ct->c_storage));
        if (ct->c_folder)
-               free(ct->c_folder);
+               mh_free0(&(ct->c_folder));
 
-       free(ct);
+       mh_free0(&ct);
 }
 
 
@@ -130,9 +131,9 @@ free_header(CT ct)
        while (hp1) {
                hp2 = hp1->next;
 
-               free(hp1->name);
-               free(hp1->value);
-               free(hp1);
+               mh_free0(&(hp1->name));
+               mh_free0(&(hp1->value));
+               mh_free0(&hp1);
 
                hp1 = hp2;
        }
@@ -150,24 +151,19 @@ free_ctinfo(CT ct)
 
        ci = &ct->c_ctinfo;
        if (ci->ci_type) {
-               free(ci->ci_type);
-               ci->ci_type = NULL;
+               mh_free0(&(ci->ci_type));
        }
        if (ci->ci_subtype) {
-               free(ci->ci_subtype);
-               ci->ci_subtype = NULL;
+               mh_free0(&(ci->ci_subtype));
        }
        for (ap = ci->ci_attrs; *ap; ap++) {
-               free(*ap);
-               *ap = NULL;
+               mh_free0(ap);
        }
        if (ci->ci_comment) {
-               free(ci->ci_comment);
-               ci->ci_comment = NULL;
+               mh_free0(&(ci->ci_comment));
        }
        if (ci->ci_magic) {
-               free(ci->ci_magic);
-               ci->ci_magic = NULL;
+               mh_free0(&(ci->ci_magic));
        }
 }
 
@@ -180,8 +176,7 @@ free_text(CT ct)
        if (!(t = (struct text *) ct->c_ctparams))
                return;
 
-       free((char *) t);
-       ct->c_ctparams = NULL;
+       mh_free0(&t);
 }
 
 
@@ -195,19 +190,17 @@ free_multi(CT ct)
                return;
 
        if (m->mp_start)
-               free(m->mp_start);
+               mh_free0(&(m->mp_start));
        if (m->mp_stop)
-               free(m->mp_stop);
+               mh_free0(&(m->mp_stop));
 
        for (part = m->mp_parts; part; part = next) {
                next = part->mp_next;
                free_content(part->mp_part);
-               free((char *) part);
+               mh_free0(&part);
        }
-       m->mp_parts = NULL;
 
-       free((char *) m);
-       ct->c_ctparams = NULL;
+       mh_free0(&m);
 }
 
 
@@ -220,10 +213,9 @@ free_partial(CT ct)
                return;
 
        if (p->pm_partid)
-               free(p->pm_partid);
+               mh_free0(&(p->pm_partid));
 
-       free((char *) p);
-       ct->c_ctparams = NULL;
+       mh_free0(&p);
 }
 
 
@@ -248,13 +240,11 @@ free_encoding(CT ct, int toplevel)
        if (ce->ce_file) {
                if (ce->ce_unlink)
                        unlink(ce->ce_file);
-               free(ce->ce_file);
-               ce->ce_file = NULL;
+               mh_free0(&(ce->ce_file));
        }
 
        if (toplevel) {
-               free((char *) ce);
-               ct->c_cefile = NULL;
+               mh_free0(&ce);
        } else {
                ct->c_ceopenfnx = NULL;
        }
index 6f87d62..ddf32ec 100644 (file)
--- a/uip/mhl.c
+++ b/uip/mhl.c
@@ -458,7 +458,7 @@ evalvar(struct mcomp *c1)
                        return 1;
                cp = concat("=", cp, NULL);
                fmtstr = new_fs(cp, NULL);
-               free(cp);
+               mh_free0(&cp);
                c1->c_fstr = getcpy(fmtstr);
                c1->c_flags |= FORMAT;
                return 0;
@@ -586,8 +586,7 @@ process(char *fname, int ofilen, int ofilec)
        if (fp != stdin)
                fclose(fp);
        if (holder.c_text) {
-               free(holder.c_text);
-               holder.c_text = NULL;
+               mh_free0(&(holder.c_text));
        }
        free_queue(&msghd, &msgtl);
        for (c1 = fmthd; c1; c1 = c1->c_next)
@@ -663,8 +662,7 @@ mhlfile(FILE *fp, char *mname, int ofilen, int ofilec)
                                        holder.c_text = concat("(Message ",
                                                        mname, ")\n", NULL);
                                        putcomp(c1, &holder, ONECOMP);
-                                       free(holder.c_text);
-                                       holder.c_text = NULL;
+                                       mh_free0(&(holder.c_text));
                                        continue;
                                }
                                if (!mh_strcasecmp(c1->c_name, "extras")) {
@@ -680,8 +678,7 @@ mhlfile(FILE *fp, char *mname, int ofilen, int ofilec)
                                                putcomp(c1, &holder, BODYCOMP);
                                                state = m_getfld(state, name, holder.c_text, sizeof(buf), fp);
                                        }
-                                       free(holder.c_text);
-                                       holder.c_text = NULL;
+                                       mh_free0(&(holder.c_text));
                                        continue;
                                }
                                for (c2 = msghd; c2; c2 = c2->c_next)
@@ -774,7 +771,7 @@ mcomp_format(struct mcomp *c1, struct mcomp *c2)
                /* Don't need to append a newline, dctime() already did */
                c2->c_text = getcpy(buffer);
 
-               free(ap);
+               mh_free0(&ap);
                return;
        }
 
@@ -809,11 +806,11 @@ mcomp_format(struct mcomp *c1, struct mcomp *c2)
                        c2->c_text = add(buffer, c2->c_text);
                }
 
-               free(p->pq_text);
+               mh_free0(&(p->pq_text));
                if (p->pq_error)
-                       free(p->pq_error);
+                       mh_free0(&(p->pq_error));
                q = p->pq_next;
-               free((char *) p);
+               mh_free0(&p);
        }
 
        c2->c_text = add("\n", c2->c_text);
@@ -860,16 +857,16 @@ free_queue(struct mcomp **head, struct mcomp **tail)
        for (c1 = *head; c1; c1 = c2) {
                c2 = c1->c_next;
                if (c1->c_name)
-                       free(c1->c_name);
+                       mh_free0(&(c1->c_name));
                if (c1->c_text)
-                       free(c1->c_text);
+                       mh_free0(&(c1->c_text));
                if (c1->c_ovtxt)
-                       free(c1->c_ovtxt);
+                       mh_free0(&(c1->c_ovtxt));
                if (c1->c_fstr)
-                       free(c1->c_fstr);
+                       mh_free0(&(c1->c_fstr));
                if (c1->c_fmt)
-                       free((char *) c1->c_fmt);
-               free((char *) c1);
+                       mh_free0(&(c1->c_fmt));
+               mh_free0(&c1);
        }
 
        *head = *tail = NULL;
index 4609012..72dc9ed 100644 (file)
@@ -276,8 +276,7 @@ main(int argc, char **argv)
        for (ctp = cts; *ctp; ctp++)
                free_content(*ctp);
 
-       free((char *) cts);
-       cts = NULL;
+       mh_free0(&cts);
 
        /* If reading from a folder, do some updating */
        if (mp) {
index 2a4f504..46b0d4c 100644 (file)
@@ -176,9 +176,9 @@ list_content(CT ct, int toplevel, int verbose, int debug)
                char *dp;
 
                dp = trimcpy(cp = getcpy(ct->c_descr));
-               free(cp);
+               mh_free0(&cp);
                printf(LSTFMT2d1, dp);
-               free(dp);
+               mh_free0(&dp);
        }
 
        printf("\n");
@@ -201,7 +201,7 @@ list_content(CT ct, int toplevel, int verbose, int debug)
                        dp = trimcpy(cp = add(ci->ci_comment, NULL));
                        free (cp);
                        snprintf(buffer, sizeof(buffer), "(%s)", dp);
-                       free(dp);
+                       mh_free0(&dp);
                        printf(LSTFMT2d2, buffer);
                }
        }
index 0f0a8d6..eb29e83 100644 (file)
@@ -121,7 +121,7 @@ losing_directory:
 
                        ep = concat("Create directory \"", file, "\"? ", NULL);
                        answer = getanswer(ep);
-                       free(ep);
+                       mh_free0(&ep);
 
                        if (!answer)
                                goto losing_directory;
@@ -237,7 +237,6 @@ flush_errors(void)
        if (errs) {
                fflush(stdout);
                fprintf(stderr, "%s", errs);
-               free(errs);
-               errs = NULL;
+               mh_free0(&errs);
        }
 }
index d3f543f..f04a348 100644 (file)
@@ -536,18 +536,18 @@ incl_name_value(unsigned char *buf, char *name, char *value) {
                                *cp = '\0';
                                newbuf = concat(prefix, insertion, suffix,
                                                "\n", NULL);
-                               free(suffix);
+                               mh_free0(&suffix);
                        } else {
                                /* Append to end. */
                                newbuf = concat(buf, insertion, "\n", NULL);
                        }
 
-                       free(prefix);
-                       free(insertion);
-                       free(buf);
+                       mh_free0(&prefix);
+                       mh_free0(&insertion);
+                       mh_free0(&buf);
                }
 
-               free(name_plus_equal);
+               mh_free0(&name_plus_equal);
        }
 
        return newbuf;
@@ -565,7 +565,7 @@ extract_name_value(char *name_suffix, char *value) {
        char *name_suffix_equals = strstr(value, name_suffix_plus_quote);
        char *cp;
 
-       free(name_suffix_plus_quote);
+       mh_free0(&name_suffix_plus_quote);
        if (name_suffix_equals) {
                char *name_suffix_begin;
 
@@ -777,8 +777,7 @@ bad_quote:
        */
        if (magic && *cp == '<') {
                if (ct->c_id) {
-                       free(ct->c_id);
-                       ct->c_id = NULL;
+                       mh_free0(&(ct->c_id));
                }
                if (!(dp = strchr(ct->c_id = ++cp, '>'))) {
                        advise(NULL, "invalid ID in message %s", ct->c_file);
@@ -925,7 +924,7 @@ invalid:
        if (istype) {
                if ((dp = ci->ci_comment)) {
                        ci->ci_comment = concat(dp, " ", buffer, NULL);
-                       free(dp);
+                       mh_free0(&dp);
                } else {
                        ci->ci_comment = getcpy(buffer);
                }
@@ -1143,7 +1142,7 @@ end_part:
                                continue;
                        *next = NULL;
                        free_content(p);
-                       free((char *) part);
+                       mh_free0(&part);
                }
        }
 
@@ -1232,7 +1231,7 @@ reverse_parts(CT ct)
        *next = NULL;
 
        /* free array of pointers */
-       free((char *) base);
+       mh_free0(&base);
 }
 
 
@@ -1541,7 +1540,7 @@ openBase64(CT ct, char **file)
                                adios(EX_IOERR, ce->ce_file, "unable to rename %s to ",
                                                file_org);
                        }
-                       free(file_org);
+                       mh_free0(&file_org);
 
                } else {
                        ce->ce_file = add(cp, ce->ce_file);
@@ -1752,7 +1751,7 @@ openQuoted(CT ct, char **file)
                                adios(EX_IOERR, ce->ce_file, "unable to rename %s to ",
                                                file_org);
                        }
-                       free(file_org);
+                       mh_free0(&file_org);
 
                } else {
                        ce->ce_file = add(cp, ce->ce_file);
@@ -1969,7 +1968,7 @@ open7Bit(CT ct, char **file)
                                adios(EX_IOERR, ce->ce_file, "unable to rename %s to ",
                                                file_org);
                        }
-                       free(file_org);
+                       mh_free0(&file_org);
 
                } else {
                        ce->ce_file = add(cp, ce->ce_file);
index 691ef8c..dc5c202 100644 (file)
@@ -174,7 +174,7 @@ main(int argc, char **argv)
                                        adios(EX_USAGE, NULL, "missing argument to %s",
                                                        argp[-2]);
                                if (formsw)
-                                       free(formsw);
+                                       mh_free0(&formsw);
                                formsw = getcpy(etcpath(cp));
                                continue;
 
@@ -359,8 +359,7 @@ main(int argc, char **argv)
        for (ctp = cts; *ctp; ctp++)
                free_content(*ctp);
 
-       free((char *) cts);
-       cts = NULL;
+       mh_free0(&cts);
 
        /* If reading from a folder, do some updating */
        if (mp) {
index aeaf0e8..6298869 100644 (file)
@@ -332,7 +332,7 @@ show_content_aux(CT ct, int alternate, char *cp, char *cracked)
 
                                        s = trimcpy(ct->c_descr);
                                        strncpy(bp, s, buflen);
-                                       free(s);
+                                       mh_free0(&s);
                                }
                                break;
 
@@ -733,7 +733,7 @@ show_multi_aux(CT ct, int alternate, char *cp)
 
                                        s = trimcpy(ct->c_descr);
                                        strncpy(bp, s, buflen);
-                                       free(s);
+                                       mh_free0(&s);
                                }
                                break;
 
index 519991f..4f84570 100644 (file)
@@ -338,8 +338,7 @@ main(int argc, char **argv)
        for (ctp = cts; *ctp; ctp++)
                free_content(*ctp);
 
-       free((char *) cts);
-       cts = NULL;
+       mh_free0(&cts);
 
        /* If reading from a folder, do some updating */
        if (mp) {
@@ -613,7 +612,7 @@ missing_part:
        ct = *ctq++;
        if (store_content(ct, NULL) == NOTOK) {
 losing:
-               free((char *) base);
+               mh_free0(&base);
                return NOTOK;
        }
 
@@ -623,7 +622,7 @@ losing:
                        goto losing;
        }
 
-       free((char *) base);
+       mh_free0(&base);
        return OK;
 }
 
@@ -788,7 +787,7 @@ store_content(CT ct, CT p)
                ct->c_folder = getcpy(folder);
 
                if (cp[1])
-                       free(folder);
+                       mh_free0(&folder);
 
                goto got_filename;
        }
index 5b9f669..0fa9741 100644 (file)
@@ -286,8 +286,7 @@ main(int argc, char **argv)
        for (ctp = cts; *ctp; ctp++)
                free_content(*ctp);
 
-       free((char *) cts);
-       cts = NULL;
+       mh_free0(&cts);
 
        /* If reading from a folder, do some updating */
        if (mp) {
index 9774729..25ee492 100644 (file)
--- a/uip/new.c
+++ b/uip/new.c
@@ -68,7 +68,7 @@ count_messages(char *field)
                }
        }
 
-       free(field);
+       mh_free0(&field);
 
        return total;
 }
@@ -134,11 +134,11 @@ get_msgnums(char *folder, char *sequences[])
                                        } else {
                                                old_msgnums = msgnums;
                                                msgnums = concat(old_msgnums, " ", this_msgnums, (void *)NULL);
-                                               free(old_msgnums);
-                                               free(this_msgnums);
+                                               mh_free0(&old_msgnums);
+                                               mh_free0(&this_msgnums);
                                        }
                                }
-                               free(cp);
+                               mh_free0(&cp);
                        } else {
                                /* and here */
                                if (seq_in_list(name, sequences)) {
@@ -148,8 +148,8 @@ get_msgnums(char *folder, char *sequences[])
                                        } else {
                                                old_msgnums = msgnums;
                                                msgnums = concat(old_msgnums, " ", this_msgnums, (void *)NULL);
-                                               free(old_msgnums);
-                                               free(this_msgnums);
+                                               mh_free0(&old_msgnums);
+                                               mh_free0(&this_msgnums);
                                        }
                                }
                        }
index b12597b..0465ef5 100644 (file)
@@ -1252,8 +1252,7 @@ plist
                case FLD:
                case FLDPLUS:
                        if (bp != NULL) {
-                               free(bp);
-                               bp = NULL;
+                               mh_free0(&bp);
                        }
                        bp = getcpy(buf);
                        while (state == FLDPLUS) {
@@ -1272,7 +1271,7 @@ plist
                        if (state == LENERR || state == FMTERR)
                                advise(NULL, "format error in message %d", msgnum);
                        if (bp != NULL)
-                               free(bp);
+                               mh_free0(&bp);
                        return 0;
 
                default:
@@ -1289,6 +1288,6 @@ plist
                        : (twsort(tw, &n->n_tws) < 0);
 
        if (bp != NULL)
-               free(bp);
+               mh_free0(&bp);
        return state;
 }
index 59ffdf2..e3c38cd 100644 (file)
@@ -260,16 +260,16 @@ finished: ;
        }
        fclose(out);
 
-       free(scanl);
+       mh_free0(&scanl);
        for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
                        nxtbuf++, i--) {
-               free(cptr->c_text);
+               mh_free0(&(cptr->c_text));
        }
        while (i-- > 0) {
-               free(*nxtbuf++);
+               mh_free0(nxtbuf++);
        }
-       free(compbuffers);
-       free(used_buf);
+       mh_free0(&compbuffers);
+       mh_free0(&used_buf);
 }
 
 
index a67b150..ab5d55f 100644 (file)
@@ -187,7 +187,7 @@ main(int argc, char **argv)
                        cp = getcpy(m_name(msgnum));
                        if (m_file(cp, folders, foldp, !linkf))
                                exit(EX_IOERR);
-                       free(cp);
+                       mh_free0(&cp);
                }
        }
 
index cd0f6be..505160e 100644 (file)
@@ -537,7 +537,7 @@ finished:
                if (sp != cptr->c_text) {
                        cp = cptr->c_text;
                        cptr->c_text = getcpy(sp);
-                       free(cp);
+                       mh_free0(&cp);
                }
        }
        i = format_len + char_read + 256;
@@ -582,14 +582,14 @@ finished:
        }
 
        /* return dynamically allocated buffers */
-       free(scanl);
+       mh_free0(&scanl);
        for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
                        nxtbuf++, i--)
-               free(cptr->c_text);  /* if not nxtbuf, nxtbuf already freed */
+               mh_free0(&(cptr->c_text));  /* if not nxtbuf, nxtbuf already freed */
        while ( i-- > 0)
-               free(*nxtbuf++);  /* free unused nxtbufs */
-       free((char *) compbuffers);
-       free((char *) used_buf);
+               mh_free0(nxtbuf++);  /* free unused nxtbufs */
+       mh_free0(&compbuffers);
+       mh_free0(&used_buf);
 }
 
 static char *buf;  /* our current working buffer */
index 230360b..64ee68c 100644 (file)
--- a/uip/rmf.c
+++ b/uip/rmf.c
@@ -7,6 +7,7 @@
 */
 
 #include <h/mh.h>
+#include <h/utils.h>
 #include <unistd.h>
 #include <dirent.h>
 #include <locale.h>
@@ -110,7 +111,7 @@ main(int argc, char **argv)
                cp = concat("Remove folder \"", folder, "\"? ", NULL);
                if (!getanswer(cp))
                        exit(EX_OK);
-               free(cp);
+               mh_free0(&cp);
        }
 
        if (rmf(folder) == OK) {
@@ -248,5 +249,5 @@ rma(char *folder)
                        pp = np;
                }
        }
-       free(cp);
+       mh_free0(&cp);
 }
index 0f755ca..e595857 100644 (file)
@@ -183,7 +183,7 @@ main(int argc, char **argv)
                }
                num_unseen_seq = i;
                if (dp) {
-                       free(dp);
+                       mh_free0(&dp);
                }
        }
 
index ca267fa..17a1d36 100644 (file)
@@ -783,7 +783,7 @@ parse(int fd)
                                                }
                                                p->p_value = add(lp, cp);
                                        }
-                                       free(lp);
+                                       mh_free0(&lp);
                                        break;
                                }
                        }
index 470fa8b..8f1d2ea 100644 (file)
@@ -265,8 +265,8 @@ main(int argc, char **argv)
                        }
                }
                *fp = 0;
-               free(slist);
-               free(dlist);
+               mh_free0(&slist);
+               mh_free0(&dlist);
                dlist = flist;
        }
 
@@ -368,9 +368,9 @@ get_fields(char *datesw, int msg, struct smsg *smsg)
                        if (state == LENERR || state == FMTERR)
                                admonish(NULL, "format error in message %d (header #%d)", msg, compnum);
                        if (datecomp)
-                               free(datecomp);
+                               mh_free0(&datecomp);
                        if (subjcomp)
-                               free(subjcomp);
+                               mh_free0(&subjcomp);
                        fclose(in);
                        return (0);
 
@@ -432,7 +432,7 @@ get_fields(char *datesw, int msg, struct smsg *smsg)
        }
        fclose(in);
        if (datecomp)
-               free(datecomp);
+               mh_free0(&datecomp);
 
        return (1);
 }
index a4c170b..e2b5a88 100644 (file)
@@ -243,7 +243,7 @@ main(int argc, char **argv)
                                cp = add(buf, cp);
                        }
                        putfmt(name, cp, out);
-                       free(cp);
+                       mh_free0(&cp);
                        continue;
 
                case BODY:
@@ -554,7 +554,7 @@ putadr(char *name, struct mailname *nl)
                                cp = getcpy(mp->m_gname);
                                cp = add(";", cp);
                                linepos = putone(cp, linepos, namelen);
-                               free(cp);
+                               mh_free0(&cp);
                                cp = NULL;
                        }
                } else {
index d313f05..27c8d91 100644 (file)
@@ -167,7 +167,7 @@ main(int argc, char **argv)
                }
                pclose(in);
        }
-       free(cmd);
+       mh_free0(&cmd);
        naddrs += n;
 
        cmd = add("ali -list", NULL);
@@ -180,7 +180,7 @@ main(int argc, char **argv)
                }
                pclose(in);
        }
-       free(cmd);
+       mh_free0(&cmd);
        naddrs += n;
 
        cmd = add("ali -list", NULL);
@@ -193,7 +193,7 @@ main(int argc, char **argv)
                }
                pclose(in);
        }
-       free(cmd);
+       mh_free0(&cmd);
        naddrs += n;
 
        return naddrs ? 0 : 1;
@@ -229,7 +229,7 @@ process(char *file)
                                cp = add(buf, cp);
                        }
                        proc_hdr(name, cp);
-                       free(cp);
+                       mh_free0(&cp);
                        continue;
 
                case BODY: