]> xenbits.xensource.com Git - libvirt.git/commitdiff
vbox: Use g_strdup_printf() instead of virAsprintf()
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 22 Oct 2019 13:26:14 +0000 (15:26 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Tue, 12 Nov 2019 15:15:58 +0000 (16:15 +0100)
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
src/vbox/vbox_MSCOMGlue.c
src/vbox/vbox_XPCOMCGlue.c
src/vbox/vbox_common.c
src/vbox/vbox_network.c
src/vbox/vbox_snapshot_conf.c
src/vbox/vbox_storage.c
src/vbox/vbox_tmpl.c

index db000385bc562f5601fe7850ad2675cfeac01ed9..18a291a0d8df2392f9ac9a075b3110890ba6bcbd 100644 (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 ||
index a20b1c2a36f1c32154e808a8ba0643cac289056d..f48a78a923aeef0b4c600b051c57c0c9c2645f81 100644 (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)
index cdbec15daec0a4cafaf4b631920db166c9a6265c..0bd47e3ddb89edb922a3e3f0ddd3b5923dd28208 100644 (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);
index 9e632ab7098848b6f788ad8aaca696dfc989cbb0..19b4d23ed8a3d5a8b7ef50ac9b2487977c6c7afa 100644 (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);
 
index 8cdfb35357dd4d552b014052f1da6ca5794be5f9..acac037208ab8952bc04bb121c8e1d09137bea8b 100644 (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"));
index 9bb3da4c57d4970168c420efb08b0d1473c98b5a..790b0e49972b7174c823f6ad8d0bd27017cf9562 100644 (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)
index 72011199cfe92c62eade4fcea590ddc1578f5f46..a5a907768b10b209563fe2925fe9511cbb7fda20 100644 (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;