vbox: 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 837f5619e7
commit 24d213b03c
7 changed files with 55 additions and 82 deletions

View File

@ -525,8 +525,8 @@ vboxComInitialize_v2(const char *pszVirtualBoxIID, IVirtualBox **ppVirtualBox,
CoInitialize(NULL);
if (virAsprintf(&mbsVirtualBoxIID, "{%s}", pszVirtualBoxIID) < 0 ||
virAsprintf(&mbsSessionIID, "{%s}", pszSessionIID) < 0)
mbsVirtualBoxIID = g_strdup_printf("{%s}", pszVirtualBoxIID);
mbsSessionIID = g_strdup_printf("{%s}", pszSessionIID)
goto cleanup;
if (vboxUtf8ToUtf16(mbsVirtualBoxIID, &wcsVirtualBoxIID) < 0 ||

View File

@ -85,8 +85,7 @@ tryLoadOne(const char *dir, bool setAppHome, bool ignoreMissing,
PFNVBOXGETXPCOMCFUNCTIONS pfnGetFunctions;
if (dir != NULL) {
if (virAsprintf(&name, "%s/%s", dir, DYNLIB_NAME) < 0)
return -1;
name = g_strdup_printf("%s/%s", dir, DYNLIB_NAME);
if (!virFileExists(name)) {
if (!ignoreMissing)

View File

@ -1770,13 +1770,12 @@ vboxAttachUSB(virDomainDefPtr def, vboxDriverPtr data, IMachine *machine)
/* Zero pad for nice alignment when fewer than 9999
* devices.
*/
if (virAsprintf(&filtername, "filter%04zu", i) >= 0) {
VBOX_UTF8_TO_UTF16(filtername, &filternameUtf16);
VIR_FREE(filtername);
gVBoxAPI.UIUSBCommon.CreateDeviceFilter(USBCommon,
filternameUtf16,
&filter);
}
filtername = g_strdup_printf("filter%04zu", i);
VBOX_UTF8_TO_UTF16(filtername, &filternameUtf16);
VIR_FREE(filtername);
gVBoxAPI.UIUSBCommon.CreateDeviceFilter(USBCommon,
filternameUtf16,
&filter);
VBOX_UTF16_FREE(filternameUtf16);
if (!filter)
@ -2127,10 +2126,9 @@ vboxStartMachine(virDomainPtr dom, int maxDomID, IMachine *machine, vboxIID *iid
if (guiPresent) {
if (guiDisplay) {
char *displayutf8;
if (virAsprintf(&displayutf8, "DISPLAY=%s", guiDisplay) >= 0) {
VBOX_UTF8_TO_UTF16(displayutf8, &env);
VIR_FREE(displayutf8);
}
displayutf8 = g_strdup_printf("DISPLAY=%s", guiDisplay);
VBOX_UTF8_TO_UTF16(displayutf8, &env);
VIR_FREE(displayutf8);
VIR_FREE(guiDisplay);
}
@ -2140,10 +2138,9 @@ vboxStartMachine(virDomainPtr dom, int maxDomID, IMachine *machine, vboxIID *iid
if (sdlPresent) {
if (sdlDisplay) {
char *displayutf8;
if (virAsprintf(&displayutf8, "DISPLAY=%s", sdlDisplay) >= 0) {
VBOX_UTF8_TO_UTF16(displayutf8, &env);
VIR_FREE(displayutf8);
}
displayutf8 = g_strdup_printf("DISPLAY=%s", sdlDisplay);
VBOX_UTF8_TO_UTF16(displayutf8, &env);
VIR_FREE(displayutf8);
VIR_FREE(sdlDisplay);
}
@ -4619,8 +4616,7 @@ vboxSnapshotRedefine(virDomainPtr dom,
}
VBOX_UTF16_TO_UTF8(machineNameUtf16, &machineName);
if (virAsprintf(&nameTmpUse, "%s.vbox", machineName) < 0)
goto cleanup;
nameTmpUse = g_strdup_printf("%s.vbox", machineName);
machineLocationPath = virStringReplace(settingsFilePath_Utf8, nameTmpUse, "");
if (machineLocationPath == NULL) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@ -4636,9 +4632,8 @@ vboxSnapshotRedefine(virDomainPtr dom,
goto cleanup;
}
if (snapshotMachineDesc->currentSnapshot != NULL) {
if (virAsprintf(&currentSnapshotXmlFilePath, "%s%s.xml", machineLocationPath,
snapshotMachineDesc->currentSnapshot) < 0)
goto cleanup;
currentSnapshotXmlFilePath = g_strdup_printf("%s%s.xml",
machineLocationPath, snapshotMachineDesc->currentSnapshot);
snapshotFileExists = virFileExists(currentSnapshotXmlFilePath);
}
@ -5206,8 +5201,8 @@ vboxSnapshotRedefine(virDomainPtr dom,
vboxIIDUnalloc(&parentiid);
VBOX_UTF8_TO_UTF16("VDI", &formatUtf16);
if (virAsprintf(&newLocationUtf8, "%sfakedisk-%d.vdi", machineLocationPath, it) < 0)
goto cleanup;
newLocationUtf8 = g_strdup_printf("%sfakedisk-%d.vdi",
machineLocationPath, it);
VBOX_UTF8_TO_UTF16(newLocationUtf8, &newLocation);
rc = gVBoxAPI.UIVirtualBox.CreateHardDisk(data->vboxObj,
formatUtf16,
@ -5305,8 +5300,8 @@ vboxSnapshotRedefine(virDomainPtr dom,
* next define. This file is saved as "'machineLocation'/snapshot-'uuid'.xml"
*/
VIR_FREE(currentSnapshotXmlFilePath);
if (virAsprintf(&currentSnapshotXmlFilePath, "%s%s.xml", machineLocationPath, snapshotMachineDesc->currentSnapshot) < 0)
goto cleanup;
currentSnapshotXmlFilePath = g_strdup_printf("%s%s.xml",
machineLocationPath, snapshotMachineDesc->currentSnapshot);
char *snapshotContent = virDomainSnapshotDefFormat(NULL, def,
data->caps,
data->xmlopt,
@ -6893,8 +6888,7 @@ vboxDomainSnapshotDeleteMetadataOnly(virDomainSnapshotPtr snapshot)
goto cleanup;
}
VBOX_UTF16_TO_UTF8(machineNameUtf16, &machineName);
if (virAsprintf(&nameTmpUse, "%s.vbox", machineName) < 0)
goto cleanup;
nameTmpUse = g_strdup_printf("%s.vbox", machineName);
machineLocationPath = virStringReplace(settingsFilepath, nameTmpUse, "");
if (machineLocationPath == NULL) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@ -6978,9 +6972,8 @@ vboxDomainSnapshotDeleteMetadataOnly(virDomainSnapshotPtr snapshot)
VBOX_UTF16_FREE(locationUtf16);
VBOX_UTF8_TO_UTF16("VDI", &formatUtf16);
if (virAsprintf(&newLocationUtf8, "%sfakedisk-%s-%d.vdi",
machineLocationPath, def->parent.parent_name, it) < 0)
goto cleanup;
newLocationUtf8 = g_strdup_printf("%sfakedisk-%s-%d.vdi",
machineLocationPath, def->parent.parent_name, it);
VBOX_UTF8_TO_UTF16(newLocationUtf8, &newLocation);
rc = gVBoxAPI.UIVirtualBox.CreateHardDisk(data->vboxObj,
formatUtf16,
@ -7383,17 +7376,14 @@ vboxDomainScreenshot(virDomainPtr dom,
return NULL;
}
if ((privileged && virAsprintf(&cacheDir, "%s/cache/libvirt", LOCALSTATEDIR) < 0) ||
(!privileged && !(cacheDir = virGetUserCacheDirectory()))) {
if (privileged) {
cacheDir = g_strdup_printf("%s/cache/libvirt", LOCALSTATEDIR);
} else if (!(cacheDir = virGetUserCacheDirectory())) {
VBOX_RELEASE(machine);
return NULL;
}
if (virAsprintf(&tmp, "%s/vbox.screendump.XXXXXX", cacheDir) < 0) {
VBOX_RELEASE(machine);
VIR_FREE(cacheDir);
return NULL;
}
tmp = g_strdup_printf("%s/vbox.screendump.XXXXXX", cacheDir);
if ((tmp_fd = mkostemp(tmp, O_CLOEXEC)) == -1) {
virReportSystemError(errno, _("mkostemp(\"%s\") failed"), tmp);

View File

@ -432,8 +432,8 @@ vboxNetworkDefineCreateXML(virConnectPtr conn, const char *xml, bool start)
VBOX_UTF16_TO_UTF8(networkInterfaceNameUtf16, &networkInterfaceNameUtf8);
if (virAsprintf(&networkNameUtf8, "HostInterfaceNetworking-%s", networkInterfaceNameUtf8) < 0)
goto cleanup;
networkNameUtf8 = g_strdup_printf("HostInterfaceNetworking-%s",
networkInterfaceNameUtf8);
VBOX_UTF8_TO_UTF16(networkNameUtf8, &networkNameUtf16);
@ -588,8 +588,7 @@ vboxNetworkUndefineDestroy(virNetworkPtr network, bool removeinterface)
* show up in the net-list in virsh
*/
if (virAsprintf(&networkNameUtf8, "HostInterfaceNetworking-%s", network->name) < 0)
goto cleanup;
networkNameUtf8 = g_strdup_printf("HostInterfaceNetworking-%s", network->name);
VBOX_UTF8_TO_UTF16(network->name, &networkInterfaceNameUtf16);
@ -693,8 +692,7 @@ static int vboxNetworkCreate(virNetworkPtr network)
* server by giving the machine static IP
*/
if (virAsprintf(&networkNameUtf8, "HostInterfaceNetworking-%s", network->name) < 0)
goto cleanup;
networkNameUtf8 = g_strdup_printf("HostInterfaceNetworking-%s", network->name);
VBOX_UTF8_TO_UTF16(network->name, &networkInterfaceNameUtf16);
@ -791,8 +789,7 @@ static char *vboxNetworkGetXMLDesc(virNetworkPtr network, unsigned int flags)
def->ips = ipdef;
def->nips = 1;
if (virAsprintf(&networkNameUtf8, "HostInterfaceNetworking-%s", network->name) < 0)
goto cleanup;
networkNameUtf8 = g_strdup_printf("HostInterfaceNetworking-%s", network->name);
VBOX_UTF8_TO_UTF16(network->name, &networkInterfaceNameUtf16);

View File

@ -84,8 +84,7 @@ virVBoxSnapshotConfCreateVBoxSnapshotConfHardDiskPtr(xmlNodePtr diskNode,
}
if (location[0] != '/') {
/*The location is a relative path, so we must change it into an absolute one. */
if (virAsprintf(&tmp, "%s%s", machineLocation, location) < 0)
goto cleanup;
tmp = g_strdup_printf("%s%s", machineLocation, location);
hardDisk->location = g_strdup(tmp);
} else {
hardDisk->location = g_strdup(location);
@ -331,8 +330,7 @@ virVBoxSnapshotConfCreateHardDiskNode(virVBoxSnapshotConfHardDiskPtr hardDisk)
size_t i = 0;
char *uuid = NULL;
xmlNodePtr ret = xmlNewNode(NULL, BAD_CAST "HardDisk");
if (virAsprintf(&uuid, "{%s}", hardDisk->uuid) < 0)
goto cleanup;
uuid = g_strdup_printf("{%s}", hardDisk->uuid);
if (xmlNewProp(ret, BAD_CAST "uuid", BAD_CAST uuid) == NULL)
goto cleanup;
@ -378,8 +376,7 @@ virVBoxSnapshotConfSerializeSnapshot(xmlNodePtr node,
char **secondRegex = NULL;
int secondRegexResult = 0;
if (virAsprintf(&uuid, "{%s}", snapshot->uuid) < 0)
goto cleanup;
uuid = g_strdup_printf("{%s}", snapshot->uuid);
if (xmlNewProp(node, BAD_CAST "uuid", BAD_CAST uuid) == NULL)
goto cleanup;
@ -400,8 +397,7 @@ virVBoxSnapshotConfSerializeSnapshot(xmlNodePtr node,
goto cleanup;
if (secondRegexResult < 1)
goto cleanup;
if (virAsprintf(&timeStamp, "%sT%sZ", firstRegex[0], secondRegex[0]) < 0)
goto cleanup;
timeStamp = g_strdup_printf("%sT%sZ", firstRegex[0], secondRegex[0]);
if (xmlNewProp(node, BAD_CAST "timeStamp", BAD_CAST timeStamp) == NULL)
goto cleanup;
@ -1073,8 +1069,7 @@ virVBoxSnapshotConfSaveVboxFile(virVBoxSnapshotConfMachinePtr machine,
}
if (machine->currentSnapshot != NULL) {
if (virAsprintf(&currentSnapshot, "{%s}", machine->currentSnapshot) < 0)
goto cleanup;
currentSnapshot = g_strdup_printf("{%s}", machine->currentSnapshot);
if (!xmlNewProp(machineNode, BAD_CAST "currentSnapshot", BAD_CAST currentSnapshot)) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("Error in xmlNewProp"));
@ -1111,8 +1106,7 @@ virVBoxSnapshotConfSaveVboxFile(virVBoxSnapshotConfMachinePtr machine,
if (secondRegexResult < 1)
goto cleanup;
if (virAsprintf(&timeStamp, "%sT%sZ", firstRegex[0], secondRegex[0]) < 0)
goto cleanup;
timeStamp = g_strdup_printf("%sT%sZ", firstRegex[0], secondRegex[0]);
if (!xmlNewProp(machineNode, BAD_CAST "lastStateChange", BAD_CAST timeStamp)) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("Error in xmlNewProp"));

View File

@ -442,9 +442,8 @@ vboxStorageVolCreateXML(virStoragePoolPtr pool,
}
/* If target.path isn't given, use default path ~/.VirtualBox/image_name */
if (def->target.path == NULL &&
virAsprintf(&def->target.path, "%s/.VirtualBox/%s", virGetUserDirectory(), def->name) < 0)
goto cleanup;
if (!def->target.path)
def->target.path = g_strdup_printf("%s/.VirtualBox/%s", virGetUserDirectory(), def->name);
VBOX_UTF8_TO_UTF16(def->target.path, &hddNameUtf16);
if (!hddFormatUtf16 || !hddNameUtf16)

View File

@ -613,29 +613,23 @@ _virtualboxCreateMachine(vboxDriverPtr data, virDomainDefPtr def, IMachine **mac
{
vboxIID iid = VBOX_IID_INITIALIZER;
PRUnichar *machineNameUtf16 = NULL;
char *createFlags = NULL;
PRUnichar *createFlagsUtf16 = NULL;
nsresult rc = -1;
VBOX_UTF8_TO_UTF16(def->name, &machineNameUtf16);
vboxIIDFromUUID(&iid, def->uuid);
{
char *createFlags = NULL;
PRUnichar *createFlagsUtf16 = NULL;
if (virAsprintf(&createFlags,
"UUID=%s,forceOverwrite=0", uuidstr) < 0)
goto cleanup;
VBOX_UTF8_TO_UTF16(createFlags, &createFlagsUtf16);
rc = data->vboxObj->vtbl->CreateMachine(data->vboxObj,
NULL,
machineNameUtf16,
0,
nsnull,
nsnull,
createFlagsUtf16,
machine);
cleanup:
VIR_FREE(createFlags);
}
createFlags = g_strdup_printf("UUID=%s,forceOverwrite=0", uuidstr);
VBOX_UTF8_TO_UTF16(createFlags, &createFlagsUtf16);
rc = data->vboxObj->vtbl->CreateMachine(data->vboxObj,
NULL,
machineNameUtf16,
0,
nsnull,
nsnull,
createFlagsUtf16,
machine);
VIR_FREE(createFlags);
VBOX_UTF16_FREE(machineNameUtf16);
vboxIIDUnalloc(&iid);
return rc;