conf: 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 89b6825189
commit daeeb3603d
14 changed files with 60 additions and 101 deletions

View File

@ -1841,8 +1841,7 @@ virCapabilitiesInitCaches(virCapsPtr caps)
int rv = -1; int rv = -1;
VIR_FREE(path); VIR_FREE(path);
if (virAsprintf(&path, "%s/cpu/cpu%zd/cache/", SYSFS_SYSTEM_PATH, pos) < 0) path = g_strdup_printf("%s/cpu/cpu%zd/cache/", SYSFS_SYSTEM_PATH, pos);
goto cleanup;
VIR_DIR_CLOSE(dirp); VIR_DIR_CLOSE(dirp);

View File

@ -1388,10 +1388,10 @@ virDomainCCWAddressAsString(virDomainDeviceCCWAddressPtr addr)
{ {
char *addrstr = NULL; char *addrstr = NULL;
ignore_value(virAsprintf(&addrstr, "%x.%x.%04x", addrstr = g_strdup_printf("%x.%x.%04x",
addr->cssid, addr->cssid,
addr->ssid, addr->ssid,
addr->devno)); addr->devno);
return addrstr; return addrstr;
} }

View File

@ -53,7 +53,7 @@ virDomainAuditGetRdev(const char *path)
(S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode))) { (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode))) {
int maj = major(sb.st_rdev); int maj = major(sb.st_rdev);
int min = minor(sb.st_rdev); int min = minor(sb.st_rdev);
ignore_value(virAsprintfQuiet(&ret, "%02X:%02X", maj, min)); ret = g_strdup_printf("%02X:%02X", maj, min);
} }
return ret; return ret;
} }
@ -101,11 +101,8 @@ virDomainAuditGenericDev(virDomainObjPtr vm,
if (!newsrcpath && !oldsrcpath) if (!newsrcpath && !oldsrcpath)
return; return;
if (virAsprintfQuiet(&newdev, "new-%s", type) < 0) newdev = g_strdup_printf("new-%s", type);
goto no_memory; olddev = g_strdup_printf("old-%s", type);
if (virAsprintfQuiet(&olddev, "old-%s", type) < 0)
goto no_memory;
virUUIDFormat(vm->def->uuid, uuidstr); virUUIDFormat(vm->def->uuid, uuidstr);
@ -376,22 +373,14 @@ virDomainAuditHostdev(virDomainObjPtr vm, virDomainHostdevDefPtr hostdev,
case VIR_DOMAIN_HOSTDEV_MODE_SUBSYS: case VIR_DOMAIN_HOSTDEV_MODE_SUBSYS:
switch ((virDomainHostdevSubsysType) hostdev->source.subsys.type) { switch ((virDomainHostdevSubsysType) hostdev->source.subsys.type) {
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
if (virAsprintfQuiet(&address, address = g_strdup_printf(VIR_PCI_DEVICE_ADDRESS_FMT,
VIR_PCI_DEVICE_ADDRESS_FMT, pcisrc->addr.domain,
pcisrc->addr.domain, pcisrc->addr.bus,
pcisrc->addr.bus, pcisrc->addr.slot,
pcisrc->addr.slot, pcisrc->addr.function);
pcisrc->addr.function) < 0) {
VIR_WARN("OOM while encoding audit message");
goto cleanup;
}
break; break;
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
if (virAsprintfQuiet(&address, "%.3d.%.3d", address = g_strdup_printf("%.3d.%.3d", usbsrc->bus, usbsrc->device);
usbsrc->bus, usbsrc->device) < 0) {
VIR_WARN("OOM while encoding audit message");
goto cleanup;
}
break; break;
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: { case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: {
if (scsisrc->protocol == if (scsisrc->protocol ==
@ -403,13 +392,10 @@ virDomainAuditHostdev(virDomainObjPtr vm, virDomainHostdevDefPtr hostdev,
} else { } else {
virDomainHostdevSubsysSCSIHostPtr scsihostsrc = virDomainHostdevSubsysSCSIHostPtr scsihostsrc =
&scsisrc->u.host; &scsisrc->u.host;
if (virAsprintfQuiet(&address, "%s:%u:%u:%llu", address = g_strdup_printf("%s:%u:%u:%llu",
scsihostsrc->adapter, scsihostsrc->bus, scsihostsrc->adapter, scsihostsrc->bus,
scsihostsrc->target, scsihostsrc->target,
scsihostsrc->unit) < 0) { scsihostsrc->unit);
VIR_WARN("OOM while encoding audit message");
goto cleanup;
}
} }
break; break;
} }
@ -661,11 +647,8 @@ virDomainAuditCgroupMajor(virDomainObjPtr vm, virCgroupPtr cgroup,
{ {
char *extra; char *extra;
if (virAsprintfQuiet(&extra, "major category=%s maj=%02X acl=%s", extra = g_strdup_printf("major category=%s maj=%02X acl=%s",
name, maj, perms) < 0) { name, maj, perms);
VIR_WARN("OOM while encoding audit message");
return;
}
virDomainAuditCgroup(vm, cgroup, reason, extra, success); virDomainAuditCgroup(vm, cgroup, reason, extra, success);
@ -699,13 +682,14 @@ virDomainAuditCgroupPath(virDomainObjPtr vm, virCgroupPtr cgroup,
rdev = virDomainAuditGetRdev(path); rdev = virDomainAuditGetRdev(path);
if (!(detail = virAuditEncode("path", path)) || if (!(detail = virAuditEncode("path", path))) {
virAsprintfQuiet(&extra, "path %s rdev=%s acl=%s",
detail, VIR_AUDIT_STR(rdev), perms) < 0) {
VIR_WARN("OOM while encoding audit message"); VIR_WARN("OOM while encoding audit message");
goto cleanup; goto cleanup;
} }
extra = g_strdup_printf("path %s rdev=%s acl=%s",
detail, VIR_AUDIT_STR(rdev), perms);
virDomainAuditCgroup(vm, cgroup, reason, extra, rc == 0); virDomainAuditCgroup(vm, cgroup, reason, extra, rc == 0);
cleanup: cleanup:
@ -936,12 +920,13 @@ virDomainAuditShmem(virDomainObjPtr vm,
virUUIDFormat(vm->def->uuid, uuidstr); virUUIDFormat(vm->def->uuid, uuidstr);
if (!vmname || if (!vmname) {
virAsprintfQuiet(&shmpath, "/dev/shm/%s", def->name) < 0) {
VIR_WARN("OOM while encoding audit message"); VIR_WARN("OOM while encoding audit message");
goto cleanup; goto cleanup;
} }
shmpath = g_strdup_printf("/dev/shm/%s", def->name);
if (!virt) { if (!virt) {
VIR_WARN("Unexpected virt type %d while encoding audit message", VIR_WARN("Unexpected virt type %d while encoding audit message",
vm->def->virtType); vm->def->virtType);

View File

@ -4952,8 +4952,7 @@ virDomainPostParseCheckISCSIPath(char **srcpath)
if (strchr(*srcpath, '/')) if (strchr(*srcpath, '/'))
return 0; return 0;
if (virAsprintf(&path, "%s/0", *srcpath) < 0) path = g_strdup_printf("%s/0", *srcpath);
return -1;
VIR_FREE(*srcpath); VIR_FREE(*srcpath);
*srcpath = g_steal_pointer(&path); *srcpath = g_steal_pointer(&path);
return 0; return 0;
@ -5614,8 +5613,7 @@ virDomainDefCollectBootOrder(virDomainDefPtr def G_GNUC_UNUSED,
*/ */
return 0; return 0;
} }
if (virAsprintf(&order, "%u", info->bootIndex) < 0) order = g_strdup_printf("%u", info->bootIndex);
return -1;
if (virHashLookup(bootHash, order)) { if (virHashLookup(bootHash, order)) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
@ -10158,9 +10156,8 @@ virDomainDiskDefParseXML(virDomainXMLOptionPtr xmlopt,
if (!target && !(flags & VIR_DOMAIN_DEF_PARSE_DISK_SOURCE)) { if (!target && !(flags & VIR_DOMAIN_DEF_PARSE_DISK_SOURCE)) {
if (def->src->srcpool) { if (def->src->srcpool) {
if (virAsprintf(&tmp, "pool = '%s', volume = '%s'", tmp = g_strdup_printf("pool = '%s', volume = '%s'",
def->src->srcpool->pool, def->src->srcpool->volume) < 0) def->src->srcpool->pool, def->src->srcpool->volume);
goto error;
virReportError(VIR_ERR_NO_TARGET, "%s", tmp); virReportError(VIR_ERR_NO_TARGET, "%s", tmp);
VIR_FREE(tmp); VIR_FREE(tmp);
@ -10357,8 +10354,7 @@ virDomainParseScaledValue(const char *xpath,
g_autofree char *bytes_str = NULL; g_autofree char *bytes_str = NULL;
*val = 0; *val = 0;
if (virAsprintf(&xpath_full, "string(%s)", xpath) < 0) xpath_full = g_strdup_printf("string(%s)", xpath);
return -1;
bytes_str = virXPathString(xpath_full, ctxt); bytes_str = virXPathString(xpath_full, ctxt);
if (!bytes_str) { if (!bytes_str) {
@ -10378,11 +10374,10 @@ virDomainParseScaledValue(const char *xpath,
return -1; return -1;
} }
if ((units_xpath && if (units_xpath)
virAsprintf(&xpath_full, "string(%s)", units_xpath) < 0) || xpath_full = g_strdup_printf("string(%s)", units_xpath);
(!units_xpath && else
virAsprintf(&xpath_full, "string(%s/@unit)", xpath) < 0)) xpath_full = g_strdup_printf("string(%s/@unit)", xpath);
return -1;
unit = virXPathString(xpath_full, ctxt); unit = virXPathString(xpath_full, ctxt);
if (virScaleInteger(&bytes, unit, scale, max) < 0) if (virScaleInteger(&bytes, unit, scale, max) < 0)
@ -19409,8 +19404,7 @@ virDomainResctrlMonDefParse(virDomainDefPtr def,
if (!(tmp = virBitmapFormat(domresmon->vcpus))) if (!(tmp = virBitmapFormat(domresmon->vcpus)))
goto cleanup; goto cleanup;
if (virAsprintf(&id, "vcpus_%s", tmp) < 0) id = g_strdup_printf("vcpus_%s", tmp);
goto cleanup;
} }
virResctrlMonitorSetAlloc(domresmon->instance, resctrl->alloc); virResctrlMonitorSetAlloc(domresmon->instance, resctrl->alloc);
@ -19460,8 +19454,7 @@ virDomainResctrlNew(xmlNodePtr node,
* directory, so it's nice to have it named appropriately. For now it's * directory, so it's nice to have it named appropriately. For now it's
* 'vcpus_...' but it's designed in order for it to be changeable in the * 'vcpus_...' but it's designed in order for it to be changeable in the
* future (it's part of the status XML). */ * future (it's part of the status XML). */
if (virAsprintf(&alloc_id, "vcpus_%s", vcpus_str) < 0) alloc_id = g_strdup_printf("vcpus_%s", vcpus_str);
goto cleanup;
} }
if (virResctrlAllocSetID(alloc, alloc_id) < 0) if (virResctrlAllocSetID(alloc, alloc_id) < 0)
@ -24062,10 +24055,8 @@ virDomainDiskSourceFormatNetwork(virBufferPtr attrBuf,
virBufferAsprintf(attrBuf, " protocol='%s'", virBufferAsprintf(attrBuf, " protocol='%s'",
virStorageNetProtocolTypeToString(src->protocol)); virStorageNetProtocolTypeToString(src->protocol));
if (src->volume) { if (src->volume)
if (virAsprintf(&path, "%s/%s", src->volume, src->path) < 0) path = g_strdup_printf("%s/%s", src->volume, src->path);
return -1;
}
virBufferEscapeString(attrBuf, " name='%s'", path ? path : src->path); virBufferEscapeString(attrBuf, " name='%s'", path ? path : src->path);
@ -29196,7 +29187,7 @@ char
{ {
char *ret; char *ret;
ignore_value(virAsprintf(&ret, "%s/%s.xml", dir, name)); ret = g_strdup_printf("%s/%s.xml", dir, name);
return ret; return ret;
} }
@ -30346,8 +30337,7 @@ virDomainDefGetShortName(const virDomainDef *def)
len = mbstowcs(NULL, def->name, 0); len = mbstowcs(NULL, def->name, 0);
if ((len == (size_t) -1 && errno == EILSEQ) || if ((len == (size_t) -1 && errno == EILSEQ) ||
len == strlen(def->name)) { len == strlen(def->name)) {
ignore_value(virAsprintf(&ret, "%d-%.*s", def->id, ret = g_strdup_printf("%d-%.*s", def->id, VIR_DOMAIN_SHORT_NAME_MAX, def->name);
VIR_DOMAIN_SHORT_NAME_MAX, def->name));
return ret; return ret;
} }
@ -30383,7 +30373,7 @@ virDomainDefGetShortName(const virDomainDef *def)
return NULL; return NULL;
} }
ignore_value(virAsprintf(&ret, "%d-%s", def->id, shortname)); ret = g_strdup_printf("%d-%s", def->id, shortname);
return ret; return ret;
} }
@ -31184,10 +31174,8 @@ virDomainDiskAddISCSIPoolSourceHost(virDomainDiskDefPtr def,
} }
/* iscsi pool has only one source device path */ /* iscsi pool has only one source device path */
if (virAsprintf(&def->src->path, "%s/%s", def->src->path = g_strdup_printf("%s/%s", pooldef->source.devices[0].path,
pooldef->source.devices[0].path, tokens[3]);
tokens[3]) < 0)
goto cleanup;
/* Storage pool have not supported these 2 attributes yet, /* Storage pool have not supported these 2 attributes yet,
* use the defaults. * use the defaults.

View File

@ -77,9 +77,8 @@ virDomainMomentDefPostParse(virDomainMomentDefPtr def)
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
if (!def->name && if (!def->name)
virAsprintf(&def->name, "%lld", (long long)tv.tv_sec) < 0) def->name = g_strdup_printf("%lld", (long long)tv.tv_sec);
return -1;
def->creationTime = tv.tv_sec; def->creationTime = tv.tv_sec;
return 0; return 0;

View File

@ -2753,7 +2753,7 @@ virNetworkConfigFile(const char *dir,
{ {
char *ret = NULL; char *ret = NULL;
ignore_value(virAsprintf(&ret, "%s/%s.xml", dir, name)); ret = g_strdup_printf("%s/%s.xml", dir, name);
return ret; return ret;
} }

View File

@ -182,8 +182,7 @@ virNodeDeviceDeleteVport(virConnectPtr conn,
goto cleanup; goto cleanup;
} }
if (virAsprintf(&scsi_host_name, "scsi_%s", name) < 0) scsi_host_name = g_strdup_printf("scsi_%s", name);
goto cleanup;
/* If at startup time we provided a parent, then use that to /* If at startup time we provided a parent, then use that to
* get the parent_host value; otherwise, we have to determine * get the parent_host value; otherwise, we have to determine

View File

@ -615,10 +615,7 @@ virDomainSnapshotDefAssignExternalNames(virDomainSnapshotDefPtr def)
if ((tmp = strrchr(tmppath, '.')) && !strchr(tmp, '/')) if ((tmp = strrchr(tmppath, '.')) && !strchr(tmp, '/'))
*tmp = '\0'; *tmp = '\0';
if (virAsprintf(&disk->src->path, "%s.%s", tmppath, def->parent.name) < 0) { disk->src->path = g_strdup_printf("%s.%s", tmppath, def->parent.name);
VIR_FREE(tmppath);
return -1;
}
VIR_FREE(tmppath); VIR_FREE(tmppath);

View File

@ -952,11 +952,9 @@ virStoragePoolDefParseXML(xmlXPathContextPtr ctxt)
* path and permissions */ * path and permissions */
if (!(options->flags & VIR_STORAGE_POOL_SOURCE_NETWORK)) { if (!(options->flags & VIR_STORAGE_POOL_SOURCE_NETWORK)) {
if (def->type == VIR_STORAGE_POOL_LOGICAL) { if (def->type == VIR_STORAGE_POOL_LOGICAL) {
if (virAsprintf(&target_path, "/dev/%s", def->source.name) < 0) target_path = g_strdup_printf("/dev/%s", def->source.name);
return NULL;
} else if (def->type == VIR_STORAGE_POOL_ZFS) { } else if (def->type == VIR_STORAGE_POOL_ZFS) {
if (virAsprintf(&target_path, "/dev/zvol/%s", def->source.name) < 0) target_path = g_strdup_printf("/dev/zvol/%s", def->source.name);
return NULL;
} else { } else {
target_path = virXPathString("string(./target/path)", ctxt); target_path = virXPathString("string(./target/path)", ctxt);
if (!target_path) { if (!target_path) {

View File

@ -88,12 +88,10 @@ static char *virChrdevLockFilePath(const char *dev)
++p; ++p;
} }
if (virAsprintf(&path, "%s/LCK..%s", VIR_CHRDEV_LOCK_FILE_PATH, filename) < 0) path = g_strdup_printf("%s/LCK..%s", VIR_CHRDEV_LOCK_FILE_PATH, filename);
goto cleanup;
sanitizedPath = virFileSanitizePath(path); sanitizedPath = virFileSanitizePath(path);
cleanup:
VIR_FREE(path); VIR_FREE(path);
VIR_FREE(devCopy); VIR_FREE(devCopy);
@ -135,8 +133,7 @@ static int virChrdevLockFileCreate(const char *dev)
/* ensure correct format according to filesystem hierarchy standard */ /* ensure correct format according to filesystem hierarchy standard */
/* http://www.pathname.com/fhs/pub/fhs-2.3.html#VARLOCKLOCKFILES */ /* http://www.pathname.com/fhs/pub/fhs-2.3.html#VARLOCKLOCKFILES */
if (virAsprintf(&pidStr, "%10lld\n", (long long) getpid()) < 0) pidStr = g_strdup_printf("%10lld\n", (long long)getpid());
goto cleanup;
/* create the lock file */ /* create the lock file */
if ((lockfd = open(path, O_WRONLY | O_CREAT | O_EXCL, 00644)) < 0) { if ((lockfd = open(path, O_WRONLY | O_CREAT | O_EXCL, 00644)) < 0) {

View File

@ -1624,7 +1624,7 @@ virNetworkObjGetPortStatusDir(virNetworkObjPtr net,
const char *stateDir) const char *stateDir)
{ {
char *ret; char *ret;
ignore_value(virAsprintf(&ret, "%s/%s/ports", stateDir, net->def->name)); ret = g_strdup_printf("%s/%s/ports", stateDir, net->def->name);
return ret; return ret;
} }
@ -1736,8 +1736,7 @@ virNetworkObjDeleteAllPorts(virNetworkObjPtr net,
if (!virStringStripSuffix(de->d_name, ".xml")) if (!virStringStripSuffix(de->d_name, ".xml"))
continue; continue;
if (virAsprintf(&file, "%s/%s.xml", dir, de->d_name) < 0) file = g_strdup_printf("%s/%s.xml", dir, de->d_name);
goto cleanup;
if (unlink(file) < 0 && errno != ENOENT) if (unlink(file) < 0 && errno != ENOENT)
VIR_WARN("Unable to delete %s", file); VIR_WARN("Unable to delete %s", file);
@ -1897,8 +1896,7 @@ virNetworkObjLoadAllPorts(virNetworkObjPtr net,
if (!virStringStripSuffix(de->d_name, ".xml")) if (!virStringStripSuffix(de->d_name, ".xml"))
continue; continue;
if (virAsprintf(&file, "%s/%s.xml", dir, de->d_name) < 0) file = g_strdup_printf("%s/%s.xml", dir, de->d_name);
goto cleanup;
portdef = virNetworkPortDefParseFile(file); portdef = virNetworkPortDefParseFile(file);
VIR_FREE(file); VIR_FREE(file);

View File

@ -425,7 +425,7 @@ virNetworkPortDefConfigFile(const char *dir,
{ {
char *ret = NULL; char *ret = NULL;
ignore_value(virAsprintf(&ret, "%s/%s.xml", dir, name)); ret = g_strdup_printf("%s/%s.xml", dir, name);
return ret; return ret;
} }

View File

@ -139,7 +139,7 @@ virNWFilterBindingObjConfigFile(const char *dir,
{ {
char *ret; char *ret;
ignore_value(virAsprintf(&ret, "%s/%s.xml", dir, name)); ret = g_strdup_printf("%s/%s.xml", dir, name);
return ret; return ret;
} }

View File

@ -1229,8 +1229,7 @@ matchFCHostToSCSIHost(virStorageAdapterFCHostPtr fchost,
*/ */
if (!fchost->parent && if (!fchost->parent &&
(conn = virGetConnectNodeDev())) { (conn = virGetConnectNodeDev())) {
if (virAsprintf(&scsi_host_name, "scsi_%s", name) < 0) scsi_host_name = g_strdup_printf("scsi_%s", name);
goto cleanup;
if ((parent_name = virNodeDeviceGetParentName(conn, if ((parent_name = virNodeDeviceGetParentName(conn,
scsi_host_name))) { scsi_host_name))) {
if (virStorageIsSameHostnum(parent_name, scsi_hostnum)) { if (virStorageIsSameHostnum(parent_name, scsi_hostnum)) {