[libvirt] [PATCH 66/75] tools: Use g_strdup_printf() instead of virAsprintf()

Michal Privoznik mprivozn at redhat.com
Tue Oct 22 13:58:10 UTC 2019


Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 tools/virsh-completer-domain.c |  6 +++---
 tools/virsh-completer-host.c   | 10 +++++-----
 tools/virsh-completer.c        |  2 +-
 tools/virsh-domain-monitor.c   | 18 +++++++++---------
 tools/virsh-domain.c           | 22 +++++++++++-----------
 tools/virsh-host.c             |  2 +-
 tools/virsh-interface.c        |  2 +-
 tools/virsh-network.c          |  2 +-
 tools/virsh-pool.c             | 12 ++++++------
 tools/virsh-volume.c           |  6 +++---
 tools/virt-admin.c             |  4 ++--
 tools/vsh.c                    | 22 +++++++++++-----------
 12 files changed, 54 insertions(+), 54 deletions(-)

diff --git a/tools/virsh-completer-domain.c b/tools/virsh-completer-domain.c
index 0bf6202c0a..d625ab8bb4 100644
--- a/tools/virsh-completer-domain.c
+++ b/tools/virsh-completer-domain.c
@@ -221,9 +221,9 @@ virshDomainInterfaceStateCompleter(vshControl *ctl,
     if (virMacAddrParse(iface, &macaddr) == 0)
         virMacAddrFormat(&macaddr, macstr);
 
-    virAsprintf(&xpath, "/domain/devices/interface[(mac/@address = '%s') or "
-                "                          (target/@dev = '%s')]", macstr,
-                iface);
+    xpath = g_strdup_printf("/domain/devices/interface[(mac/@address = '%s') or "
+                            "                          (target/@dev = '%s')]", macstr,
+                            iface);
 
     if ((ninterfaces = virXPathNodeSet(xpath, ctxt, &interfaces)) < 0)
         return NULL;
diff --git a/tools/virsh-completer-host.c b/tools/virsh-completer-host.c
index 8cb98ad88d..41615226d2 100644
--- a/tools/virsh-completer-host.c
+++ b/tools/virsh-completer-host.c
@@ -44,7 +44,7 @@ virshPagesizeNodeToString(xmlNodePtr node)
     if (virScaleInteger(&byteval, unit, 1024, UINT_MAX) < 0)
         return NULL;
     size = vshPrettyCapacity(byteval, &suffix);
-    virAsprintf(&ret, "%.0f%s", size, suffix);
+    ret = g_strdup_printf("%.0f%s", size, suffix);
     return ret;
 }
 
@@ -78,11 +78,11 @@ virshAllocpagesPagesizeCompleter(vshControl *ctl,
         return NULL;
 
     if (cellno && vshCommandOptStringQuiet(ctl, cmd, "cellno", &cellnum) > 0) {
-        virAsprintf(&path,
-                    "/capabilities/host/topology/cells/cell[@id=\"%s\"]/pages",
-                    cellnum);
+        path = g_strdup_printf(
+                               "/capabilities/host/topology/cells/cell[@id=\"%s\"]/pages",
+                               cellnum);
     } else {
-        virAsprintf(&path, "/capabilities/host/cpu/pages");
+        path = g_strdup_printf("/capabilities/host/cpu/pages");
     }
 
     npages = virXPathNodeSet(path, ctxt, &pages);
diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c
index 8bd7be888f..1d9d212f8a 100644
--- a/tools/virsh-completer.c
+++ b/tools/virsh-completer.c
@@ -119,7 +119,7 @@ virshCommaStringListComplete(const char *input,
             continue;
 
         if (inputCopy)
-            virAsprintf(&ret[nret], "%s,%s", inputCopy, options[i]);
+            ret[nret] = g_strdup_printf("%s,%s", inputCopy, options[i]);
         else
             ret[nret] = g_strdup(options[i]);
 
diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 9178e534d6..530bfeca1b 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -431,9 +431,9 @@ cmdDomblkinfoGet(const virDomainBlockInfo *info,
         *alloc = g_strdup("-");
         *phy = g_strdup("-");
     } else if (!human) {
-        virAsprintf(cap, "%llu", info->capacity);
-        virAsprintf(alloc, "%llu", info->allocation);
-        virAsprintf(phy, "%llu", info->physical);
+        *cap = g_strdup_printf("%llu", info->capacity);
+        *alloc = g_strdup_printf("%llu", info->allocation);
+        *phy = g_strdup_printf("%llu", info->physical);
     } else {
         double val_cap, val_alloc, val_phy;
         const char *unit_cap, *unit_alloc, *unit_phy;
@@ -442,9 +442,9 @@ cmdDomblkinfoGet(const virDomainBlockInfo *info,
         val_alloc = vshPrettyCapacity(info->allocation, &unit_alloc);
         val_phy = vshPrettyCapacity(info->physical, &unit_phy);
 
-        virAsprintf(cap, "%.3lf %s", val_cap, unit_cap);
-        virAsprintf(alloc, "%.3lf %s", val_alloc, unit_alloc);
-        virAsprintf(phy, "%.3lf %s", val_phy, unit_phy);
+        *cap = g_strdup_printf("%.3lf %s", val_cap, unit_cap);
+        *alloc = g_strdup_printf("%.3lf %s", val_alloc, unit_alloc);
+        *phy = g_strdup_printf("%.3lf %s", val_phy, unit_phy);
     }
 
     return true;
@@ -826,9 +826,9 @@ cmdDomIfGetLink(vshControl *ctl, const vshCmd *cmd)
     if (virMacAddrParse(iface, &macaddr) == 0)
         virMacAddrFormat(&macaddr, macstr);
 
-    virAsprintf(&xpath, "/domain/devices/interface[(mac/@address = '%s') or "
-                "                          (target/@dev = '%s')]", macstr,
-                iface);
+    xpath = g_strdup_printf("/domain/devices/interface[(mac/@address = '%s') or "
+                            "                          (target/@dev = '%s')]", macstr,
+                            iface);
 
     if ((ninterfaces = virXPathNodeSet(xpath, ctxt, &interfaces)) < 0) {
         vshError(ctl, _("Failed to extract interface information"));
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 7a03c1fb6e..3a7015b198 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -5535,8 +5535,8 @@ virshGenFileName(vshControl *ctl, virDomainPtr dom, const char *mime)
     localtime_r(&cur_time, &time_info);
     strftime(timestr, sizeof(timestr), "%Y-%m-%d-%H:%M:%S", &time_info);
 
-    virAsprintf(&ret, "%s-%s%s", virDomainGetName(dom), timestr,
-                NULLSTR_EMPTY(ext));
+    ret = g_strdup_printf("%s-%s%s", virDomainGetName(dom), timestr,
+                          NULLSTR_EMPTY(ext));
 
     return ret;
 }
@@ -6966,7 +6966,7 @@ virshVcpuPinQuery(vshControl *ctl,
                                                 cpumaplen)))
                 goto cleanup;
 
-            virAsprintf(&vcpuStr, "%zu", i);
+            vcpuStr = g_strdup_printf("%zu", i);
 
             if (vshTableRowAppend(table, vcpuStr, pinInfo, NULL) < 0)
                 goto cleanup;
@@ -7582,7 +7582,7 @@ cmdIOThreadInfo(vshControl *ctl, const vshCmd *cmd)
         g_autofree char *pinInfo = NULL;
         g_autofree char *iothreadIdStr = NULL;
 
-        virAsprintf(&iothreadIdStr, "%u", info[i]->iothread_id);
+        iothreadIdStr = g_strdup_printf("%u", info[i]->iothread_id);
 
         ignore_value(pinInfo = virBitmapDataFormat(info[i]->cpumap, info[i]->cpumaplen));
 
@@ -11386,7 +11386,7 @@ cmdDomDisplay(vshControl *ctl, const vshCmd *cmd)
 
         /* Create our XPATH lookup for the current display's port */
         VIR_FREE(xpath);
-        virAsprintf(&xpath, xpath_fmt, scheme[iter], "@port");
+        xpath = g_strdup_printf(xpath_fmt, scheme[iter], "@port");
 
         /* Attempt to get the port number for the current graphics scheme */
         tmp = virXPathInt(xpath, ctxt, &port);
@@ -11399,7 +11399,7 @@ cmdDomDisplay(vshControl *ctl, const vshCmd *cmd)
 
         /* Create our XPATH lookup for TLS Port (automatically skipped
          * for unsupported schemes */
-        virAsprintf(&xpath, xpath_fmt, scheme[iter], "@tlsPort");
+        xpath = g_strdup_printf(xpath_fmt, scheme[iter], "@tlsPort");
 
         /* Attempt to get the TLS port number */
         tmp = virXPathInt(xpath, ctxt, &tls_port);
@@ -11408,7 +11408,7 @@ cmdDomDisplay(vshControl *ctl, const vshCmd *cmd)
             tls_port = 0;
 
         /* Create our XPATH lookup for the current display's address */
-        virAsprintf(&xpath, xpath_fmt, scheme[iter], "@listen");
+        xpath = g_strdup_printf(xpath_fmt, scheme[iter], "@listen");
 
         /* Attempt to get the listening addr if set for the current
          * graphics scheme */
@@ -11417,7 +11417,7 @@ cmdDomDisplay(vshControl *ctl, const vshCmd *cmd)
         VIR_FREE(xpath);
 
         /* Create our XPATH lookup for the current spice type. */
-        virAsprintf(&xpath, xpath_fmt, scheme[iter], "listen/@type");
+        xpath = g_strdup_printf(xpath_fmt, scheme[iter], "listen/@type");
 
         /* Attempt to get the type of spice connection */
         VIR_FREE(type_conn);
@@ -11426,7 +11426,7 @@ cmdDomDisplay(vshControl *ctl, const vshCmd *cmd)
 
         if (STREQ_NULLABLE(type_conn, "socket")) {
             if (!sockpath) {
-                virAsprintf(&xpath, xpath_fmt, scheme[iter], "listen/@socket");
+                xpath = g_strdup_printf(xpath_fmt, scheme[iter], "listen/@socket");
 
                 sockpath = virXPathString(xpath, ctxt);
 
@@ -11446,7 +11446,7 @@ cmdDomDisplay(vshControl *ctl, const vshCmd *cmd)
              * subelement (which, by the way, doesn't exist on libvirt
              * < 0.9.4, so we really do need to check both places)
              */
-            virAsprintf(&xpath, xpath_fmt, scheme[iter], "listen/@address");
+            xpath = g_strdup_printf(xpath_fmt, scheme[iter], "listen/@address");
 
             listen_addr = virXPathString(xpath, ctxt);
             VIR_FREE(xpath);
@@ -11482,7 +11482,7 @@ cmdDomDisplay(vshControl *ctl, const vshCmd *cmd)
          * care of when getting the XML */
 
         /* Create our XPATH lookup for the password */
-        virAsprintf(&xpath, xpath_fmt, scheme[iter], "@passwd");
+        xpath = g_strdup_printf(xpath_fmt, scheme[iter], "@passwd");
 
         /* Attempt to get the password */
         VIR_FREE(passwd);
diff --git a/tools/virsh-host.c b/tools/virsh-host.c
index fe172d2c29..0fd77cbae5 100644
--- a/tools/virsh-host.c
+++ b/tools/virsh-host.c
@@ -1140,7 +1140,7 @@ vshExtractCPUDefXMLs(vshControl *ctl,
     else
         doc = buffer;
 
-    virAsprintf(&xmlStr, "<container>%s</container>", doc);
+    xmlStr = g_strdup_printf("<container>%s</container>", doc);
 
     if (!(xml = virXMLParseStringCtxt(xmlStr, xmlFile, &ctxt)))
         goto error;
diff --git a/tools/virsh-interface.c b/tools/virsh-interface.c
index fbeb40fc6a..8aa2fcf71d 100644
--- a/tools/virsh-interface.c
+++ b/tools/virsh-interface.c
@@ -894,7 +894,7 @@ cmdInterfaceBridge(vshControl *ctl, const vshCmd *cmd)
     }
 
     if (stp) {
-        virAsprintf(&delay_str, "%d", delay);
+        delay_str = g_strdup_printf("%d", delay);
         if (!xmlSetProp(br_node, BAD_CAST "delay", BAD_CAST delay_str)) {
             vshError(ctl, _("Failed to set bridge delay %d in xml document"), delay);
             goto cleanup;
diff --git a/tools/virsh-network.c b/tools/virsh-network.c
index 662ca00e1c..59b7b5d075 100644
--- a/tools/virsh-network.c
+++ b/tools/virsh-network.c
@@ -1447,7 +1447,7 @@ cmdNetworkDHCPLeases(vshControl *ctl, const vshCmd *cmd)
         else if (lease->type == VIR_IP_ADDR_TYPE_IPV6)
             typestr = "ipv6";
 
-        virAsprintf(&cidr_format, "%s/%d", lease->ipaddr, lease->prefix);
+        cidr_format = g_strdup_printf("%s/%d", lease->ipaddr, lease->prefix);
 
         if (vshTableRowAppend(table,
                               expirytime,
diff --git a/tools/virsh-pool.c b/tools/virsh-pool.c
index 5138a5946c..2c39a9b1a6 100644
--- a/tools/virsh-pool.c
+++ b/tools/virsh-pool.c
@@ -1286,16 +1286,16 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
                     const char *unit;
 
                     val = vshPrettyCapacity(info.capacity, &unit);
-                    virAsprintf(&poolInfoTexts[i].capacity, "%.2lf %s", val,
-                                unit);
+                    poolInfoTexts[i].capacity = g_strdup_printf("%.2lf %s", val,
+                                                                unit);
 
                     val = vshPrettyCapacity(info.allocation, &unit);
-                    virAsprintf(&poolInfoTexts[i].allocation, "%.2lf %s", val,
-                                unit);
+                    poolInfoTexts[i].allocation = g_strdup_printf("%.2lf %s", val,
+                                                                  unit);
 
                     val = vshPrettyCapacity(info.available, &unit);
-                    virAsprintf(&poolInfoTexts[i].available, "%.2lf %s", val,
-                                unit);
+                    poolInfoTexts[i].available = g_strdup_printf("%.2lf %s", val,
+                                                                 unit);
                 } else {
                     /* Capacity related information isn't available */
                     poolInfoTexts[i].capacity = g_strdup(_("-"));
diff --git a/tools/virsh-volume.c b/tools/virsh-volume.c
index bd57fd695d..b4afa51d9b 100644
--- a/tools/virsh-volume.c
+++ b/tools/virsh-volume.c
@@ -1430,11 +1430,11 @@ cmdVolList(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
                 volInfoTexts[i].type = g_strdup(virshVolumeTypeToString(volumeInfo.type));
 
                 val = vshPrettyCapacity(volumeInfo.capacity, &unit);
-                virAsprintf(&volInfoTexts[i].capacity, "%.2lf %s", val, unit);
+                volInfoTexts[i].capacity = g_strdup_printf("%.2lf %s", val, unit);
 
                 val = vshPrettyCapacity(volumeInfo.allocation, &unit);
-                virAsprintf(&volInfoTexts[i].allocation, "%.2lf %s", val,
-                            unit);
+                volInfoTexts[i].allocation = g_strdup_printf("%.2lf %s", val,
+                                                             unit);
             }
         }
     }
diff --git a/tools/virt-admin.c b/tools/virt-admin.c
index da28d760db..30106d1971 100644
--- a/tools/virt-admin.c
+++ b/tools/virt-admin.c
@@ -394,7 +394,7 @@ cmdSrvList(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
 
     for (i = 0; i < nsrvs; i++) {
         g_autofree char *idStr = NULL;
-        virAsprintf(&idStr, "%zu", i);
+        idStr = g_strdup_printf("%zu", i);
 
         if (vshTableRowAppend(table,
                               idStr,
@@ -655,7 +655,7 @@ cmdSrvClientsList(vshControl *ctl, const vshCmd *cmd)
                              &timestr) < 0)
             goto cleanup;
 
-        virAsprintf(&idStr, "%llu", id);
+        idStr = g_strdup_printf("%llu", id);
         if (vshTableRowAppend(table, idStr,
                               vshAdmClientTransportToString(transport),
                               timestr, NULL) < 0)
diff --git a/tools/vsh.c b/tools/vsh.c
index 8cd613bbbd..7002aa635e 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -1799,23 +1799,23 @@ vshGetTypedParamValue(vshControl *ctl, virTypedParameterPtr item)
 
     switch (item->type) {
     case VIR_TYPED_PARAM_INT:
-        virAsprintf(&str, "%d", item->value.i);
+        str = g_strdup_printf("%d", item->value.i);
         break;
 
     case VIR_TYPED_PARAM_UINT:
-        virAsprintf(&str, "%u", item->value.ui);
+        str = g_strdup_printf("%u", item->value.ui);
         break;
 
     case VIR_TYPED_PARAM_LLONG:
-        virAsprintf(&str, "%lld", item->value.l);
+        str = g_strdup_printf("%lld", item->value.l);
         break;
 
     case VIR_TYPED_PARAM_ULLONG:
-        virAsprintf(&str, "%llu", item->value.ul);
+        str = g_strdup_printf("%llu", item->value.ul);
         break;
 
     case VIR_TYPED_PARAM_DOUBLE:
-        virAsprintf(&str, "%f", item->value.d);
+        str = g_strdup_printf("%f", item->value.d);
         break;
 
     case VIR_TYPED_PARAM_BOOLEAN:
@@ -2418,7 +2418,7 @@ vshEditWriteToTempFile(vshControl *ctl, const char *doc)
 
     tmpdir = getenv("TMPDIR");
     if (!tmpdir) tmpdir = "/tmp";
-    virAsprintf(&ret, "%s/virshXXXXXX.xml", tmpdir);
+    ret = g_strdup_printf("%s/virshXXXXXX.xml", tmpdir);
     fd = mkostemps(ret, 4, O_CLOEXEC);
     if (fd == -1) {
         vshError(ctl, _("mkostemps: failed to create temporary file: %s"),
@@ -2936,7 +2936,7 @@ vshReadlineInit(vshControl *ctl)
     rl_completer_quote_characters = quote_characters;
     rl_char_is_quoted_p = vshReadlineCharIsQuoted;
 
-    virAsprintf(&histsize_env, "%s_HISTSIZE", ctl->env_prefix);
+    histsize_env = g_strdup_printf("%s_HISTSIZE", ctl->env_prefix);
 
     /* Limit the total size of the history buffer */
     if ((histsize_str = getenv(histsize_env))) {
@@ -2962,9 +2962,9 @@ vshReadlineInit(vshControl *ctl)
         goto cleanup;
     }
 
-    virAsprintf(&ctl->historydir, "%s/%s", userdir, ctl->name);
+    ctl->historydir = g_strdup_printf("%s/%s", userdir, ctl->name);
 
-    virAsprintf(&ctl->historyfile, "%s/history", ctl->historydir);
+    ctl->historyfile = g_strdup_printf("%s/history", ctl->historydir);
 
     read_history(ctl->historyfile);
     ret = 0;
@@ -3046,7 +3046,7 @@ vshInitDebug(vshControl *ctl)
     char *env = NULL;
 
     if (ctl->debug == VSH_DEBUG_DEFAULT) {
-        virAsprintf(&env, "%s_DEBUG", ctl->env_prefix);
+        env = g_strdup_printf("%s_DEBUG", ctl->env_prefix);
 
         /* log level not set from commandline, check env variable */
         debugEnv = getenv(env);
@@ -3064,7 +3064,7 @@ vshInitDebug(vshControl *ctl)
     }
 
     if (ctl->logfile == NULL) {
-        virAsprintf(&env, "%s_LOG_FILE", ctl->env_prefix);
+        env = g_strdup_printf("%s_LOG_FILE", ctl->env_prefix);
 
         /* log file not set from cmdline */
         debugEnv = getenv(env);
-- 
2.21.0




More information about the libvir-list mailing list