[PATCH 05/12] vsh: Prefer g_strdup_printf() over g_snprintf() in vshReadlineOptionsGenerator()

Michal Privoznik mprivozn at redhat.com
Thu Feb 4 14:13:30 UTC 2021


The vshReadlineOptionsGenerator() function returns a string list
of all --options for given command. But the way that individual
items on the list are allocated can be written better.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 tools/vsh.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/tools/vsh.c b/tools/vsh.c
index 13bf525acc..d7ab7c61db 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -2627,7 +2627,6 @@ vshReadlineOptionsGenerator(const char *text,
         const char *name = cmd->opts[list_index].name;
         bool exists = false;
         vshCmdOpt *opt =  last->opts;
-        size_t name_len;
 
         /* Skip aliases, we do not report them in help output either. */
         if (cmd->opts[list_index].type == VSH_OT_ALIAS)
@@ -2660,9 +2659,7 @@ vshReadlineOptionsGenerator(const char *text,
             return NULL;
         }
 
-        name_len = strlen(name);
-        ret[ret_size] = g_new0(char, name_len + 3);
-        g_snprintf(ret[ret_size], name_len + 3,  "--%s", name);
+        ret[ret_size] = g_strdup_printf("--%s", name);
         ret_size++;
         /* Terminate the string list properly. */
         ret[ret_size] = NULL;
-- 
2.26.2




More information about the libvir-list mailing list