From a7850b871becb5121794a7fc6c1f7242588ee33f Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Fri, 19 Jun 2015 13:06:34 +0200 Subject: [PATCH] src/utils_cmd_flush.c: Fix memory leak. --- src/utils_cmd_flush.c | 115 +++++++++++++++++++------------------------------- 1 file changed, 43 insertions(+), 72 deletions(-) diff --git a/src/utils_cmd_flush.c b/src/utils_cmd_flush.c index 4e7526be..9a59310a 100644 --- a/src/utils_cmd_flush.c +++ b/src/utils_cmd_flush.c @@ -26,32 +26,6 @@ #include "plugin.h" #include "utils_parse_option.h" -#define print_to_socket(fh, ...) \ - do { \ - if (fprintf (fh, __VA_ARGS__) < 0) { \ - char errbuf[1024]; \ - WARNING ("handle_flush: failed to write to socket #%i: %s", \ - fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \ - return -1; \ - } \ - fflush(fh); \ - } while (0) - -static int add_to_array (char ***array, int *array_num, char *value) -{ - char **temp; - - temp = (char **) realloc (*array, sizeof (char *) * (*array_num + 1)); - if (temp == NULL) - return (-1); - - *array = temp; - (*array)[*array_num] = value; - (*array_num)++; - - return (0); -} /* int add_to_array */ - int handle_flush (FILE *fh, char *buffer) { int success = 0; @@ -59,11 +33,24 @@ int handle_flush (FILE *fh, char *buffer) double timeout = 0.0; char **plugins = NULL; - int plugins_num = 0; + size_t plugins_num = 0; char **identifiers = NULL; - int identifiers_num = 0; + size_t identifiers_num = 0; - int i; + size_t i; + +#define PRINT_TO_SOCK(fh, ...) \ + do { \ + if (fprintf (fh, __VA_ARGS__) < 0) { \ + char errbuf[1024]; \ + WARNING ("handle_flush: failed to write to socket #%i: %s", \ + fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \ + strarray_free (plugins, plugins_num); \ + strarray_free (identifiers, identifiers_num); \ + return -1; \ + } \ + fflush(fh); \ + } while (0) if ((fh == NULL) || (buffer == NULL)) return (-1); @@ -73,7 +60,7 @@ int handle_flush (FILE *fh, char *buffer) if (strncasecmp ("FLUSH", buffer, strlen ("FLUSH")) != 0) { - print_to_socket (fh, "-1 Cannot parse command.\n"); + PRINT_TO_SOCK (fh, "-1 Cannot parse command.\n"); return (-1); } buffer += strlen ("FLUSH"); @@ -89,34 +76,30 @@ int handle_flush (FILE *fh, char *buffer) status = parse_option (&buffer, &opt_key, &opt_value); if (status != 0) { - print_to_socket (fh, "-1 Parsing options failed.\n"); - sfree (plugins); - sfree (identifiers); + PRINT_TO_SOCK (fh, "-1 Parsing options failed.\n"); + strarray_free (plugins, plugins_num); + strarray_free (identifiers, identifiers_num); return (-1); } if (strcasecmp ("plugin", opt_key) == 0) - { - add_to_array (&plugins, &plugins_num, opt_value); - } + strarray_add (&plugins, &plugins_num, opt_value); else if (strcasecmp ("identifier", opt_key) == 0) - { - add_to_array (&identifiers, &identifiers_num, opt_value); - } + strarray_add (&identifiers, &identifiers_num, opt_value); else if (strcasecmp ("timeout", opt_key) == 0) { char *endptr; - + errno = 0; endptr = NULL; timeout = strtod (opt_value, &endptr); if ((endptr == opt_value) || (errno != 0) || (!isfinite (timeout))) { - print_to_socket (fh, "-1 Invalid value for option `timeout': " + PRINT_TO_SOCK (fh, "-1 Invalid value for option `timeout': " "%s\n", opt_value); - sfree (plugins); - sfree (identifiers); + strarray_free (plugins, plugins_num); + strarray_free (identifiers, identifiers_num); return (-1); } else if (timeout < 0.0) @@ -126,34 +109,29 @@ int handle_flush (FILE *fh, char *buffer) } else { - print_to_socket (fh, "-1 Cannot parse option %s\n", opt_key); - sfree (plugins); - sfree (identifiers); + PRINT_TO_SOCK (fh, "-1 Cannot parse option %s\n", opt_key); + strarray_free (plugins, plugins_num); + strarray_free (identifiers, identifiers_num); return (-1); } } /* while (*buffer != 0) */ - /* Add NULL entries for `any plugin' and/or `any value' if nothing was - * specified. */ - if (plugins_num == 0) - add_to_array (&plugins, &plugins_num, NULL); - - if (identifiers_num == 0) - add_to_array (&identifiers, &identifiers_num, NULL); - - for (i = 0; i < plugins_num; i++) + for (i = 0; (i == 0) || (i < plugins_num); i++) { - char *plugin; + char *plugin = NULL; int j; - plugin = plugins[i]; + if (plugins_num != 0) + plugin = plugins[i]; - for (j = 0; j < identifiers_num; j++) + for (j = 0; (j == 0) || (j < identifiers_num); j++) { - char *identifier; + char *identifier = NULL; int status; - identifier = identifiers[j]; + if (identifiers_num != 0) + identifier = identifiers[j]; + status = plugin_flush (plugin, DOUBLE_TO_CDTIME_T (timeout), identifier); @@ -164,20 +142,13 @@ int handle_flush (FILE *fh, char *buffer) } } - if ((success + error) > 0) - { - print_to_socket (fh, "0 Done: %i successful, %i errors\n", - success, error); - } - else - { - plugin_flush (NULL, timeout, NULL); - print_to_socket (fh, "0 Done\n"); - } + PRINT_TO_SOCK (fh, "0 Done: %i successful, %i errors\n", + success, error); - sfree (plugins); - sfree (identifiers); + strarray_free (plugins, plugins_num); + strarray_free (identifiers, identifiers_num); return (0); +#undef PRINT_TO_SOCK } /* int handle_flush */ /* vim: set sw=4 ts=4 tw=78 noexpandtab : */ -- 2.11.0