summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2021-11-03 22:12:39 +0200
committerSergey Poznyakoff <gray@gnu.org>2021-11-03 22:12:39 +0200
commit278aece2a6e442704d4b48269dec320fd85fcc1f (patch)
tree66724e78c896adaed784bb12b7eb531f00bb64d1
parent8c6f0191c8522627841d3953cfb57484ef29ca17 (diff)
downloadmailutils-278aece2a6e442704d4b48269dec320fd85fcc1f.tar.gz
mailutils-278aece2a6e442704d4b48269dec320fd85fcc1f.tar.bz2
Free allocated memory on exit.
See also 91d35ab2ee. * include/mailutils/cfg.h (mu_cfg_section): New members: ident_storage and label_storage. * libmailutils/cfg/driver.c (mu_config_destroy_container): Free the storage allocated for ident and label. Do this onlyw if the decreased refcount hits zero. (mu_config_container_register_section): Assign allocated strings to ident_storage and label_storage. (mu_config_root_register_section): Register an onexit routine for freeing the allocated memory. * libmailutils/cli/capa.c (capa_free): Register an onexit routine for freeing the allocated memory. * libmailutils/opt/progname.c (mu_set_program_name): Likewise.
-rw-r--r--include/mailutils/cfg.h2
-rw-r--r--libmailutils/cfg/driver.c64
-rw-r--r--libmailutils/cli/capa.c7
-rw-r--r--libmailutils/opt/progname.c17
4 files changed, 67 insertions, 23 deletions
diff --git a/include/mailutils/cfg.h b/include/mailutils/cfg.h
index 19bdd5e7a..2266acc61 100644
--- a/include/mailutils/cfg.h
+++ b/include/mailutils/cfg.h
@@ -192,6 +192,8 @@ struct mu_cfg_section
void *target; /* Actual pointer to the data. It is
recomputed each time the section is
reduced. */
+ char *ident_storage; /* Storage for ident, if malloc'ed */
+ char *label_storage; /* Same for the label */
};
enum mu_cfg_cont_type
diff --git a/libmailutils/cfg/driver.c b/libmailutils/cfg/driver.c
index 56c1daa06..9a8fd2265 100644
--- a/libmailutils/cfg/driver.c
+++ b/libmailutils/cfg/driver.c
@@ -94,6 +94,12 @@ mu_get_canned_container (const char *name)
static struct mu_cfg_cont *root_container;
+static void
+destroy_root_container (void *ptr)
+{
+ mu_config_destroy_container (&root_container);
+}
+
int
mu_config_create_container (struct mu_cfg_cont **pcont,
enum mu_cfg_cont_type type)
@@ -211,25 +217,32 @@ destroy_list (mu_list_t *plist)
void
mu_config_destroy_container (struct mu_cfg_cont **pcont)
{
- struct mu_cfg_cont *cont = *pcont;
- unsigned refcount = mu_refcount_dec (cont->refcount);
- /* printf ("destr %p-%s: %d\n", cont, cont->v.section.ident, refcount); */
- // FIXME: Shouldn't it be done inside the conditional below?
- switch (cont->type)
+ if (pcont)
{
- case mu_cfg_cont_section:
- destroy_list (&cont->v.section.children);
- break;
-
- case mu_cfg_cont_param:
- break;
- }
+ struct mu_cfg_cont *cont = *pcont;
+ if (cont)
+ {
+ unsigned refcount = mu_refcount_dec (cont->refcount);
- if (refcount == 0)
- {
- mu_refcount_destroy (&cont->refcount);
- free (cont);
- *pcont = 0;
+ if (refcount == 0)
+ {
+ switch (cont->type)
+ {
+ case mu_cfg_cont_section:
+ free (cont->v.section.ident_storage);
+ free (cont->v.section.label_storage);
+ destroy_list (&cont->v.section.children);
+ break;
+
+ case mu_cfg_cont_param:
+ break;
+ }
+
+ mu_refcount_destroy (&cont->refcount);
+ free (cont);
+ *pcont = 0;
+ }
+ }
}
}
@@ -418,8 +431,8 @@ mu_config_container_register_section (struct mu_cfg_cont **proot,
mu_list_append (parent->children, container);
s = &container->v.section;
- s->ident = strdup (ident);
- s->label = label ? strdup (label) : NULL;
+ s->ident = s->ident_storage = strdup (ident);
+ s->label = s->label_storage = label ? strdup (label) : NULL;
s->parser = parser;
s->children = NULL;
mu_cfg_section_add_params (s, param);
@@ -445,10 +458,15 @@ mu_config_root_register_section (const char *parent_path,
mu_cfg_section_fp parser,
struct mu_cfg_param *param)
{
- return mu_config_container_register_section (&root_container,
- parent_path,
- ident, label,
- parser, param, NULL);
+ int rc = mu_config_container_register_section (&root_container,
+ parent_path,
+ ident, label,
+ parser, param, NULL);
+ if (rc == 0)
+ {
+ mu_onexit (destroy_root_container, NULL);
+ }
+ return rc;
}
int
diff --git a/libmailutils/cli/capa.c b/libmailutils/cli/capa.c
index 312aed0df..f23752f1c 100644
--- a/libmailutils/cli/capa.c
+++ b/libmailutils/cli/capa.c
@@ -27,6 +27,12 @@
static mu_list_t capa_list;
static void
+capa_list_destroy (void *ptr)
+{
+ mu_list_destroy (&capa_list);
+}
+
+static void
capa_free (void *ptr)
{
struct mu_cli_capa *cp = ptr;
@@ -47,6 +53,7 @@ mu_cli_capa_register (struct mu_cli_capa *capa)
{
mu_list_create (&capa_list);
mu_list_set_destroy_item (capa_list, capa_free);
+ mu_onexit (capa_list_destroy, NULL);
}
mu_list_append (capa_list, cp);
}
diff --git a/libmailutils/opt/progname.c b/libmailutils/opt/progname.c
index 9d4d2469f..e2dbc570e 100644
--- a/libmailutils/opt/progname.c
+++ b/libmailutils/opt/progname.c
@@ -21,9 +21,21 @@
#include <string.h>
#include <mailutils/alloc.h>
#include <mailutils/opt.h>
+#include <mailutils/util.h>
char *mu_program_name;
char *mu_full_program_name;
+static int progname_cleaner_installed;
+
+static void
+progname_cleaner (void *ptr)
+{
+ free (mu_program_name);
+ mu_program_name = NULL;
+ free (mu_full_program_name);
+ mu_full_program_name = NULL;
+ progname_cleaner_installed = 0;
+}
void
mu_set_program_name (const char *arg)
@@ -43,4 +55,9 @@ mu_set_program_name (const char *arg)
p += 3;
free (mu_program_name);
mu_program_name = mu_strdup (p);
+ if (!progname_cleaner_installed)
+ {
+ mu_onexit (progname_cleaner, NULL);
+ progname_cleaner_installed = 1;
+ }
}

Return to:

Send suggestions and report system problems to the System administrator.