From afbfe4ff6003d2e148130d570fd46bf5dd3103bd Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Tue, 25 Oct 2016 14:18:09 +0200 Subject: Stack and release color names memory Don't call the color_names memory freeing code explicitly in the cleanup function as it is taken care of via {STACK,RELEASE}_VAR(). This implies the memory is "garbage collected" like other uses of those macros, too. Also, declare and initialize the color_names array in a non-global, tighter scope. --- colorize.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'colorize.c') diff --git a/colorize.c b/colorize.c index 05fd846..a891135 100644 --- a/colorize.c +++ b/colorize.c @@ -125,8 +125,6 @@ struct color_name { char *orig; }; -static struct color_name *color_names[3] = { NULL, NULL, NULL }; - struct color { const char *name; const char *code; @@ -510,8 +508,6 @@ print_version (void) static void cleanup (void) { - free_color_names (color_names); - if (stream && fileno (stream) != STDIN_FILENO) fclose (stream); #if DEBUG @@ -534,9 +530,9 @@ free_color_names (struct color_name **color_names) unsigned int i; for (i = 0; color_names[i]; i++) { - free (color_names[i]->name); - free (color_names[i]->orig); - free_null (color_names[i]); + RELEASE_VAR (color_names[i]->name); + RELEASE_VAR (color_names[i]->orig); + RELEASE_VAR (color_names[i]); } } @@ -546,6 +542,7 @@ process_args (unsigned int arg_cnt, char **arg_strings, bool *bold, const struct int ret; char *p; struct stat sb; + struct color_name *color_names[3] = { NULL, NULL, NULL }; const char *color_string = arg_cnt >= 1 ? arg_strings[0] : NULL; const char *file_string = arg_cnt == 2 ? arg_strings[1] : NULL; @@ -743,13 +740,16 @@ gather_color_names (const char *color_string, bool *bold, struct color_name **co } color_names[index] = xcalloc (1, sizeof (struct color_name)); + STACK_VAR (color_names[index]); color_names[index]->orig = xstrdup (color); + STACK_VAR (color_names[index]->orig); for (ch = color; *ch; ch++) *ch = tolower (*ch); color_names[index]->name = xstrdup (color); + STACK_VAR (color_names[index]->name); } RELEASE_VAR (str); -- cgit v1.2.3