From d0ce5fc550ba59522c09c261303d8bbc058174a5 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 30 Mar 2017 13:20:40 +0200 Subject: [PATCH] cli: cleanup get_value_to_print() util Don't cast const strings to non-const. And don't track whether to free a variable in a boolean. Instead, assign ownership to variables that get destroyed when the function returns. --- clients/cli/utils.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/clients/cli/utils.c b/clients/cli/utils.c index b93fa10e95..cac6e1a128 100644 --- a/clients/cli/utils.c +++ b/clients/cli/utils.c @@ -822,35 +822,37 @@ colorize_string (NmcColorOption color_option, static const char * get_value_to_print (NmcColorOption color_option, - NmcOutputField *field, + const NmcOutputField *field, gboolean field_name, const char *not_set_str, char **out_to_free) { gboolean is_array = field->value_is_array; - char *value; + const char *value; const char *out; - gboolean free_value; + gs_free char *free_value = NULL; + + nm_assert (out_to_free && !*out_to_free); if (field_name) value = _(field->name); - else + else { value = field->value ? (is_array - ? g_strjoinv (" | ", (char **) field->value) - : (*((char *) field->value) - ? (char *) field->value - : (char *) not_set_str)) - : (char *) not_set_str; - free_value = field->value && is_array && !field_name; + ? (free_value = g_strjoinv (" | ", (char **) field->value)) + : (*((const char *) field->value)) + ? field->value + : not_set_str) + : not_set_str; + } /* colorize the value */ out = colorize_string (color_option, field->color, field->color_fmt, value, out_to_free); - if (*out_to_free) { - if (free_value) - g_free (value); - } else if (free_value) - *out_to_free = value; + + if (out && out == free_value) { + nm_assert (!*out_to_free); + *out_to_free = g_steal_pointer (&free_value); + } return out; }