From a6b16a6cff7593df26deed0f5c861ff12299b0d5 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Fri, 4 May 2007 09:24:07 +0200 Subject: [PATCH] src/plugin.c: Copy the data-sets before inserting them in the list. Thus memory management is done by `plugin.c' and overwritten DSes can be freed. --- src/plugin.c | 46 +++++++++++++++++++++++++++++++++++++++++++--- src/types_list.c | 5 +++-- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/plugin.c b/src/plugin.c index 495e1693..ee1240cb 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -417,7 +417,32 @@ int plugin_register_shutdown (char *name, int plugin_register_data_set (const data_set_t *ds) { - return (register_callback (&list_data_set, ds->type, (void *) ds)); + data_set_t *ds_copy; + int i; + + if (llist_search (list_data_set, ds->type) != NULL) + { + NOTICE ("Replacing DS `%s' with another version.", ds->type); + plugin_unregister_data_set (ds->type); + } + + ds_copy = (data_set_t *) malloc (sizeof (data_set_t)); + if (ds_copy == NULL) + return (-1); + memcpy(ds_copy, ds, sizeof (data_set_t)); + + ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t) + * ds->ds_num); + if (ds_copy->ds == NULL) + { + free (ds_copy); + return (-1); + } + + for (i = 0; i < ds->ds_num; i++) + memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t)); + + return (register_callback (&list_data_set, ds->type, (void *) ds_copy)); } /* int plugin_register_data_set */ int plugin_register_log (char *name, @@ -465,8 +490,23 @@ int plugin_unregister_shutdown (const char *name) int plugin_unregister_data_set (const char *name) { - return (plugin_unregister (list_data_set, name)); -} + llentry_t *e; + data_set_t *ds; + + e = llist_search (list_data_set, name); + + if (e == NULL) + return (-1); + + llist_remove (list_data_set, e); + ds = (data_set_t *) e->value; + llentry_destroy (e); + + sfree (ds->ds); + sfree (ds); + + return (0); +} /* int plugin_unregister_data_set */ int plugin_unregister_log (const char *name) { diff --git a/src/types_list.c b/src/types_list.c index 6fce0190..3963897e 100644 --- a/src/types_list.c +++ b/src/types_list.c @@ -129,8 +129,9 @@ static void parse_line (char *buf, size_t buf_len) ds->type, ds->ds_num, (void *) ds->ds); plugin_register_data_set (ds); - /* Do NOT free `ds' and `ds->ds', because it's NOT copied by - * `plugin_register_data_set'!. */ + + sfree (ds->ds); + sfree (ds); } /* void parse_line */ static void parse_file (FILE *fh) -- 2.30.2