[PATCH 2/7] Prefer g_auto(GStrv) over g_strfreev()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



There are a few cases where a string list is freed by an explicit
call of g_strfreev(), but the same result can be achieved by
g_atuo(GStrv).

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 src/bhyve/bhyve_command.c           |  3 +--
 src/bhyve/bhyve_parse_command.c     | 22 +++++-----------
 src/libxl/libxl_conf.c              |  9 +++----
 src/libxl/xen_common.c              | 18 +++++--------
 src/libxl/xen_xl.c                  | 17 +++---------
 src/lxc/lxc_container.c             |  4 +--
 src/lxc/lxc_native.c                | 24 +++++------------
 src/qemu/qemu_driver.c              |  6 ++---
 src/remote/remote_daemon_dispatch.c |  3 +--
 src/remote/remote_driver.c          |  4 +--
 src/storage/storage_backend_rbd.c   |  3 +--
 src/util/vircgroup.c                |  3 +--
 src/util/vircgroupv2.c              |  4 +--
 src/util/virfirmware.c              |  6 ++---
 src/util/viruri.c                   |  3 +--
 src/vbox/vbox_common.c              | 12 +++------
 src/vbox/vbox_snapshot_conf.c       | 40 ++++++++++-------------------
 src/vbox/vbox_tmpl.c                |  3 +--
 src/vz/vz_sdk.c                     |  3 +--
 tests/qemumonitorjsontest.c         |  3 +--
 tests/vboxsnapshotxmltest.c         |  3 +--
 tests/virconftest.c                 |  3 +--
 tests/virfiletest.c                 |  3 +--
 tests/virstringtest.c               |  3 +--
 tools/virsh-host.c                  | 13 +++-------
 tools/virt-login-shell-helper.c     |  7 +++--
 tools/vsh.c                         |  4 +--
 27 files changed, 71 insertions(+), 155 deletions(-)

diff --git a/src/bhyve/bhyve_command.c b/src/bhyve/bhyve_command.c
index ab9d3026cc..cf858dfcd6 100644
--- a/src/bhyve/bhyve_command.c
+++ b/src/bhyve/bhyve_command.c
@@ -833,12 +833,11 @@ virBhyveProcessBuildDestroyCmd(struct _bhyveConn *driver G_GNUC_UNUSED,
 static void
 virAppendBootloaderArgs(virCommand *cmd, virDomainDef *def)
 {
-    char **blargs;
+    g_auto(GStrv) blargs = NULL;
 
     /* XXX: Handle quoted? */
     blargs = g_strsplit(def->os.bootloaderArgs, " ", 0);
     virCommandAddArgSet(cmd, (const char * const *)blargs);
-    g_strfreev(blargs);
 }
 
 static virCommand *
diff --git a/src/bhyve/bhyve_parse_command.c b/src/bhyve/bhyve_parse_command.c
index 76dcea6f21..f2c15f3aa5 100644
--- a/src/bhyve/bhyve_parse_command.c
+++ b/src/bhyve/bhyve_parse_command.c
@@ -127,7 +127,7 @@ bhyveCommandLineToArgv(const char *nativeConfig,
     const char *start;
     const char *next;
     char *line;
-    char **lines = NULL;
+    g_auto(GStrv) lines = NULL;
     size_t i;
     size_t line_count = 0;
     size_t lines_alloc = 0;
@@ -168,7 +168,7 @@ bhyveCommandLineToArgv(const char *nativeConfig,
 
     for (i = 0; i < line_count; i++) {
         size_t j;
-        char **arglist = NULL;
+        g_auto(GStrv) arglist = NULL;
         size_t args_count = 0;
         size_t args_alloc = 0;
 
@@ -223,23 +223,17 @@ bhyveCommandLineToArgv(const char *nativeConfig,
             if (!bhyve_argc)
                 goto error;
             for (j = 0; j < args_count; j++)
-                _bhyve_argv[j] = arglist[j];
+                _bhyve_argv[j] = g_steal_pointer(&arglist[j]);
             _bhyve_argv[j] = NULL;
             *bhyve_argc = args_count-1;
-            VIR_FREE(arglist);
         } else if (!_loader_argv) {
             VIR_REALLOC_N(_loader_argv, args_count + 1);
             if (!loader_argc)
                 goto error;
             for (j = 0; j < args_count; j++)
-                _loader_argv[j] = arglist[j];
+                _loader_argv[j] = g_steal_pointer(&arglist[j]);
             _loader_argv[j] = NULL;
             *loader_argc = args_count-1;
-            VIR_FREE(arglist);
-        } else {
-            /* To prevent a use-after-free here, only free the argument list
-             * when it is definitely not going to be used */
-            g_strfreev(arglist);
         }
     }
 
@@ -247,13 +241,11 @@ bhyveCommandLineToArgv(const char *nativeConfig,
     if (!(*bhyve_argv = _bhyve_argv))
         goto error;
 
-    g_strfreev(lines);
     return 0;
 
  error:
     VIR_FREE(_loader_argv);
     VIR_FREE(_bhyve_argv);
-    g_strfreev(lines);
     return -1;
 }
 
@@ -944,9 +936,9 @@ bhyveParseCommandLineString(const char* nativeConfig,
 {
     virDomainDef *def = NULL;
     int bhyve_argc = 0;
-    char **bhyve_argv = NULL;
+    g_auto(GStrv) bhyve_argv = NULL;
     int loader_argc = 0;
-    char **loader_argv = NULL;
+    g_auto(GStrv) loader_argv = NULL;
 
     if (!(def = virDomainDefNew(xmlopt)))
         goto cleanup;
@@ -982,8 +974,6 @@ bhyveParseCommandLineString(const char* nativeConfig,
     }
 
  cleanup:
-    g_strfreev(loader_argv);
-    g_strfreev(bhyve_argv);
     return def;
  error:
     virDomainDefFree(def);
diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c
index 9f0739e1fa..f37c228139 100644
--- a/src/libxl/libxl_conf.c
+++ b/src/libxl/libxl_conf.c
@@ -1887,8 +1887,7 @@ int
 libxlDriverGetDom0MaxmemConf(libxlDriverConfig *cfg,
                              unsigned long long *maxmem)
 {
-    char **cmd_tokens = NULL;
-    char **mem_tokens = NULL;
+    g_auto(GStrv) cmd_tokens = NULL;
     size_t i;
     size_t j;
     libxl_physinfo physinfo;
@@ -1899,6 +1898,8 @@ libxlDriverGetDom0MaxmemConf(libxlDriverConfig *cfg,
         goto physmem;
 
     for (i = 0; cmd_tokens[i] != NULL; i++) {
+        g_auto(GStrv) mem_tokens = NULL;
+
         if (!STRPREFIX(cmd_tokens[i], "dom0_mem="))
             continue;
 
@@ -1934,8 +1935,6 @@ libxlDriverGetDom0MaxmemConf(libxlDriverConfig *cfg,
                 goto cleanup;
             }
         }
-        g_strfreev(mem_tokens);
-        mem_tokens = NULL;
     }
 
  physmem:
@@ -1950,8 +1949,6 @@ libxlDriverGetDom0MaxmemConf(libxlDriverConfig *cfg,
     ret = 0;
 
  cleanup:
-    g_strfreev(cmd_tokens);
-    g_strfreev(mem_tokens);
     return ret;
 }
 
diff --git a/src/libxl/xen_common.c b/src/libxl/xen_common.c
index 87f090f979..32c31d240e 100644
--- a/src/libxl/xen_common.c
+++ b/src/libxl/xen_common.c
@@ -1045,7 +1045,7 @@ xenParseVifBridge(virDomainNetDef *net, const char *bridge)
         /* 'bridge' string contains a bridge name and one or more vlan trunks */
         size_t i;
         size_t nvlans = 0;
-        char **vlanstr_list = g_strsplit(bridge, ":", 0);
+        g_auto(GStrv) vlanstr_list = g_strsplit(bridge, ":", 0);
 
         if (!vlanstr_list)
             return -1;
@@ -1058,15 +1058,13 @@ xenParseVifBridge(virDomainNetDef *net, const char *bridge)
         net->vlan.tag = g_new0(unsigned int, nvlans);
 
         for (i = 1; i <= nvlans; i++) {
-            if (virStrToLong_ui(vlanstr_list[i], NULL, 10, &tag) < 0) {
-                g_strfreev(vlanstr_list);
+            if (virStrToLong_ui(vlanstr_list[i], NULL, 10, &tag) < 0)
                 return -1;
-            }
+
             net->vlan.tag[i - 1] = tag;
         }
         net->vlan.nTags = nvlans;
         net->vlan.trunk = true;
-        g_strfreev(vlanstr_list);
 
         net->virtPortProfile = g_new0(virNetDevVPortProfile, 1);
         net->virtPortProfile->virtPortType = VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH;
@@ -1201,19 +1199,16 @@ xenParseVif(char *entry, const char *vif_typename)
             goto cleanup;
     }
     if (ip) {
-        char **ip_list = g_strsplit(ip, " ", 0);
+        g_auto(GStrv) ip_list = g_strsplit(ip, " ", 0);
         size_t i;
 
         if (!ip_list)
             goto cleanup;
 
         for (i = 0; ip_list[i]; i++) {
-            if (virDomainNetAppendIPAddress(net, ip_list[i], 0, 0) < 0) {
-                g_strfreev(ip_list);
+            if (virDomainNetAppendIPAddress(net, ip_list[i], 0, 0) < 0)
                 goto cleanup;
-            }
         }
-        g_strfreev(ip_list);
     }
 
     if (script && script[0])
@@ -1579,7 +1574,7 @@ char *
 xenMakeIPList(virNetDevIPInfo *guestIP)
 {
     size_t i;
-    char **address_array;
+    g_auto(GStrv) address_array = NULL;
     char *ret = NULL;
 
     address_array = g_new0(char *, guestIP->nips + 1);
@@ -1592,7 +1587,6 @@ xenMakeIPList(virNetDevIPInfo *guestIP)
     ret = g_strjoinv(" ", address_array);
 
  cleanup:
-    g_strfreev(address_array);
     return ret;
 }
 
diff --git a/src/libxl/xen_xl.c b/src/libxl/xen_xl.c
index 850786e5c9..05d4abbe81 100644
--- a/src/libxl/xen_xl.c
+++ b/src/libxl/xen_xl.c
@@ -246,8 +246,7 @@ static int
 xenParseXLCPUID(virConf *conf, virDomainDef *def)
 {
     g_autofree char *cpuid_str = NULL;
-    char **cpuid_pairs = NULL;
-    char **name_and_value = NULL;
+    g_auto(GStrv) cpuid_pairs = NULL;
     size_t i;
     int ret = -1;
     int policy;
@@ -283,7 +282,7 @@ xenParseXLCPUID(virConf *conf, virDomainDef *def)
     }
 
     for (i = 1; cpuid_pairs[i]; i++) {
-        name_and_value = g_strsplit(cpuid_pairs[i], "=", 2);
+        g_auto(GStrv) name_and_value = g_strsplit(cpuid_pairs[i], "=", 2);
         if (!name_and_value)
             goto cleanup;
         if (!name_and_value[0] || !name_and_value[1]) {
@@ -313,16 +312,11 @@ xenParseXLCPUID(virConf *conf, virDomainDef *def)
                                 xenTranslateCPUFeature(name_and_value[0], true),
                                 policy) < 0)
             goto cleanup;
-
-        g_strfreev(name_and_value);
-        name_and_value = NULL;
     }
 
     ret = 0;
 
  cleanup:
-    g_strfreev(name_and_value);
-    g_strfreev(cpuid_pairs);
     return ret;
 }
 
@@ -406,7 +400,6 @@ xenParseXLVnuma(virConf *conf,
 {
     int ret = -1;
     char *tmp = NULL;
-    char **token = NULL;
     size_t vcpus = 0;
     size_t nr_nodes = 0;
     size_t vnodeCnt = 0;
@@ -506,6 +499,7 @@ xenParseXLVnuma(virConf *conf,
                         vcpus += virBitmapCountBits(cpumask);
 
                     } else if (STRPREFIX(str, "vdistances")) {
+                        g_auto(GStrv) token = NULL;
                         size_t i, ndistances;
                         unsigned int value;
 
@@ -519,7 +513,6 @@ xenParseXLVnuma(virConf *conf,
                         VIR_FREE(tmp);
                         tmp = g_strdup(vtoken);
 
-                        g_strfreev(token);
                         if (!(token = g_strsplit(tmp, ",", 0)))
                             goto cleanup;
 
@@ -583,7 +576,6 @@ xenParseXLVnuma(virConf *conf,
  cleanup:
     if (ret)
         VIR_FREE(cpu);
-    g_strfreev(token);
     VIR_FREE(tmp);
 
     return ret;
@@ -1301,7 +1293,7 @@ xenFormatXLOS(virConf *conf, virDomainDef *def)
 static int
 xenFormatXLCPUID(virConf *conf, virDomainDef *def)
 {
-    char **cpuid_pairs = NULL;
+    g_auto(GStrv) cpuid_pairs = NULL;
     g_autofree char *cpuid_string = NULL;
     size_t i, j;
     int ret = -1;
@@ -1359,7 +1351,6 @@ xenFormatXLCPUID(virConf *conf, virDomainDef *def)
     ret = 0;
 
  cleanup:
-    g_strfreev(cpuid_pairs);
     return ret;
 }
 
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c
index d788e77196..13f2fd4c29 100644
--- a/src/lxc/lxc_container.c
+++ b/src/lxc/lxc_container.c
@@ -510,7 +510,7 @@ extern int pivot_root(const char * new_root, const char * put_old);
 static int lxcContainerUnmountSubtree(const char *prefix,
                                       bool isOldRootFS)
 {
-    char **mounts = NULL;
+    g_auto(GStrv) mounts = NULL;
     size_t nmounts = 0;
     size_t i;
     int saveErrno;
@@ -555,8 +555,6 @@ static int lxcContainerUnmountSubtree(const char *prefix,
     ret = 0;
 
  cleanup:
-    g_strfreev(mounts);
-
     return ret;
 }
 
diff --git a/src/lxc/lxc_native.c b/src/lxc/lxc_native.c
index 3840652912..f3b8e85143 100644
--- a/src/lxc/lxc_native.c
+++ b/src/lxc/lxc_native.c
@@ -107,8 +107,8 @@ static char ** lxcStringSplit(const char *string)
     g_autofree char *tmp = NULL;
     size_t i;
     size_t ntokens = 0;
-    char **parts;
-    char **result = NULL;
+    g_auto(GStrv) parts = NULL;
+    g_auto(GStrv) result = NULL;
 
     tmp = g_strdup(string);
 
@@ -132,12 +132,9 @@ static char ** lxcStringSplit(const char *string)
         result[ntokens - 2] = g_strdup(parts[i]);
     }
 
-    g_strfreev(parts);
-    return result;
+    return g_steal_pointer(&result);
 
  error:
-    g_strfreev(parts);
-    g_strfreev(result);
     return NULL;
 }
 
@@ -145,7 +142,7 @@ static lxcFstab *
 lxcParseFstabLine(char *fstabLine)
 {
     lxcFstab *fstab = NULL;
-    char **parts;
+    g_auto(GStrv) parts = NULL;
 
     if (!fstabLine)
         return NULL;
@@ -162,13 +159,10 @@ lxcParseFstabLine(char *fstabLine)
     fstab->type = g_strdup(parts[2]);
     fstab->options = g_strdup(parts[3]);
 
-    g_strfreev(parts);
-
     return fstab;
 
  error:
     lxcFstabFree(fstab);
-    g_strfreev(parts);
     return NULL;
 }
 
@@ -252,7 +246,7 @@ lxcAddFstabLine(virDomainDef *def, lxcFstab *fstab)
 {
     const char *src = NULL;
     g_autofree char *dst = NULL;
-    char **options = g_strsplit(fstab->options, ",", 0);
+    g_auto(GStrv) options = g_strsplit(fstab->options, ",", 0);
     bool readonly;
     int type = VIR_DOMAIN_FS_TYPE_MOUNT;
     unsigned long long usage = 0;
@@ -307,7 +301,6 @@ lxcAddFstabLine(virDomainDef *def, lxcFstab *fstab)
     ret = 1;
 
  cleanup:
-    g_strfreev(options);
     return ret;
 }
 
@@ -967,7 +960,7 @@ lxcSetCpusetTune(virDomainDef *def, virConf *properties)
 static int
 lxcBlkioDeviceWalkCallback(const char *name, virConfValue *value, void *data)
 {
-    char **parts = NULL;
+    g_auto(GStrv) parts = NULL;
     virBlkioDevice *device = NULL;
     virDomainDef *def = data;
     size_t i = 0;
@@ -1044,7 +1037,6 @@ lxcBlkioDeviceWalkCallback(const char *name, virConfValue *value, void *data)
     ret = 0;
 
  cleanup:
-    g_strfreev(parts);
     return ret;
 }
 
@@ -1072,7 +1064,7 @@ static void
 lxcSetCapDrop(virDomainDef *def, virConf *properties)
 {
     g_autofree char *value = NULL;
-    char **toDrop = NULL;
+    g_auto(GStrv) toDrop = NULL;
     const char *capString;
     size_t i;
 
@@ -1087,8 +1079,6 @@ lxcSetCapDrop(virDomainDef *def, virConf *properties)
     }
 
     def->features[VIR_DOMAIN_FEATURE_CAPABILITIES] = VIR_DOMAIN_CAPABILITIES_POLICY_ALLOW;
-
-    g_strfreev(toDrop);
 }
 
 virDomainDef *
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 70b5f37e6b..a12ef2227e 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -1329,7 +1329,7 @@ qemuGetSchedInfo(unsigned long long *cpuWait,
 {
     g_autofree char *proc = NULL;
     g_autofree char *data = NULL;
-    char **lines = NULL;
+    g_auto(GStrv) lines = NULL;
     size_t i;
     int ret = -1;
     double val;
@@ -1392,7 +1392,6 @@ qemuGetSchedInfo(unsigned long long *cpuWait,
     ret = 0;
 
  cleanup:
-    g_strfreev(lines);
     return ret;
 }
 
@@ -12517,7 +12516,7 @@ qemuConnectBaselineHypervisorCPU(virConnectPtr conn,
     bool migratable;
     virCPUDef *cpu = NULL;
     char *cpustr = NULL;
-    char **features = NULL;
+    g_auto(GStrv) features = NULL;
 
     virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES |
                   VIR_CONNECT_BASELINE_CPU_MIGRATABLE, NULL);
@@ -12591,7 +12590,6 @@ qemuConnectBaselineHypervisorCPU(virConnectPtr conn,
  cleanup:
     virCPUDefListFree(cpus);
     virCPUDefFree(cpu);
-    g_strfreev(features);
 
     return cpustr;
 }
diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c
index bcfeadc2ae..d8fb02a89d 100644
--- a/src/remote/remote_daemon_dispatch.c
+++ b/src/remote/remote_daemon_dispatch.c
@@ -5824,7 +5824,7 @@ remoteDispatchConnectGetCPUModelNames(virNetServer *server G_GNUC_UNUSED,
                                       remote_connect_get_cpu_model_names_ret *ret)
 {
     int len, rv = -1;
-    char **models = NULL;
+    g_auto(GStrv) models = NULL;
     virConnectPtr conn = remoteGetHypervisorConn(client);
 
     if (!conn)
@@ -5858,7 +5858,6 @@ remoteDispatchConnectGetCPUModelNames(virNetServer *server G_GNUC_UNUSED,
  cleanup:
     if (rv < 0)
         virNetMessageSaveError(rerr);
-    g_strfreev(models);
     return rv;
 }
 
diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index 719fcf4297..8f094db68c 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -6266,7 +6266,7 @@ remoteConnectGetCPUModelNames(virConnectPtr conn,
 {
     int rv = -1;
     size_t i;
-    char **retmodels = NULL;
+    g_auto(GStrv) retmodels = NULL;
     remote_connect_get_cpu_model_names_args args;
     remote_connect_get_cpu_model_names_ret ret;
 
@@ -6307,8 +6307,6 @@ remoteConnectGetCPUModelNames(virConnectPtr conn,
     rv = ret.ret;
 
  cleanup:
-    g_strfreev(retmodels);
-
     xdr_free((xdrproc_t) xdr_remote_connect_get_cpu_model_names_ret, (char *) &ret);
 
  done:
diff --git a/src/storage/storage_backend_rbd.c b/src/storage/storage_backend_rbd.c
index 28b4b7fae6..8276ce20ab 100644
--- a/src/storage/storage_backend_rbd.c
+++ b/src/storage/storage_backend_rbd.c
@@ -659,7 +659,7 @@ virStorageBackendRBDRefreshPool(virStoragePoolObj *pool)
     virStorageBackendRBDState *ptr = NULL;
     struct rados_cluster_stat_t clusterstat;
     struct rados_pool_stat_t poolstat;
-    char **names = NULL;
+    g_auto(GStrv) names = NULL;
     size_t i;
 
     if (!(ptr = virStorageBackendRBDNewState(pool)))
@@ -724,7 +724,6 @@ virStorageBackendRBDRefreshPool(virStoragePoolObj *pool)
     ret = 0;
 
  cleanup:
-    g_strfreev(names);
     virStorageBackendRBDFreeState(&ptr);
     return ret;
 }
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 37b63a2e2d..6234095827 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -851,7 +851,7 @@ virCgroupAddThread(virCgroup *group,
 static int
 virCgroupSetPartitionSuffix(const char *path, char **res)
 {
-    char **tokens;
+    g_auto(GStrv) tokens = NULL;
     size_t i;
     int ret = -1;
 
@@ -887,7 +887,6 @@ virCgroupSetPartitionSuffix(const char *path, char **res)
     ret = 0;
 
  cleanup:
-    g_strfreev(tokens);
     return ret;
 }
 
diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
index 8881d3a88a..4c110940cf 100644
--- a/src/util/vircgroupv2.c
+++ b/src/util/vircgroupv2.c
@@ -264,7 +264,7 @@ virCgroupV2ParseControllersFile(virCgroup *group,
     int rc;
     g_autofree char *contStr = NULL;
     g_autofree char *contFile = NULL;
-    char **contList = NULL;
+    g_auto(GStrv) contList = NULL;
     char **tmp;
 
     if (parent) {
@@ -300,8 +300,6 @@ virCgroupV2ParseControllersFile(virCgroup *group,
         tmp++;
     }
 
-    g_strfreev(contList);
-
     return 0;
 }
 
diff --git a/src/util/virfirmware.c b/src/util/virfirmware.c
index 258d13bb73..cc5e315b7a 100644
--- a/src/util/virfirmware.c
+++ b/src/util/virfirmware.c
@@ -59,7 +59,7 @@ int
 virFirmwareParse(const char *str, virFirmware *firmware)
 {
     int ret = -1;
-    char **token;
+    g_auto(GStrv) token = NULL;
 
     if (!(token = g_strsplit(str, ":", 0)))
         goto cleanup;
@@ -84,7 +84,6 @@ virFirmwareParse(const char *str, virFirmware *firmware)
 
     ret = 0;
  cleanup:
-    g_strfreev(token);
     return ret;
 }
 
@@ -95,7 +94,7 @@ virFirmwareParseList(const char *list,
                      size_t *nfirmwares)
 {
     int ret = -1;
-    char **token;
+    g_auto(GStrv) token = NULL;
     size_t i, j;
 
     if (!(token = g_strsplit(list, ":", 0)))
@@ -126,6 +125,5 @@ virFirmwareParseList(const char *list,
 
     ret = 0;
  cleanup:
-    g_strfreev(token);
     return ret;
 }
diff --git a/src/util/viruri.c b/src/util/viruri.c
index 252e4f598e..3c73188a55 100644
--- a/src/util/viruri.c
+++ b/src/util/viruri.c
@@ -349,7 +349,7 @@ int
 virURIResolveAlias(virConf *conf, const char *alias, char **uri)
 {
     int ret = -1;
-    char **aliases = NULL;
+    g_auto(GStrv) aliases = NULL;
 
     *uri = NULL;
 
@@ -358,7 +358,6 @@ virURIResolveAlias(virConf *conf, const char *alias, char **uri)
 
     if (aliases && *aliases) {
         ret = virURIFindAliasMatch(aliases, alias, uri);
-        g_strfreev(aliases);
     } else {
         ret = 0;
     }
diff --git a/src/vbox/vbox_common.c b/src/vbox/vbox_common.c
index 45e7225ae1..cc90e634a3 100644
--- a/src/vbox/vbox_common.c
+++ b/src/vbox/vbox_common.c
@@ -4559,16 +4559,16 @@ vboxSnapshotRedefine(virDomainPtr dom,
     char *currentSnapshotXmlFilePath = NULL;
     PRUnichar *machineNameUtf16 = NULL;
     char *machineName = NULL;
-    char **realReadWriteDisksPath = NULL;
+    g_auto(GStrv) realReadWriteDisksPath = NULL;
     int realReadWriteDisksPathSize = 0;
-    char **realReadOnlyDisksPath = NULL;
+    g_auto(GStrv) realReadOnlyDisksPath = NULL;
     int realReadOnlyDisksPathSize = 0;
     virVBoxSnapshotConfSnapshot *newSnapshotPtr = NULL;
     unsigned char snapshotUuid[VIR_UUID_BUFLEN];
     virVBoxSnapshotConfHardDisk **hardDiskToOpen = NULL;
     size_t hardDiskToOpenSize = 0;
     virVBoxSnapshotConfHardDisk *newHardDisk = NULL;
-    char **searchResultTab = NULL;
+    g_auto(GStrv) searchResultTab = NULL;
     ssize_t resultSize = 0;
     int it = 0;
     int jt = 0;
@@ -5371,9 +5371,6 @@ vboxSnapshotRedefine(virDomainPtr dom,
     VIR_FREE(currentSnapshotXmlFilePath);
     VBOX_UTF16_FREE(machineNameUtf16);
     VBOX_UTF8_FREE(machineName);
-    g_strfreev(realReadOnlyDisksPath);
-    g_strfreev(realReadWriteDisksPath);
-    g_strfreev(searchResultTab);
     virVboxSnapshotConfHardDiskFree(newHardDisk);
     VIR_FREE(hardDiskToOpen);
     VIR_FREE(newSnapshotPtr);
@@ -6792,7 +6789,7 @@ vboxDomainSnapshotDeleteMetadataOnly(virDomainSnapshotPtr snapshot)
     char *settingsFilepath = NULL;
     virVBoxSnapshotConfMachine *snapshotMachineDesc = NULL;
     int isCurrent = -1;
-    char **searchResultTab = NULL;
+    g_auto(GStrv) searchResultTab = NULL;
     ssize_t resultSize = 0;
     int it = 0;
     PRUnichar *machineNameUtf16 = NULL;
@@ -7195,7 +7192,6 @@ vboxDomainSnapshotDeleteMetadataOnly(virDomainSnapshotPtr snapshot)
     VBOX_RELEASE(machine);
     VBOX_UTF16_FREE(settingsFilePathUtf16);
     VBOX_UTF8_FREE(settingsFilepath);
-    g_strfreev(searchResultTab);
     VIR_FREE(snapshotMachineDesc);
     VBOX_UTF16_FREE(machineNameUtf16);
     VBOX_UTF8_FREE(machineName);
diff --git a/src/vbox/vbox_snapshot_conf.c b/src/vbox/vbox_snapshot_conf.c
index 65546b785e..5894ee1cac 100644
--- a/src/vbox/vbox_snapshot_conf.c
+++ b/src/vbox/vbox_snapshot_conf.c
@@ -38,7 +38,7 @@ virVBoxSnapshotConfCreateVBoxSnapshotConfHardDiskPtr(xmlNodePtr diskNode,
     virVBoxSnapshotConfHardDisk *hardDisk = NULL;
     xmlNodePtr *nodes = NULL;
     char *uuid = NULL;
-    char **searchTabResult = NULL;
+    g_auto(GStrv) searchTabResult = NULL;
     int resultSize = 0;
     size_t i = 0;
     int result = -1;
@@ -103,7 +103,6 @@ virVBoxSnapshotConfCreateVBoxSnapshotConfHardDiskPtr(xmlNodePtr diskNode,
     VIR_FREE(nodes);
     VIR_FREE(location);
     VIR_FREE(tmp);
-    g_strfreev(searchTabResult);
     if (result < 0) {
         virVboxSnapshotConfHardDiskFree(hardDisk);
         hardDisk = NULL;
@@ -184,7 +183,7 @@ virVBoxSnapshotConfRetrieveSnapshot(xmlNodePtr snapshotNode,
     xmlNodePtr snapshotsNode = NULL;
     xmlNodePtr *nodes = NULL;
     char *uuid = NULL;
-    char **searchTabResult = NULL;
+    g_auto(GStrv) searchTabResult = NULL;
     int resultSize = 0;
     size_t i = 0;
     int result = -1;
@@ -270,7 +269,6 @@ virVBoxSnapshotConfRetrieveSnapshot(xmlNodePtr snapshotNode,
     }
     VIR_FREE(nodes);
     VIR_FREE(uuid);
-    g_strfreev(searchTabResult);
     return snapshot;
 }
 
@@ -371,9 +369,9 @@ virVBoxSnapshotConfSerializeSnapshot(xmlNodePtr node,
     char *uuid = NULL;
     char *timeStamp = NULL;
 
-    char **firstRegex = NULL;
+    g_auto(GStrv) firstRegex = NULL;
     int firstRegexResult = 0;
-    char **secondRegex = NULL;
+    g_auto(GStrv) secondRegex = NULL;
     int secondRegexResult = 0;
 
     uuid = g_strdup_printf("{%s}", snapshot->uuid);
@@ -450,8 +448,6 @@ virVBoxSnapshotConfSerializeSnapshot(xmlNodePtr node,
         xmlUnlinkNode(snapshotsNode);
         xmlFreeNode(snapshotsNode);
     }
-    g_strfreev(firstRegex);
-    g_strfreev(secondRegex);
     VIR_FREE(uuid);
     VIR_FREE(timeStamp);
     return result;
@@ -581,7 +577,7 @@ virVBoxSnapshotConfLoadVboxFile(const char *filePath,
     g_autoptr(xmlXPathContext) xPathContext = NULL;
     char *currentStateModifiedString = NULL;
 
-    char **searchResultTab = NULL;
+    g_auto(GStrv) searchResultTab = NULL;
     ssize_t searchResultSize = 0;
     char *currentSnapshotAttribute = NULL;
 
@@ -719,7 +715,6 @@ virVBoxSnapshotConfLoadVboxFile(const char *filePath,
 
     VIR_FREE(currentStateModifiedString);
     VIR_FREE(currentSnapshotAttribute);
-    g_strfreev(searchResultTab);
     if (ret < 0) {
         virVBoxSnapshotConfMachineFree(machineDescription);
         machineDescription = NULL;
@@ -963,9 +958,9 @@ virVBoxSnapshotConfSaveVboxFile(virVBoxSnapshotConfMachine *machine,
     char *currentSnapshot = NULL;
     char *timeStamp = NULL;
 
-    char **firstRegex = NULL;
+    g_auto(GStrv) firstRegex = NULL;
     int firstRegexResult = 0;
-    char **secondRegex = NULL;
+    g_auto(GStrv) secondRegex = NULL;
     int secondRegexResult = 0;
 
     if (machine == NULL) {
@@ -1171,9 +1166,6 @@ virVBoxSnapshotConfSaveVboxFile(virVBoxSnapshotConfMachine *machine,
     xmlUnlinkNode(machineNode);
     xmlFreeNode(machineNode);
 
-
-    g_strfreev(firstRegex);
-    g_strfreev(secondRegex);
     return ret;
 }
 
@@ -1216,7 +1208,7 @@ virVBoxSnapshotConfGetRWDisksPathsFromLibvirtXML(const char *filePath,
 {
     int result = -1;
     size_t i = 0;
-    char **ret = NULL;
+    g_auto(GStrv) ret = NULL;
     g_autoptr(xmlDoc) xml = NULL;
     g_autoptr(xmlXPathContext) xPathContext = NULL;
     xmlNodePtr *nodes = NULL;
@@ -1253,15 +1245,12 @@ virVBoxSnapshotConfGetRWDisksPathsFromLibvirtXML(const char *filePath,
         if (sourceNode)
             ret[i] = virXMLPropString(sourceNode, "file");
     }
+    *rwDisksPath = g_steal_pointer(&ret);
     result = 0;
 
  cleanup:
-    if (result < 0) {
-        g_strfreev(ret);
+    if (result < 0)
         nodeSize = -1;
-    } else {
-        *rwDisksPath = ret;
-    }
     VIR_FREE(nodes);
     return nodeSize;
 }
@@ -1277,7 +1266,7 @@ virVBoxSnapshotConfGetRODisksPathsFromLibvirtXML(const char *filePath,
 {
     int result = -1;
     size_t i = 0;
-    char **ret = NULL;
+    g_auto(GStrv) ret = NULL;
     g_autoptr(xmlDoc) xml = NULL;
     g_autoptr(xmlXPathContext) xPathContext = NULL;
     xmlNodePtr *nodes = NULL;
@@ -1313,15 +1302,12 @@ virVBoxSnapshotConfGetRODisksPathsFromLibvirtXML(const char *filePath,
         if (sourceNode)
             ret[i] = virXMLPropString(sourceNode, "file");
     }
+    *roDisksPath = g_steal_pointer(&ret);
     result = 0;
 
  cleanup:
-    if (result < 0) {
-        g_strfreev(ret);
+    if (result < 0)
         nodeSize = -1;
-    } else {
-        *roDisksPath = ret;
-    }
     VIR_FREE(nodes);
     return nodeSize;
 }
diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c
index 1772a9b70e..7344882bbd 100644
--- a/src/vbox/vbox_tmpl.c
+++ b/src/vbox/vbox_tmpl.c
@@ -1548,7 +1548,7 @@ _vrdeServerGetPorts(struct _vboxDriver *data, IVRDEServer *VRDEServer,
     PRUnichar *VRDEPortsValue = NULL;
     PRInt32 port = -1;
     ssize_t nmatches = 0;
-    char **matches = NULL;
+    g_auto(GStrv) matches = NULL;
     char *portUtf8 = NULL;
 
     /* get active (effective) port - available only when VM is running and has
@@ -1596,7 +1596,6 @@ _vrdeServerGetPorts(struct _vboxDriver *data, IVRDEServer *VRDEServer,
     }
 
  cleanup:
-    g_strfreev(matches);
     VBOX_UTF8_FREE(portUtf8);
     VBOX_UTF16_FREE(VRDEPortsValue);
     VBOX_UTF16_FREE(VRDEPortsKey);
diff --git a/src/vz/vz_sdk.c b/src/vz/vz_sdk.c
index 2ba48134b0..1772f75c3e 100644
--- a/src/vz/vz_sdk.c
+++ b/src/vz/vz_sdk.c
@@ -705,7 +705,7 @@ prlsdkGetFSInfo(PRL_HANDLE prldisk,
 {
     char *buf = NULL;
     int ret = -1;
-    char **matches = NULL;
+    g_auto(GStrv) matches = NULL;
     virURI *uri = NULL;
 
     fs->type = VIR_DOMAIN_FS_TYPE_FILE;
@@ -767,7 +767,6 @@ prlsdkGetFSInfo(PRL_HANDLE prldisk,
 
  cleanup:
     VIR_FREE(buf);
-    g_strfreev(matches);
     return ret;
 }
 
diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c
index e5ba39cd2f..25a91cdeb3 100644
--- a/tests/qemumonitorjsontest.c
+++ b/tests/qemumonitorjsontest.c
@@ -1003,7 +1003,7 @@ testQemuMonitorJSONGetDeviceAliases(const void *opaque)
     const testGenericData *data = opaque;
     virDomainXMLOption *xmlopt = data->xmlopt;
     int ret = -1;
-    char **aliases = NULL;
+    g_auto(GStrv) aliases = NULL;
     const char **alias;
     const char *expected[] = {
         "virtio-disk25", "video0", "serial0", "ide0-0-0", "usb", NULL };
@@ -1053,7 +1053,6 @@ testQemuMonitorJSONGetDeviceAliases(const void *opaque)
     }
 
  cleanup:
-    g_strfreev(aliases);
     return ret;
 }
 
diff --git a/tests/vboxsnapshotxmltest.c b/tests/vboxsnapshotxmltest.c
index da39561456..d69eb3fc24 100644
--- a/tests/vboxsnapshotxmltest.c
+++ b/tests/vboxsnapshotxmltest.c
@@ -19,7 +19,7 @@ static char *
 testFilterXML(char *xml)
 {
     g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
-    char **xmlLines = NULL;
+    g_auto(GStrv) xmlLines = NULL;
     char **xmlLine;
     char *ret = NULL;
 
@@ -39,7 +39,6 @@ testFilterXML(char *xml)
     ret = virBufferContentAndReset(&buf);
 
  cleanup:
-   g_strfreev(xmlLines);
    return ret;
 }
 
diff --git a/tests/virconftest.c b/tests/virconftest.c
index 5d1ad8a612..32d3d8849b 100644
--- a/tests/virconftest.c
+++ b/tests/virconftest.c
@@ -372,7 +372,7 @@ static int testConfParseStringList(const void *opaque G_GNUC_UNUSED)
 
     int ret = -1;
     g_autoptr(virConf) conf = virConfReadString(srcdata, 0);
-    char **str = NULL;
+    g_auto(GStrv) str = NULL;
 
     if (!conf)
         return -1;
@@ -423,7 +423,6 @@ static int testConfParseStringList(const void *opaque G_GNUC_UNUSED)
 
     ret = 0;
  cleanup:
-    g_strfreev(str);
     return ret;
 }
 
diff --git a/tests/virfiletest.c b/tests/virfiletest.c
index 1434d6b7ba..2fbece8f63 100644
--- a/tests/virfiletest.c
+++ b/tests/virfiletest.c
@@ -65,7 +65,7 @@ struct testFileGetMountSubtreeData {
 static int testFileGetMountSubtree(const void *opaque)
 {
     int ret = -1;
-    char **gotmounts = NULL;
+    g_auto(GStrv) gotmounts = NULL;
     size_t gotnmounts = 0;
     const struct testFileGetMountSubtreeData *data = opaque;
 
@@ -88,7 +88,6 @@ static int testFileGetMountSubtree(const void *opaque)
                               data->mounts, data->nmounts);
 
  cleanup:
-    g_strfreev(gotmounts);
     return ret;
 }
 #endif /* ! defined WITH_MNTENT_H && defined WITH_GETMNTENT_R */
diff --git a/tests/virstringtest.c b/tests/virstringtest.c
index 277deca934..77fcec5613 100644
--- a/tests/virstringtest.c
+++ b/tests/virstringtest.c
@@ -127,7 +127,7 @@ static int
 testStringSearch(const void *opaque)
 {
     const struct stringSearchData *data = opaque;
-    char **matches = NULL;
+    g_auto(GStrv) matches = NULL;
     ssize_t nmatches;
     int ret = -1;
 
@@ -174,7 +174,6 @@ testStringSearch(const void *opaque)
     ret = 0;
 
  cleanup:
-    g_strfreev(matches);
     return ret;
 }
 
diff --git a/tools/virsh-host.c b/tools/virsh-host.c
index 2a84c58be7..f6aa532b40 100644
--- a/tools/virsh-host.c
+++ b/tools/virsh-host.c
@@ -1059,7 +1059,7 @@ static char **
 vshExtractCPUDefXMLs(vshControl *ctl,
                      const char *xmlFile)
 {
-    char **cpus = NULL;
+    g_auto(GStrv) cpus = NULL;
     g_autofree char *buffer = NULL;
     g_autofree char *xmlStr = NULL;
     g_autoptr(xmlDoc) xml = NULL;
@@ -1126,7 +1126,6 @@ vshExtractCPUDefXMLs(vshControl *ctl,
     return cpus;
 
  error:
-    g_strfreev(cpus);
     goto cleanup;
 }
 
@@ -1163,7 +1162,7 @@ cmdCPUCompare(vshControl *ctl, const vshCmd *cmd)
     const char *from = NULL;
     bool ret = false;
     int result;
-    char **cpus = NULL;
+    g_auto(GStrv) cpus = NULL;
     unsigned int flags = 0;
     virshControl *priv = ctl->privData;
 
@@ -1207,8 +1206,6 @@ cmdCPUCompare(vshControl *ctl, const vshCmd *cmd)
     ret = true;
 
  cleanup:
-    g_strfreev(cpus);
-
     return ret;
 }
 
@@ -1585,7 +1582,7 @@ cmdHypervisorCPUCompare(vshControl *ctl,
     const char *machine = NULL;
     bool ret = false;
     int result;
-    char **cpus = NULL;
+    g_auto(GStrv) cpus = NULL;
     unsigned int flags = 0;
     virshControl *priv = ctl->privData;
 
@@ -1640,7 +1637,6 @@ cmdHypervisorCPUCompare(vshControl *ctl,
     ret = true;
 
  cleanup:
-    g_strfreev(cpus);
     return ret;
 }
 
@@ -1699,7 +1695,7 @@ cmdHypervisorCPUBaseline(vshControl *ctl,
     const char *machine = NULL;
     bool ret = false;
     g_autofree char *result = NULL;
-    char **list = NULL;
+    g_auto(GStrv) list = NULL;
     unsigned int flags = 0;
     virshControl *priv = ctl->privData;
 
@@ -1729,7 +1725,6 @@ cmdHypervisorCPUBaseline(vshControl *ctl,
         ret = true;
     }
 
-    g_strfreev(list);
     return ret;
 }
 
diff --git a/tools/virt-login-shell-helper.c b/tools/virt-login-shell-helper.c
index 5c6e007b09..0fb03da697 100644
--- a/tools/virt-login-shell-helper.c
+++ b/tools/virt-login-shell-helper.c
@@ -47,7 +47,8 @@ static int virLoginShellAllowedUser(virConf *conf,
     int ret = -1;
     size_t i;
     char *gname = NULL;
-    char **users = NULL, **entries;
+    g_auto(GStrv) users = NULL;
+    char **entries;
 
     if (virConfGetValueStringList(conf, "allowed_users", false, &users) < 0)
         goto cleanup;
@@ -84,7 +85,6 @@ static int virLoginShellAllowedUser(virConf *conf,
                          name, conf_file);
  cleanup:
     VIR_FREE(gname);
-    g_strfreev(users);
     return ret;
 }
 
@@ -157,7 +157,7 @@ main(int argc, char **argv)
     uid_t uid;
     gid_t gid;
     char *name = NULL;
-    char **shargv = NULL;
+    g_auto(GStrv) shargv = NULL;
     size_t shargvlen = 0;
     char *shcmd = NULL;
     virSecurityModelPtr secmodel = NULL;
@@ -403,7 +403,6 @@ main(int argc, char **argv)
         virDomainFree(dom);
     if (conn)
         virConnectClose(conn);
-    g_strfreev(shargv);
     VIR_FREE(shcmd);
     VIR_FREE(term);
     VIR_FREE(name);
diff --git a/tools/vsh.c b/tools/vsh.c
index ecb591fb63..e3e27a0ba6 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -3365,7 +3365,8 @@ cmdComplete(vshControl *ctl, const vshCmd *cmd)
     int stdin_fileno = STDIN_FILENO;
     const char *arg = "";
     const vshCmdOpt *opt = NULL;
-    char **matches = NULL, **iter;
+    g_auto(GStrv) matches = NULL;
+    char **iter;
     g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
 
     if (vshCommandOptStringQuiet(ctl, cmd, "string", &arg) <= 0)
@@ -3406,7 +3407,6 @@ cmdComplete(vshControl *ctl, const vshCmd *cmd)
 
     ret = true;
  cleanup:
-    g_strfreev(matches);
     return ret;
 }
 
-- 
2.32.0




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux