From 5ea1f5a36f99f6c828936d1f993c9ba790b953ec Mon Sep 17 00:00:00 2001 From: Pavel Rochnyack Date: Fri, 7 Jul 2017 01:48:21 +0700 Subject: [PATCH] Free `userdata` if plugin_register_complex_read() has failed. --- src/apache.c | 32 ++++++++++++++------------------ src/daemon/plugin.c | 26 +++++++++++++++----------- src/memcached.c | 40 ++++++++++++++++++++-------------------- src/perl.c | 9 +++++---- src/routeros.c | 19 +++++++++---------- src/snmp.c | 1 - src/tail_csv.c | 1 - 7 files changed, 63 insertions(+), 65 deletions(-) diff --git a/src/apache.c b/src/apache.c index 650760c3..07b2b57d 100644 --- a/src/apache.c +++ b/src/apache.c @@ -216,29 +216,25 @@ static int config_add(oconfig_item_t *ci) { status = -1; } - if (status == 0) { - char callback_name[3 * DATA_MAX_NAME_LEN]; - - snprintf(callback_name, sizeof(callback_name), "apache/%s/%s", - (st->host != NULL) ? st->host : hostname_g, - (st->name != NULL) ? st->name : "default"); - - status = plugin_register_complex_read( - /* group = */ NULL, - /* name = */ callback_name, - /* callback = */ apache_read_host, - /* interval = */ 0, - &(user_data_t){ - .data = st, .free_func = apache_free, - }); - } - if (status != 0) { apache_free(st); return -1; } - return 0; + char callback_name[3 * DATA_MAX_NAME_LEN]; + + snprintf(callback_name, sizeof(callback_name), "apache/%s/%s", + (st->host != NULL) ? st->host : hostname_g, + (st->name != NULL) ? st->name : "default"); + + return plugin_register_complex_read( + /* group = */ NULL, + /* name = */ callback_name, + /* callback = */ apache_read_host, + /* interval = */ 0, + &(user_data_t){ + .data = st, .free_func = apache_free, + }); } /* int config_add */ static int config(oconfig_item_t *ci) { diff --git a/src/daemon/plugin.c b/src/daemon/plugin.c index 0451e8a7..d22bd940 100644 --- a/src/daemon/plugin.c +++ b/src/daemon/plugin.c @@ -191,16 +191,21 @@ static int plugin_update_internal_statistics(void) { /* {{{ */ return 0; } /* }}} int plugin_update_internal_statistics */ -static void destroy_callback(callback_func_t *cf) /* {{{ */ +static void free_userdata(user_data_t const *ud) /* {{{ */ { - if (cf == NULL) + if (ud == NULL) return; - if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL)) { - cf->cf_udata.free_func(cf->cf_udata.data); - cf->cf_udata.data = NULL; - cf->cf_udata.free_func = NULL; + if ((ud->data != NULL) && (ud->free_func != NULL)) { + ud->free_func(ud->data); } +} /* }}} void free_userdata */ + +static void destroy_callback(callback_func_t *cf) /* {{{ */ +{ + if (cf == NULL) + return; + free_userdata(&cf->cf_udata); sfree(cf); } /* }}} void destroy_callback */ @@ -345,6 +350,7 @@ static int create_register_callback(llist_t **list, /* {{{ */ cf = calloc(1, sizeof(*cf)); if (cf == NULL) { + free_userdata(ud); ERROR("plugin: create_register_callback: calloc failed."); return -1; } @@ -1186,6 +1192,7 @@ int plugin_register_complex_read(const char *group, const char *name, rf = calloc(1, sizeof(*rf)); if (rf == NULL) { + free_userdata(user_data); ERROR("plugin_register_complex_read: calloc failed."); return ENOMEM; } @@ -1211,6 +1218,7 @@ int plugin_register_complex_read(const char *group, const char *name, status = plugin_insert_read(rf); if (status != 0) { + free_userdata(&rf->rf_udata); sfree(rf->rf_name); sfree(rf); } @@ -1303,11 +1311,7 @@ int plugin_register_flush(const char *name, plugin_flush_cb callback, }); sfree(flush_name); - if (status != 0) { - sfree(cb->name); - sfree(cb); - return status; - } + return status; } return 0; diff --git a/src/memcached.c b/src/memcached.c index e4ccbce6..3502e355 100644 --- a/src/memcached.c +++ b/src/memcached.c @@ -473,13 +473,7 @@ static int memcached_read(user_data_t *user_data) { return 0; } /* int memcached_read */ -static int memcached_add_read_callback(memcached_t *st) { - char callback_name[3 * DATA_MAX_NAME_LEN]; - int status; - - snprintf(callback_name, sizeof(callback_name), "memcached/%s", - (st->name != NULL) ? st->name : "__legacy__"); - +static int memcached_set_defaults(memcached_t *st) { /* If no
used then: * - Connect to the destination specified by , if present. * If not, use the default address. @@ -516,7 +510,21 @@ static int memcached_add_read_callback(memcached_t *st) { assert(st->connhost != NULL); assert(st->connport != NULL); - status = plugin_register_complex_read( + return 0; +} /* int memcached_set_defaults */ + +static int memcached_add_read_callback(memcached_t *st) { + char callback_name[3 * DATA_MAX_NAME_LEN]; + + if (memcached_set_defaults(st) != 0) { + memcached_free(st); + return -1; + } + + snprintf(callback_name, sizeof(callback_name), "memcached/%s", + (st->name != NULL) ? st->name : "__legacy__"); + + return plugin_register_complex_read( /* group = */ "memcached", /* name = */ callback_name, /* callback = */ memcached_read, @@ -524,8 +532,6 @@ static int memcached_add_read_callback(memcached_t *st) { &(user_data_t){ .data = st, .free_func = memcached_free, }); - - return status; } /* int memcached_add_read_callback */ /* Configuration handling functiions @@ -584,19 +590,15 @@ static int config_add_instance(oconfig_item_t *ci) { break; } - if (status == 0) - status = memcached_add_read_callback(st); - if (status != 0) { memcached_free(st); return -1; } - return 0; -} + return memcached_add_read_callback(st); +} /* int config_add_instance */ static int memcached_config(oconfig_item_t *ci) { - int status = 0; _Bool have_instance_block = 0; for (int i = 0; i < ci->children_num; i++) { @@ -617,8 +619,8 @@ static int memcached_config(oconfig_item_t *ci) { child->key); } /* for (ci->children) */ - return status; -} + return 0; +} /* int memcached_config */ static int memcached_init(void) { memcached_t *st; @@ -640,8 +642,6 @@ static int memcached_init(void) { status = memcached_add_read_callback(st); if (status == 0) memcached_have_instances = 1; - else - memcached_free(st); return status; } /* int memcached_init */ diff --git a/src/perl.c b/src/perl.c index 7c8a6158..971fabe2 100644 --- a/src/perl.c +++ b/src/perl.c @@ -1626,18 +1626,19 @@ static void _plugin_register_generic_userdata(pTHX, int type, ret = plugin_register_flush("perl", perl_flush, /* user_data = */ NULL); } - if (0 == ret) + if (0 == ret) { ret = plugin_register_flush(pluginname, perl_flush, &userdata); + } else { + free(userdata.data); + } } else { ret = -1; } if (0 == ret) XSRETURN_YES; - else { - free(userdata.data); + else XSRETURN_EMPTY; - } } /* static void _plugin_register_generic_userdata ( ... ) */ /* diff --git a/src/routeros.c b/src/routeros.c index fa08b3b0..9ea82976 100644 --- a/src/routeros.c +++ b/src/routeros.c @@ -376,18 +376,17 @@ static int cr_config_router(oconfig_item_t *ci) /* {{{ */ } } - snprintf(read_name, sizeof(read_name), "routeros/%s", router_data->node); - if (status == 0) - status = plugin_register_complex_read( - /* group = */ NULL, read_name, cr_read, /* interval = */ 0, - &(user_data_t){ - .data = router_data, .free_func = (void *)cr_free_data, - }); - - if (status != 0) + if (status != 0) { cr_free_data(router_data); + return status; + } - return status; + snprintf(read_name, sizeof(read_name), "routeros/%s", router_data->node); + return plugin_register_complex_read( + /* group = */ NULL, read_name, cr_read, /* interval = */ 0, + &(user_data_t){ + .data = router_data, .free_func = (void *)cr_free_data, + }); } /* }}} int cr_config_router */ static int cr_config(oconfig_item_t *ci) { diff --git a/src/snmp.c b/src/snmp.c index c299db96..aa3c9dda 100644 --- a/src/snmp.c +++ b/src/snmp.c @@ -711,7 +711,6 @@ static int csnmp_config_add_host(oconfig_item_t *ci) { }); if (status != 0) { ERROR("snmp plugin: Registering complex read function failed."); - csnmp_host_definition_destroy(hd); return -1; } diff --git a/src/tail_csv.c b/src/tail_csv.c index 9d0a15be..a9ce5d19 100644 --- a/src/tail_csv.c +++ b/src/tail_csv.c @@ -484,7 +484,6 @@ static int tcsv_config_add_file(oconfig_item_t *ci) { }); if (status != 0) { ERROR("tail_csv plugin: Registering complex read function failed."); - tcsv_instance_definition_destroy(id); return -1; } -- 2.11.0