tools: Use g_strdup_printf() instead of virAsprintf()

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
This commit is contained in:
Michal Privoznik 2019-10-22 15:26:14 +02:00
parent 4fa804c0c7
commit 26a137093b
12 changed files with 61 additions and 103 deletions

View File

@ -214,10 +214,9 @@ virshDomainInterfaceStateCompleter(vshControl *ctl,
if (virMacAddrParse(iface, &macaddr) == 0) if (virMacAddrParse(iface, &macaddr) == 0)
virMacAddrFormat(&macaddr, macstr); virMacAddrFormat(&macaddr, macstr);
if (virAsprintf(&xpath, "/domain/devices/interface[(mac/@address = '%s') or " xpath = g_strdup_printf("/domain/devices/interface[(mac/@address = '%s') or "
" (target/@dev = '%s')]", " (target/@dev = '%s')]", macstr,
macstr, iface) < 0) iface);
return NULL;
if ((ninterfaces = virXPathNodeSet(xpath, ctxt, &interfaces)) < 0) if ((ninterfaces = virXPathNodeSet(xpath, ctxt, &interfaces)) < 0)
return NULL; return NULL;

View File

@ -44,8 +44,7 @@ virshPagesizeNodeToString(xmlNodePtr node)
if (virScaleInteger(&byteval, unit, 1024, UINT_MAX) < 0) if (virScaleInteger(&byteval, unit, 1024, UINT_MAX) < 0)
return NULL; return NULL;
size = vshPrettyCapacity(byteval, &suffix); size = vshPrettyCapacity(byteval, &suffix);
if (virAsprintf(&ret, "%.0f%s", size, suffix) < 0) ret = g_strdup_printf("%.0f%s", size, suffix);
return NULL;
return ret; return ret;
} }
@ -78,13 +77,10 @@ virshAllocpagesPagesizeCompleter(vshControl *ctl,
return NULL; return NULL;
if (cellno && vshCommandOptStringQuiet(ctl, cmd, "cellno", &cellnum) > 0) { if (cellno && vshCommandOptStringQuiet(ctl, cmd, "cellno", &cellnum) > 0) {
if (virAsprintf(&path, path = g_strdup_printf("/capabilities/host/topology/cells/cell[@id=\"%s\"]/pages",
"/capabilities/host/topology/cells/cell[@id=\"%s\"]/pages", cellnum);
cellnum) < 0)
return NULL;
} else { } else {
if (virAsprintf(&path, "/capabilities/host/cpu/pages") < 0) path = g_strdup("/capabilities/host/cpu/pages");
return NULL;
} }
npages = virXPathNodeSet(path, ctxt, &pages); npages = virXPathNodeSet(path, ctxt, &pages);

View File

@ -118,9 +118,9 @@ virshCommaStringListComplete(const char *input,
if (virStringListHasString((const char **)inputList, options[i])) if (virStringListHasString((const char **)inputList, options[i]))
continue; continue;
if (inputCopy && virAsprintf(&ret[nret], "%s,%s", inputCopy, options[i]) < 0) if (inputCopy)
return NULL; ret[nret] = g_strdup_printf("%s,%s", inputCopy, options[i]);
if (!inputCopy) else
ret[nret] = g_strdup(options[i]); ret[nret] = g_strdup(options[i]);
nret++; nret++;

View File

@ -431,10 +431,9 @@ cmdDomblkinfoGet(const virDomainBlockInfo *info,
*alloc = g_strdup("-"); *alloc = g_strdup("-");
*phy = g_strdup("-"); *phy = g_strdup("-");
} else if (!human) { } else if (!human) {
if (virAsprintf(cap, "%llu", info->capacity) < 0 || *cap = g_strdup_printf("%llu", info->capacity);
virAsprintf(alloc, "%llu", info->allocation) < 0 || *alloc = g_strdup_printf("%llu", info->allocation);
virAsprintf(phy, "%llu", info->physical) < 0) *phy = g_strdup_printf("%llu", info->physical);
return false;
} else { } else {
double val_cap, val_alloc, val_phy; double val_cap, val_alloc, val_phy;
const char *unit_cap, *unit_alloc, *unit_phy; const char *unit_cap, *unit_alloc, *unit_phy;
@ -443,10 +442,9 @@ cmdDomblkinfoGet(const virDomainBlockInfo *info,
val_alloc = vshPrettyCapacity(info->allocation, &unit_alloc); val_alloc = vshPrettyCapacity(info->allocation, &unit_alloc);
val_phy = vshPrettyCapacity(info->physical, &unit_phy); val_phy = vshPrettyCapacity(info->physical, &unit_phy);
if (virAsprintf(cap, "%.3lf %s", val_cap, unit_cap) < 0 || *cap = g_strdup_printf("%.3lf %s", val_cap, unit_cap);
virAsprintf(alloc, "%.3lf %s", val_alloc, unit_alloc) < 0 || *alloc = g_strdup_printf("%.3lf %s", val_alloc, unit_alloc);
virAsprintf(phy, "%.3lf %s", val_phy, unit_phy) < 0) *phy = g_strdup_printf("%.3lf %s", val_phy, unit_phy);
return false;
} }
return true; return true;
@ -828,10 +826,9 @@ cmdDomIfGetLink(vshControl *ctl, const vshCmd *cmd)
if (virMacAddrParse(iface, &macaddr) == 0) if (virMacAddrParse(iface, &macaddr) == 0)
virMacAddrFormat(&macaddr, macstr); virMacAddrFormat(&macaddr, macstr);
if (virAsprintf(&xpath, "/domain/devices/interface[(mac/@address = '%s') or " xpath = g_strdup_printf("/domain/devices/interface[(mac/@address = '%s') or "
" (target/@dev = '%s')]", " (target/@dev = '%s')]", macstr,
macstr, iface) < 0) iface);
goto cleanup;
if ((ninterfaces = virXPathNodeSet(xpath, ctxt, &interfaces)) < 0) { if ((ninterfaces = virXPathNodeSet(xpath, ctxt, &interfaces)) < 0) {
vshError(ctl, _("Failed to extract interface information")); vshError(ctl, _("Failed to extract interface information"));

View File

@ -5523,11 +5523,8 @@ virshGenFileName(vshControl *ctl, virDomainPtr dom, const char *mime)
localtime_r(&cur_time, &time_info); localtime_r(&cur_time, &time_info);
strftime(timestr, sizeof(timestr), "%Y-%m-%d-%H:%M:%S", &time_info); strftime(timestr, sizeof(timestr), "%Y-%m-%d-%H:%M:%S", &time_info);
if (virAsprintf(&ret, "%s-%s%s", virDomainGetName(dom), ret = g_strdup_printf("%s-%s%s", virDomainGetName(dom), timestr,
timestr, NULLSTR_EMPTY(ext)) < 0) { NULLSTR_EMPTY(ext));
vshError(ctl, "%s", _("Out of memory"));
return NULL;
}
return ret; return ret;
} }
@ -6957,8 +6954,7 @@ virshVcpuPinQuery(vshControl *ctl,
cpumaplen))) cpumaplen)))
goto cleanup; goto cleanup;
if (virAsprintf(&vcpuStr, "%zu", i) < 0) vcpuStr = g_strdup_printf("%zu", i);
goto cleanup;
if (vshTableRowAppend(table, vcpuStr, pinInfo, NULL) < 0) if (vshTableRowAppend(table, vcpuStr, pinInfo, NULL) < 0)
goto cleanup; goto cleanup;
@ -7574,8 +7570,7 @@ cmdIOThreadInfo(vshControl *ctl, const vshCmd *cmd)
g_autofree char *pinInfo = NULL; g_autofree char *pinInfo = NULL;
g_autofree char *iothreadIdStr = NULL; g_autofree char *iothreadIdStr = NULL;
if (virAsprintf(&iothreadIdStr, "%u", info[i]->iothread_id) < 0) iothreadIdStr = g_strdup_printf("%u", info[i]->iothread_id);
goto cleanup;
ignore_value(pinInfo = virBitmapDataFormat(info[i]->cpumap, info[i]->cpumaplen)); ignore_value(pinInfo = virBitmapDataFormat(info[i]->cpumap, info[i]->cpumaplen));
@ -11367,8 +11362,7 @@ cmdDomDisplay(vshControl *ctl, const vshCmd *cmd)
/* Create our XPATH lookup for the current display's port */ /* Create our XPATH lookup for the current display's port */
VIR_FREE(xpath); VIR_FREE(xpath);
if (virAsprintf(&xpath, xpath_fmt, scheme[iter], "@port") < 0) xpath = g_strdup_printf(xpath_fmt, scheme[iter], "@port");
goto cleanup;
/* Attempt to get the port number for the current graphics scheme */ /* Attempt to get the port number for the current graphics scheme */
tmp = virXPathInt(xpath, ctxt, &port); tmp = virXPathInt(xpath, ctxt, &port);
@ -11381,8 +11375,7 @@ cmdDomDisplay(vshControl *ctl, const vshCmd *cmd)
/* Create our XPATH lookup for TLS Port (automatically skipped /* Create our XPATH lookup for TLS Port (automatically skipped
* for unsupported schemes */ * for unsupported schemes */
if (virAsprintf(&xpath, xpath_fmt, scheme[iter], "@tlsPort") < 0) xpath = g_strdup_printf(xpath_fmt, scheme[iter], "@tlsPort");
goto cleanup;
/* Attempt to get the TLS port number */ /* Attempt to get the TLS port number */
tmp = virXPathInt(xpath, ctxt, &tls_port); tmp = virXPathInt(xpath, ctxt, &tls_port);
@ -11391,8 +11384,7 @@ cmdDomDisplay(vshControl *ctl, const vshCmd *cmd)
tls_port = 0; tls_port = 0;
/* Create our XPATH lookup for the current display's address */ /* Create our XPATH lookup for the current display's address */
if (virAsprintf(&xpath, xpath_fmt, scheme[iter], "@listen") < 0) xpath = g_strdup_printf(xpath_fmt, scheme[iter], "@listen");
goto cleanup;
/* Attempt to get the listening addr if set for the current /* Attempt to get the listening addr if set for the current
* graphics scheme */ * graphics scheme */
@ -11401,8 +11393,7 @@ cmdDomDisplay(vshControl *ctl, const vshCmd *cmd)
VIR_FREE(xpath); VIR_FREE(xpath);
/* Create our XPATH lookup for the current spice type. */ /* Create our XPATH lookup for the current spice type. */
if (virAsprintf(&xpath, xpath_fmt, scheme[iter], "listen/@type") < 0) xpath = g_strdup_printf(xpath_fmt, scheme[iter], "listen/@type");
goto cleanup;
/* Attempt to get the type of spice connection */ /* Attempt to get the type of spice connection */
VIR_FREE(type_conn); VIR_FREE(type_conn);
@ -11411,8 +11402,7 @@ cmdDomDisplay(vshControl *ctl, const vshCmd *cmd)
if (STREQ_NULLABLE(type_conn, "socket")) { if (STREQ_NULLABLE(type_conn, "socket")) {
if (!sockpath) { if (!sockpath) {
if (virAsprintf(&xpath, xpath_fmt, scheme[iter], "listen/@socket") < 0) xpath = g_strdup_printf(xpath_fmt, scheme[iter], "listen/@socket");
goto cleanup;
sockpath = virXPathString(xpath, ctxt); sockpath = virXPathString(xpath, ctxt);
@ -11432,8 +11422,7 @@ cmdDomDisplay(vshControl *ctl, const vshCmd *cmd)
* subelement (which, by the way, doesn't exist on libvirt * subelement (which, by the way, doesn't exist on libvirt
* < 0.9.4, so we really do need to check both places) * < 0.9.4, so we really do need to check both places)
*/ */
if (virAsprintf(&xpath, xpath_fmt, scheme[iter], "listen/@address") < 0) xpath = g_strdup_printf(xpath_fmt, scheme[iter], "listen/@address");
goto cleanup;
listen_addr = virXPathString(xpath, ctxt); listen_addr = virXPathString(xpath, ctxt);
VIR_FREE(xpath); VIR_FREE(xpath);
@ -11469,8 +11458,7 @@ cmdDomDisplay(vshControl *ctl, const vshCmd *cmd)
* care of when getting the XML */ * care of when getting the XML */
/* Create our XPATH lookup for the password */ /* Create our XPATH lookup for the password */
if (virAsprintf(&xpath, xpath_fmt, scheme[iter], "@passwd") < 0) xpath = g_strdup_printf(xpath_fmt, scheme[iter], "@passwd");
goto cleanup;
/* Attempt to get the password */ /* Attempt to get the password */
VIR_FREE(passwd); VIR_FREE(passwd);

View File

@ -1140,8 +1140,7 @@ vshExtractCPUDefXMLs(vshControl *ctl,
else else
doc = buffer; doc = buffer;
if (virAsprintf(&xmlStr, "<container>%s</container>", doc) < 0) xmlStr = g_strdup_printf("<container>%s</container>", doc);
goto error;
if (!(xml = virXMLParseStringCtxt(xmlStr, xmlFile, &ctxt))) if (!(xml = virXMLParseStringCtxt(xmlStr, xmlFile, &ctxt)))
goto error; goto error;

View File

@ -893,12 +893,13 @@ cmdInterfaceBridge(vshControl *ctl, const vshCmd *cmd)
goto cleanup; goto cleanup;
} }
if (stp && if (stp) {
((virAsprintf(&delay_str, "%d", delay) < 0) || delay_str = g_strdup_printf("%d", delay);
!xmlSetProp(br_node, BAD_CAST "delay", BAD_CAST delay_str))) { if (!xmlSetProp(br_node, BAD_CAST "delay", BAD_CAST delay_str)) {
vshError(ctl, _("Failed to set bridge delay %d in xml document"), delay); vshError(ctl, _("Failed to set bridge delay %d in xml document"), delay);
goto cleanup; goto cleanup;
} }
}
/* Change the type of the outer/master interface to "bridge" and the /* Change the type of the outer/master interface to "bridge" and the
* name to the provided bridge name. * name to the provided bridge name.

View File

@ -1444,8 +1444,7 @@ cmdNetworkDHCPLeases(vshControl *ctl, const vshCmd *cmd)
else if (lease->type == VIR_IP_ADDR_TYPE_IPV6) else if (lease->type == VIR_IP_ADDR_TYPE_IPV6)
typestr = "ipv6"; typestr = "ipv6";
ignore_value(virAsprintf(&cidr_format, "%s/%d", cidr_format = g_strdup_printf("%s/%d", lease->ipaddr, lease->prefix);
lease->ipaddr, lease->prefix));
if (vshTableRowAppend(table, if (vshTableRowAppend(table,
expirytime, expirytime,

View File

@ -1281,19 +1281,16 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
const char *unit; const char *unit;
val = vshPrettyCapacity(info.capacity, &unit); val = vshPrettyCapacity(info.capacity, &unit);
if (virAsprintf(&poolInfoTexts[i].capacity, poolInfoTexts[i].capacity = g_strdup_printf("%.2lf %s", val,
"%.2lf %s", val, unit) < 0) unit);
goto cleanup;
val = vshPrettyCapacity(info.allocation, &unit); val = vshPrettyCapacity(info.allocation, &unit);
if (virAsprintf(&poolInfoTexts[i].allocation, poolInfoTexts[i].allocation = g_strdup_printf("%.2lf %s", val,
"%.2lf %s", val, unit) < 0) unit);
goto cleanup;
val = vshPrettyCapacity(info.available, &unit); val = vshPrettyCapacity(info.available, &unit);
if (virAsprintf(&poolInfoTexts[i].available, poolInfoTexts[i].available = g_strdup_printf("%.2lf %s", val,
"%.2lf %s", val, unit) < 0) unit);
goto cleanup;
} else { } else {
/* Capacity related information isn't available */ /* Capacity related information isn't available */
poolInfoTexts[i].capacity = g_strdup(_("-")); poolInfoTexts[i].capacity = g_strdup(_("-"));

View File

@ -1426,14 +1426,11 @@ cmdVolList(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
volInfoTexts[i].type = g_strdup(virshVolumeTypeToString(volumeInfo.type)); volInfoTexts[i].type = g_strdup(virshVolumeTypeToString(volumeInfo.type));
val = vshPrettyCapacity(volumeInfo.capacity, &unit); val = vshPrettyCapacity(volumeInfo.capacity, &unit);
if (virAsprintf(&volInfoTexts[i].capacity, volInfoTexts[i].capacity = g_strdup_printf("%.2lf %s", val, unit);
"%.2lf %s", val, unit) < 0)
goto cleanup;
val = vshPrettyCapacity(volumeInfo.allocation, &unit); val = vshPrettyCapacity(volumeInfo.allocation, &unit);
if (virAsprintf(&volInfoTexts[i].allocation, volInfoTexts[i].allocation = g_strdup_printf("%.2lf %s", val,
"%.2lf %s", val, unit) < 0) unit);
goto cleanup;
} }
} }
} }

View File

@ -394,8 +394,7 @@ cmdSrvList(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
for (i = 0; i < nsrvs; i++) { for (i = 0; i < nsrvs; i++) {
g_autofree char *idStr = NULL; g_autofree char *idStr = NULL;
if (virAsprintf(&idStr, "%zu", i) < 0) idStr = g_strdup_printf("%zu", i);
goto cleanup;
if (vshTableRowAppend(table, if (vshTableRowAppend(table,
idStr, idStr,
@ -656,8 +655,7 @@ cmdSrvClientsList(vshControl *ctl, const vshCmd *cmd)
&timestr) < 0) &timestr) < 0)
goto cleanup; goto cleanup;
if (virAsprintf(&idStr, "%llu", id) < 0) idStr = g_strdup_printf("%llu", id);
goto cleanup;
if (vshTableRowAppend(table, idStr, if (vshTableRowAppend(table, idStr,
vshAdmClientTransportToString(transport), vshAdmClientTransportToString(transport),
timestr, NULL) < 0) timestr, NULL) < 0)

View File

@ -1787,28 +1787,27 @@ vshCommandOptTimeoutToMs(vshControl *ctl, const vshCmd *cmd, int *timeout)
char * char *
vshGetTypedParamValue(vshControl *ctl, virTypedParameterPtr item) vshGetTypedParamValue(vshControl *ctl, virTypedParameterPtr item)
{ {
int ret = 0;
char *str = NULL; char *str = NULL;
switch (item->type) { switch (item->type) {
case VIR_TYPED_PARAM_INT: case VIR_TYPED_PARAM_INT:
ret = virAsprintf(&str, "%d", item->value.i); str = g_strdup_printf("%d", item->value.i);
break; break;
case VIR_TYPED_PARAM_UINT: case VIR_TYPED_PARAM_UINT:
ret = virAsprintf(&str, "%u", item->value.ui); str = g_strdup_printf("%u", item->value.ui);
break; break;
case VIR_TYPED_PARAM_LLONG: case VIR_TYPED_PARAM_LLONG:
ret = virAsprintf(&str, "%lld", item->value.l); str = g_strdup_printf("%lld", item->value.l);
break; break;
case VIR_TYPED_PARAM_ULLONG: case VIR_TYPED_PARAM_ULLONG:
ret = virAsprintf(&str, "%llu", item->value.ul); str = g_strdup_printf("%llu", item->value.ul);
break; break;
case VIR_TYPED_PARAM_DOUBLE: case VIR_TYPED_PARAM_DOUBLE:
ret = virAsprintf(&str, "%f", item->value.d); str = g_strdup_printf("%f", item->value.d);
break; break;
case VIR_TYPED_PARAM_BOOLEAN: case VIR_TYPED_PARAM_BOOLEAN:
@ -1823,7 +1822,7 @@ vshGetTypedParamValue(vshControl *ctl, virTypedParameterPtr item)
vshError(ctl, _("unimplemented parameter type %d"), item->type); vshError(ctl, _("unimplemented parameter type %d"), item->type);
} }
if (ret < 0) { if (!str) {
vshError(ctl, "%s", _("Out of memory")); vshError(ctl, "%s", _("Out of memory"));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -2408,10 +2407,7 @@ vshEditWriteToTempFile(vshControl *ctl, const char *doc)
tmpdir = getenv("TMPDIR"); tmpdir = getenv("TMPDIR");
if (!tmpdir) tmpdir = "/tmp"; if (!tmpdir) tmpdir = "/tmp";
if (virAsprintf(&ret, "%s/virshXXXXXX.xml", tmpdir) < 0) { ret = g_strdup_printf("%s/virshXXXXXX.xml", tmpdir);
vshError(ctl, "%s", _("out of memory"));
return NULL;
}
fd = mkostemps(ret, 4, O_CLOEXEC); fd = mkostemps(ret, 4, O_CLOEXEC);
if (fd == -1) { if (fd == -1) {
vshError(ctl, _("mkostemps: failed to create temporary file: %s"), vshError(ctl, _("mkostemps: failed to create temporary file: %s"),
@ -2919,8 +2915,7 @@ vshReadlineInit(vshControl *ctl)
rl_completer_quote_characters = quote_characters; rl_completer_quote_characters = quote_characters;
rl_char_is_quoted_p = vshReadlineCharIsQuoted; rl_char_is_quoted_p = vshReadlineCharIsQuoted;
if (virAsprintf(&histsize_env, "%s_HISTSIZE", ctl->env_prefix) < 0) histsize_env = g_strdup_printf("%s_HISTSIZE", ctl->env_prefix);
goto cleanup;
/* Limit the total size of the history buffer */ /* Limit the total size of the history buffer */
if ((histsize_str = getenv(histsize_env))) { if ((histsize_str = getenv(histsize_env))) {
@ -2946,15 +2941,9 @@ vshReadlineInit(vshControl *ctl)
goto cleanup; goto cleanup;
} }
if (virAsprintf(&ctl->historydir, "%s/%s", userdir, ctl->name) < 0) { ctl->historydir = g_strdup_printf("%s/%s", userdir, ctl->name);
vshError(ctl, "%s", _("Out of memory"));
goto cleanup;
}
if (virAsprintf(&ctl->historyfile, "%s/history", ctl->historydir) < 0) { ctl->historyfile = g_strdup_printf("%s/history", ctl->historydir);
vshError(ctl, "%s", _("Out of memory"));
goto cleanup;
}
read_history(ctl->historyfile); read_history(ctl->historyfile);
ret = 0; ret = 0;
@ -3036,8 +3025,7 @@ vshInitDebug(vshControl *ctl)
char *env = NULL; char *env = NULL;
if (ctl->debug == VSH_DEBUG_DEFAULT) { if (ctl->debug == VSH_DEBUG_DEFAULT) {
if (virAsprintf(&env, "%s_DEBUG", ctl->env_prefix) < 0) env = g_strdup_printf("%s_DEBUG", ctl->env_prefix);
return -1;
/* log level not set from commandline, check env variable */ /* log level not set from commandline, check env variable */
debugEnv = getenv(env); debugEnv = getenv(env);
@ -3055,8 +3043,7 @@ vshInitDebug(vshControl *ctl)
} }
if (ctl->logfile == NULL) { if (ctl->logfile == NULL) {
if (virAsprintf(&env, "%s_LOG_FILE", ctl->env_prefix) < 0) env = g_strdup_printf("%s_LOG_FILE", ctl->env_prefix);
return -1;
/* log file not set from cmdline */ /* log file not set from cmdline */
debugEnv = getenv(env); debugEnv = getenv(env);