]> xenbits.xensource.com Git - libvirt.git/commitdiff
vsh: Use g_auto() for string lists returned in readline command/options generators
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 2 Feb 2021 10:09:01 +0000 (11:09 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 10 Feb 2021 10:51:59 +0000 (11:51 +0100)
There are two functions that are used to generate completion
lists: vshReadlineCommandGenerator() for command names and
vshReadlineOptionsGenerator() for --options for given command.
Both return a string list, but may also fail while constructing
it. For that case, they call g_strfreev() explicitly, which is
needless since we have g_auto(GStrv).

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
tools/vsh.c

index 201b7ba6d16f77fefb723b468537f1de14ea8ee8..9b994559ee5f8856f36014c1dab4aed49cac6a77 100644 (file)
@@ -2572,7 +2572,7 @@ vshReadlineCommandGenerator(const char *text)
     const vshCmdGrp *grp;
     const vshCmdDef *cmds;
     size_t ret_size = 0;
-    char **ret = NULL;
+    g_auto(GStrv) ret = NULL;
 
     grp = cmdGroups;
 
@@ -2588,10 +2588,9 @@ vshReadlineCommandGenerator(const char *text)
                     continue;
 
                 if (STRPREFIX(name, text)) {
-                    if (VIR_REALLOC_N(ret, ret_size + 2) < 0) {
-                        g_strfreev(ret);
+                    if (VIR_REALLOC_N(ret, ret_size + 2) < 0)
                         return NULL;
-                    }
+
                     ret[ret_size] = g_strdup(name);
                     ret_size++;
                     /* Terminate the string list properly. */
@@ -2604,7 +2603,7 @@ vshReadlineCommandGenerator(const char *text)
         }
     }
 
-    return ret;
+    return g_steal_pointer(&ret);
 }
 
 static char **
@@ -2615,7 +2614,7 @@ vshReadlineOptionsGenerator(const char *text,
     size_t list_index = 0;
     size_t len = strlen(text);
     size_t ret_size = 0;
-    char **ret = NULL;
+    g_auto(GStrv) ret = NULL;
 
     if (!cmd)
         return NULL;
@@ -2654,10 +2653,8 @@ vshReadlineOptionsGenerator(const char *text,
         if (exists)
             continue;
 
-        if (VIR_REALLOC_N(ret, ret_size + 2) < 0) {
-            g_strfreev(ret);
+        if (VIR_REALLOC_N(ret, ret_size + 2) < 0)
             return NULL;
-        }
 
         ret[ret_size] = g_strdup_printf("--%s", name);
         ret_size++;
@@ -2665,7 +2662,7 @@ vshReadlineOptionsGenerator(const char *text,
         ret[ret_size] = NULL;
     }
 
-    return ret;
+    return g_steal_pointer(&ret);
 }