]> diplodocus.org Git - nmh/blobdiff - uip/mhfree.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / mhfree.c
index 38b830ec20908eba042b1517d954642ad17bed3a..6a827e080beb568f913c88ca92c98ccaf7965f6f 100644 (file)
@@ -1,6 +1,4 @@
-
-/*
- * mhfree.c -- routines to free the data structures used to
+/* mhfree.c -- routines to free the data structures used to
  *          -- represent MIME messages
  *
  * This code is Copyright (c) 2002, by the authors of nmh.  See the
@@ -9,29 +7,24 @@
  */
 
 #include <h/mh.h>
-#include <errno.h>
+#include <h/utils.h>
 #include <h/mime.h>
 #include <h/mhparse.h>
+#include "sbr/m_mktemp.h"
+#include "mhfree.h"
 
 /* The list of top-level contents to display */
 CT *cts = NULL;
 
-/*
- * prototypes
- */
-void free_content (CT);
-void free_header (CT);
-void free_ctinfo (CT);
-void free_encoding (CT, int);
-void freects_done (int);
-
 /*
  * static prototypes
  */
+static void free_header (CT);
 static void free_text (CT);
 static void free_multi (CT);
 static void free_partial (CT);
 static void free_external (CT);
+static void free_pmlist (PM *);
 
 
 /*
@@ -49,14 +42,10 @@ free_content (CT ct)
      */
     free_header (ct);
 
-    if (ct->c_partno)
-       free (ct->c_partno);
-
-    if (ct->c_vrsn)
-       free (ct->c_vrsn);
-
-    if (ct->c_ctline)
-       free (ct->c_ctline);
+    free(ct->c_partno);
+    free(ct->c_vrsn);
+    free(ct->c_ctline);
+    ct->c_partno = ct->c_vrsn = ct->c_ctline = NULL;
 
     free_ctinfo (ct);
 
@@ -81,43 +70,45 @@ free_content (CT ct)
            }
            break;
 
-       case CT_TEXT:
+       default:
+           /* Assume that the ct is for text.  mhfixmsg(1) uses it for
+              decoding application content. */
            free_text (ct);
            break;
     }
 
-    if (ct->c_showproc)
-       free (ct->c_showproc);
-    if (ct->c_termproc)
-       free (ct->c_termproc);
-    if (ct->c_storeproc)
-       free (ct->c_storeproc);
+    free(ct->c_showproc);
+    free(ct->c_termproc);
+    free(ct->c_storeproc);
+    ct->c_showproc = ct->c_termproc = ct->c_storeproc = NULL;
 
-    if (ct->c_celine)
-       free (ct->c_celine);
+    free(ct->c_celine);
+    ct->c_celine = NULL;
 
     /* free structures for content encodings */
     free_encoding (ct, 1);
 
-    if (ct->c_id)
-       free (ct->c_id);
-    if (ct->c_descr)
-       free (ct->c_descr);
-    if (ct->c_dispo)
-       free (ct->c_dispo);
+    free(ct->c_id);
+    free(ct->c_descr);
+    free(ct->c_dispo);
+    free(ct->c_dispo_type);
+    ct->c_id = ct->c_descr = ct->c_dispo = ct->c_dispo_type = NULL;
+    free_pmlist (&ct->c_dispo_first);
 
     if (ct->c_file) {
        if (ct->c_unlink)
-           unlink (ct->c_file);
+           (void) m_unlink (ct->c_file);
        free (ct->c_file);
+       ct->c_file = NULL;
     }
-    if (ct->c_fp)
+    if (ct->c_fp) {
        fclose (ct->c_fp);
+       ct->c_fp = NULL;
+    }
 
-    if (ct->c_storage)
-       free (ct->c_storage);
-    if (ct->c_folder)
-       free (ct->c_folder);
+    free(ct->c_storage);
+    free(ct->c_folder);
+    ct->c_storage = ct->c_folder = NULL;
 
     free (ct);
 }
@@ -128,7 +119,7 @@ free_content (CT ct)
  * for this content.
  */
 
-void
+static void
 free_header (CT ct)
 {
     HF hp1, hp2;
@@ -152,30 +143,16 @@ free_header (CT ct)
 void
 free_ctinfo (CT ct)
 {
-    char **ap;
     CI ci;
 
     ci = &ct->c_ctinfo;
-    if (ci->ci_type) {
-       free (ci->ci_type);
-       ci->ci_type = NULL;
-    }
-    if (ci->ci_subtype) {
-       free (ci->ci_subtype);
-       ci->ci_subtype = NULL;
-    }
-    for (ap = ci->ci_attrs; *ap; ap++) {
-       free (*ap);
-       *ap = NULL;
-    }
-    if (ci->ci_comment) {
-       free (ci->ci_comment);
-       ci->ci_comment = NULL;
-    }
-    if (ci->ci_magic) {
-       free (ci->ci_magic);
-       ci->ci_magic = NULL;
-    }
+    free(ci->ci_type);
+    free(ci->ci_subtype);
+    ci->ci_type = ci->ci_subtype = NULL;
+    free_pmlist(&ci->ci_first_pm);
+    free(ci->ci_comment);
+    free(ci->ci_magic);
+    ci->ci_comment = ci->ci_magic = NULL;
 }
 
 
@@ -187,7 +164,7 @@ free_text (CT ct)
     if (!(t = (struct text *) ct->c_ctparams))
        return;
 
-    free ((char *) t);
+    free(t);
     ct->c_ctparams = NULL;
 }
 
@@ -201,19 +178,19 @@ free_multi (CT ct)
     if (!(m = (struct multipart *) ct->c_ctparams))
        return;
 
-    if (m->mp_start)
-       free (m->mp_start);
-    if (m->mp_stop)
-       free (m->mp_stop);
+    free(m->mp_start);
+    free(m->mp_stop);
+    free (m->mp_content_before);
+    free (m->mp_content_after);
        
     for (part = m->mp_parts; part; part = next) {
        next = part->mp_next;
        free_content (part->mp_part);
-       free ((char *) part);
+       free(part);
     }
     m->mp_parts = NULL;
 
-    free ((char *) m);
+    free(m);
     ct->c_ctparams = NULL;
 }
 
@@ -226,10 +203,9 @@ free_partial (CT ct)
     if (!(p = (struct partial *) ct->c_ctparams))
        return;
 
-    if (p->pm_partid)
-       free (p->pm_partid);
+    free(p->pm_partid);
 
-    free ((char *) p);
+    free(p);
     ct->c_ctparams = NULL;
 }
 
@@ -243,14 +219,33 @@ free_external (CT ct)
        return;
 
     free_content (e->eb_content);
-    if (e->eb_body)
-       free (e->eb_body);
-
-    free ((char *) e);
+    free(e->eb_body);
+    free(e->eb_url);
+    free(e);
     ct->c_ctparams = NULL;
 }
 
 
+static void
+free_pmlist (PM *p)
+{
+    PM pm = *p, pm2;
+
+    while (pm != NULL) {
+        free(pm->pm_name);
+        free(pm->pm_value);
+        free(pm->pm_charset);
+        free(pm->pm_lang);
+       pm2 = pm->pm_next;
+       free(pm);
+       pm = pm2;
+    }
+
+    if (*p)
+       *p = NULL;
+}
+
+
 /*
  * Free data structures related to encoding/decoding
  * Content-Transfer-Encodings.
@@ -259,10 +254,7 @@ free_external (CT ct)
 void
 free_encoding (CT ct, int toplevel)
 {
-    CE ce;
-
-    if (!(ce = ct->c_cefile))
-       return;
+    CE ce = &ct->c_cefile;
 
     if (ce->ce_fp) {
        fclose (ce->ce_fp);
@@ -271,28 +263,26 @@ free_encoding (CT ct, int toplevel)
 
     if (ce->ce_file) {
        if (ce->ce_unlink)
-           unlink (ce->ce_file);
+           (void) m_unlink (ce->ce_file);
        free (ce->ce_file);
        ce->ce_file = NULL;
     }
 
-    if (toplevel) {
-       free ((char *) ce);
-       ct->c_cefile = NULL;
-    } else {
+    if (! toplevel) {
        ct->c_ceopenfnx = NULL;
     }
 }
 
 
-void
+void NORETURN
 freects_done (int status)
 {
     CT *ctp;
 
-    if ((ctp = cts))
-       for (; *ctp; ctp++)
-           free_content (*ctp);
+    for (ctp = cts; ctp && *ctp; ctp++)
+       free_content (*ctp);
+
+    free (cts);
 
     exit (status);
 }