[libvirt] [PATCH]: virAsprintf cleanup v2

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,
new version attached. It adds your suggestions and also uses virAsprintf
in the new src/logging.c.
 -- Guido
diff --git a/.x-sc_prohibit_asprintf b/.x-sc_prohibit_asprintf
new file mode 100644
index 0000000..614c238
--- /dev/null
+++ b/.x-sc_prohibit_asprintf
@@ -0,0 +1,3 @@
+^gnulib/
+^po/
+ChangeLog
diff --git a/Makefile.maint b/Makefile.maint
index 29c50fc..0f43b1c 100644
--- a/Makefile.maint
+++ b/Makefile.maint
@@ -105,6 +105,11 @@ sc_prohibit_strcmp:
 	  { echo '$(ME): use STREQ in place of the above uses of str''cmp' \
 		1>&2; exit 1; } || :
 
+# Use virAsprintf rather than a'sprintf since *strp is undefined on error.
+sc_prohibit_asprintf:
+	@grep -nE '\<[a]sprintf\>' $$($(VC_LIST_EXCEPT)) && \
+	  { echo '$(ME): use virAsprintf, not a'sprintf 1>&2; exit 1; } || :
+
 # Using EXIT_SUCCESS as the first argument to error is misleading,
 # since when that parameter is 0, error does not exit.  Use `0' instead.
 sc_error_exit_success:
diff --git a/src/cgroup.c b/src/cgroup.c
index be4cb12..1b4c27f 100644
--- a/src/cgroup.c
+++ b/src/cgroup.c
@@ -128,7 +128,7 @@ static int virCgroupPathOfGroup(const char *group,
         goto out;
     }
 
-    if (asprintf(path, "%s/%s", root->path, group) == -1)
+    if (virAsprintf(path, "%s/%s", root->path, group) == -1)
         rc = -ENOMEM;
 out:
     virCgroupFree(&root);
@@ -156,7 +156,7 @@ static int virCgroupPathOf(const char *grppath,
         goto out;
     }
 
-    if (asprintf(path, "%s/%s/%s", root->path, grppath, key) == -1)
+    if (virAsprintf(path, "%s/%s/%s", root->path, grppath, key) == -1)
         rc = -ENOMEM;
 out:
     virCgroupFree(&root);
@@ -212,7 +212,7 @@ static int virCgroupSetValueU64(virCgroupPtr group,
     char *strval = NULL;
     int rc;
 
-    if (asprintf(&strval, "%" PRIu64, value) == -1)
+    if (virAsprintf(&strval, "%" PRIu64, value) == -1)
         return -ENOMEM;
 
     rc = virCgroupSetValueStr(group, key, strval);
@@ -281,7 +281,7 @@ static int virCgroupSetValueI64(virCgroupPtr group,
     char *strval = NULL;
     int rc;
 
-    if (asprintf(&strval, "%" PRIi64, value) == -1)
+    if (virAsprintf(&strval, "%" PRIi64, value) == -1)
         return -ENOMEM;
 
     rc = virCgroupSetValueStr(group, key, strval);
@@ -341,7 +341,7 @@ static int _virCgroupInherit(const char *path,
 
     memset(buf, 0, sizeof(buf));
 
-    if (asprintf(&keypath, "%s/%s", path, key) == -1) {
+    if (virAsprintf(&keypath, "%s/%s", path, key) == -1) {
         rc = -ENOMEM;
         goto out;
     }
@@ -351,7 +351,7 @@ static int _virCgroupInherit(const char *path,
         goto out;
     }
 
-    if (asprintf(&pkeypath, "%s/../%s", path, key) == -1) {
+    if (virAsprintf(&pkeypath, "%s/../%s", path, key) == -1) {
         rc = -ENOMEM;
         VIR_FREE(keypath);
         goto out;
@@ -493,10 +493,10 @@ static int virCgroupNew(virCgroupPtr *parent,
         goto err;
     }
 
-    rc = asprintf(&((*newgroup)->path),
-                  "%s/%s",
-                  (*parent)->path,
-                  group);
+    rc = virAsprintf(&((*newgroup)->path),
+                     "%s/%s",
+                     (*parent)->path,
+                     group);
     if (rc == -1) {
         rc = -ENOMEM;
         goto err;
@@ -631,7 +631,7 @@ int virCgroupAddTask(virCgroupPtr group, pid_t pid)
         if (rc != 0)
             goto done;
 
-        if (asprintf(&taskpath, "%s/tasks", grppath) == -1) {
+        if (virAsprintf(&taskpath, "%s/tasks", grppath) == -1) {
             rc = -ENOMEM;
             goto done;
         }
@@ -642,7 +642,7 @@ int virCgroupAddTask(virCgroupPtr group, pid_t pid)
             goto done;
         }
 
-        if (asprintf(&pidstr, "%lu", (unsigned long)pid) == -1) {
+        if (virAsprintf(&pidstr, "%lu", (unsigned long)pid) == -1) {
             rc = -ENOMEM;
             goto done;
         }
@@ -745,7 +745,7 @@ int virCgroupAllowDevice(virCgroupPtr group,
     int rc;
     char *devstr = NULL;
 
-    if (asprintf(&devstr, "%c %i:%i rwm", type, major, minor) == -1) {
+    if (virAsprintf(&devstr, "%c %i:%i rwm", type, major, minor) == -1) {
         rc = -ENOMEM;
         goto out;
     }
@@ -775,7 +775,7 @@ int virCgroupAllowDeviceMajor(virCgroupPtr group,
     int rc;
     char *devstr = NULL;
 
-    if (asprintf(&devstr, "%c %i:* rwm", type, major) == -1) {
+    if (virAsprintf(&devstr, "%c %i:* rwm", type, major) == -1) {
         rc = -ENOMEM;
         goto out;
     }
diff --git a/src/domain_conf.c b/src/domain_conf.c
index 5374e17..f63b846 100644
--- a/src/domain_conf.c
+++ b/src/domain_conf.c
@@ -3457,7 +3457,7 @@ char *virDomainConfigFile(virConnectPtr conn,
 {
     char *ret = NULL;
 
-    if (asprintf(&ret, "%s/%s.xml", dir, name) < 0) {
+    if (virAsprintf(&ret, "%s/%s.xml", dir, name) < 0) {
         virDomainReportError(conn, VIR_ERR_NO_MEMORY, NULL);
         return NULL;
     }
diff --git a/src/driver.c b/src/driver.c
index 3d4ee3c..fda64dd 100644
--- a/src/driver.c
+++ b/src/driver.c
@@ -49,7 +49,7 @@ virDriverLoadModule(const char *name)
 
     DEBUG("Module load %s", name);
 
-    if (asprintf(&modfile, "%s/libvirt_driver_%s.so", moddir, name) < 0)
+    if (virAsprintf(&modfile, "%s/libvirt_driver_%s.so", moddir, name) < 0)
         return NULL;
 
     if (access(modfile, R_OK) < 0) {
@@ -63,8 +63,7 @@ virDriverLoadModule(const char *name)
         goto cleanup;
     }
 
-    if (asprintf(&regfunc, "%sRegister", name) < 0) {
-        regfunc = NULL;
+    if (virAsprintf(&regfunc, "%sRegister", name) < 0) {
         goto cleanup;
     }
 
diff --git a/src/libvirt.c b/src/libvirt.c
index 8922f08..37eaa63 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -941,8 +941,8 @@ do_open (const char *name,
                                    "Is the libvirtd daemon running ?");
             } else {
                 char *msg;
-                if (asprintf(&msg, "Is the %s daemon running?",
-                             virDeviceMonitorTab[i]->name) > 0) {
+                if (virAsprintf(&msg, "Is the %s daemon running?",
+                                virDeviceMonitorTab[i]->name) > 0) {
                     virLibConnWarning (NULL, VIR_WAR_NO_NODE, msg);
                     VIR_FREE(msg);
                 }
diff --git a/src/logging.c b/src/logging.c
index b1ff6df..9c899a6 100644
--- a/src/logging.c
+++ b/src/logging.c
@@ -512,15 +512,15 @@ void virLogMessage(const char *category, int priority, const char *funcname,
     localtime_r(&cur_time.tv_sec, &time_info);
 
     if ((funcname != NULL) && (priority == VIR_LOG_DEBUG)) {
-        ret = asprintf(&msg, "%02d:%02d:%02d.%03d: %s : %s:%lld : %s\n",
-                       time_info.tm_hour, time_info.tm_min,
-                       time_info.tm_sec, (int) cur_time.tv_usec / 1000,
-                       virLogPriorityString(priority), funcname, linenr, str);
+        ret = virAsprintf(&msg, "%02d:%02d:%02d.%03d: %s : %s:%lld : %s\n",
+                          time_info.tm_hour, time_info.tm_min,
+                          time_info.tm_sec, (int) cur_time.tv_usec / 1000,
+                          virLogPriorityString(priority), funcname, linenr, str);
     } else {
-        ret = asprintf(&msg, "%02d:%02d:%02d.%03d: %s : %s\n",
-                       time_info.tm_hour, time_info.tm_min,
-                       time_info.tm_sec, (int) cur_time.tv_usec / 1000,
-                       virLogPriorityString(priority), str);
+        ret = virAsprintf(&msg, "%02d:%02d:%02d.%03d: %s : %s\n",
+                          time_info.tm_hour, time_info.tm_min,
+                          time_info.tm_sec, (int) cur_time.tv_usec / 1000,
+                          virLogPriorityString(priority), str);
     }
     VIR_FREE(str);
     if (ret < 0) {
diff --git a/src/lxc_container.c b/src/lxc_container.c
index 1c5891c..50e8eaa 100644
--- a/src/lxc_container.c
+++ b/src/lxc_container.c
@@ -277,8 +277,7 @@ static int lxcContainerPivotRoot(virDomainFSDefPtr root)
         return -1;
     }
 
-    if (asprintf(&oldroot, "%s/.oldroot", root->src) < 0) {
-        oldroot = NULL;
+    if (virAsprintf(&oldroot, "%s/.oldroot", root->src) < 0) {
         lxcError(NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
         return -1;
     }
@@ -378,7 +377,7 @@ static int lxcContainerMountNewFS(virDomainDefPtr vmDef)
         if (vmDef->fss[i]->type != VIR_DOMAIN_FS_TYPE_MOUNT)
             continue;
 
-        if (asprintf(&src, "/.oldroot/%s", vmDef->fss[i]->src) < 0) {
+        if (virAsprintf(&src, "/.oldroot/%s", vmDef->fss[i]->src) < 0) {
             lxcError(NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
             return -1;
         }
diff --git a/src/lxc_controller.c b/src/lxc_controller.c
index da1af45..5f0793b 100644
--- a/src/lxc_controller.c
+++ b/src/lxc_controller.c
@@ -122,11 +122,10 @@ out:
 static char*lxcMonitorPath(virDomainDefPtr def)
 {
     char *sockpath;
-    if (asprintf(&sockpath, "%s/%s.sock",
-                 LXC_STATE_DIR, def->name) < 0) {
+
+    if (virAsprintf(&sockpath, "%s/%s.sock",
+                    LXC_STATE_DIR, def->name) < 0)
         lxcError(NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
-        return NULL;
-    }
     return sockpath;
 }
 
diff --git a/src/lxc_driver.c b/src/lxc_driver.c
index 12f6adc..97e297a 100644
--- a/src/lxc_driver.c
+++ b/src/lxc_driver.c
@@ -610,8 +610,8 @@ static int lxcMonitorClient(virConnectPtr conn,
     int fd;
     struct sockaddr_un addr;
 
-    if (asprintf(&sockpath, "%s/%s.sock",
-                 driver->stateDir, vm->def->name) < 0) {
+    if (virAsprintf(&sockpath, "%s/%s.sock",
+                    driver->stateDir, vm->def->name) < 0) {
         lxcError(conn, NULL, VIR_ERR_NO_MEMORY, NULL);
         return -1;
     }
@@ -854,8 +854,8 @@ static int lxcVmStart(virConnectPtr conn,
         return -1;
     }
 
-    if (asprintf(&logfile, "%s/%s.log",
-                 driver->logDir, vm->def->name) < 0) {
+    if (virAsprintf(&logfile, "%s/%s.log",
+                    driver->logDir, vm->def->name) < 0) {
         lxcError(conn, NULL, VIR_ERR_NO_MEMORY, NULL);
         return -1;
     }
diff --git a/src/network_conf.c b/src/network_conf.c
index 21dbc67..b0c16e6 100644
--- a/src/network_conf.c
+++ b/src/network_conf.c
@@ -375,7 +375,7 @@ virNetworkDefParseXML(virConnectPtr conn,
         inaddress.s_addr &= innetmask.s_addr;
         netaddr = inet_ntoa(inaddress);
 
-        if (asprintf(&def->network, "%s/%s", netaddr, def->netmask) < 0) {
+        if (virAsprintf(&def->network, "%s/%s", netaddr, def->netmask) < 0) {
             virNetworkReportError(conn, VIR_ERR_NO_MEMORY, NULL);
             goto error;
         }
@@ -643,16 +643,14 @@ int virNetworkSaveConfig(virConnectPtr conn,
     int err;
 
     if (!net->configFile &&
-        asprintf(&net->configFile, "%s/%s.xml",
-                 configDir, net->def->name) < 0) {
-        net->configFile = NULL;
+        virAsprintf(&net->configFile, "%s/%s.xml",
+                    configDir, net->def->name) < 0) {
         virNetworkReportError(conn, VIR_ERR_NO_MEMORY, NULL);
         goto cleanup;
     }
     if (!net->autostartLink &&
-        asprintf(&net->autostartLink, "%s/%s.xml",
-                 autostartDir, net->def->name) < 0) {
-        net->autostartLink = NULL;
+        virAsprintf(&net->autostartLink, "%s/%s.xml",
+                    autostartDir, net->def->name) < 0) {
         virNetworkReportError(conn, VIR_ERR_NO_MEMORY, NULL);
         goto cleanup;
     }
@@ -720,15 +718,13 @@ virNetworkObjPtr virNetworkLoadConfig(virConnectPtr conn,
     virNetworkObjPtr net;
     int autostart;
 
-    if (asprintf(&configFile, "%s/%s",
-                 configDir, file) < 0) {
-        configFile = NULL;
+    if (virAsprintf(&configFile, "%s/%s",
+                    configDir, file) < 0) {
         virNetworkReportError(conn, VIR_ERR_NO_MEMORY, NULL);
         goto error;
     }
-    if (asprintf(&autostartLink, "%s/%s",
-                 autostartDir, file) < 0) {
-        autostartLink = NULL;
+    if (virAsprintf(&autostartLink, "%s/%s",
+                    autostartDir, file) < 0) {
         virNetworkReportError(conn, VIR_ERR_NO_MEMORY, NULL);
         goto error;
     }
diff --git a/src/network_driver.c b/src/network_driver.c
index 0978341..04671aa 100644
--- a/src/network_driver.c
+++ b/src/network_driver.c
@@ -138,8 +138,8 @@ networkStartup(void) {
     networkDriverLock(driverState);
 
     if (!uid) {
-        if (asprintf(&driverState->logDir,
-                     "%s/log/libvirt/qemu", LOCAL_STATE_DIR) == -1)
+        if (virAsprintf(&driverState->logDir,
+                        "%s/log/libvirt/qemu", LOCAL_STATE_DIR) == -1)
             goto out_of_memory;
 
         if ((base = strdup (SYSCONF_DIR "/libvirt")) == NULL)
@@ -151,13 +151,11 @@ networkStartup(void) {
             goto out_of_memory;
         }
 
-        if (asprintf(&driverState->logDir,
-                     "%s/.libvirt/qemu/log", pw->pw_dir) == -1)
+        if (virAsprintf(&driverState->logDir,
+                        "%s/.libvirt/qemu/log", pw->pw_dir) == -1)
             goto out_of_memory;
 
-        if (asprintf (&base, "%s/.libvirt", pw->pw_dir) == -1) {
-            networkLog (NETWORK_ERR,
-                      "%s", _("out of memory in asprintf\n"));
+        if (virAsprintf(&base, "%s/.libvirt", pw->pw_dir) == -1) {
             goto out_of_memory;
         }
     }
@@ -165,11 +163,11 @@ networkStartup(void) {
     /* Configuration paths are either ~/.libvirt/qemu/... (session) or
      * /etc/libvirt/qemu/... (system).
      */
-    if (asprintf (&driverState->networkConfigDir, "%s/qemu/networks", base) == -1)
+    if (virAsprintf(&driverState->networkConfigDir, "%s/qemu/networks", base) == -1)
         goto out_of_memory;
 
-    if (asprintf (&driverState->networkAutostartDir, "%s/qemu/networks/autostart",
-                  base) == -1)
+    if (virAsprintf(&driverState->networkAutostartDir, "%s/qemu/networks/autostart",
+                    base) == -1)
         goto out_of_memory;
 
     VIR_FREE(base);
diff --git a/src/openvz_conf.c b/src/openvz_conf.c
index 49cc183..6fa0212 100644
--- a/src/openvz_conf.c
+++ b/src/openvz_conf.c
@@ -406,10 +406,8 @@ int openvzLoadDomains(struct openvz_driver *driver) {
         dom->pid = veid;
         dom->def->id = dom->state == VIR_DOMAIN_SHUTOFF ? -1 : veid;
 
-        if (asprintf(&dom->def->name, "%i", veid) < 0) {
-            dom->def->name = NULL;
+        if (virAsprintf(&dom->def->name, "%i", veid) < 0)
             goto no_memory;
-        }
 
         openvzGetVPSUUID(veid, uuidstr);
         ret = virUUIDParse(uuidstr, dom->def->uuid);
diff --git a/src/qemu_conf.c b/src/qemu_conf.c
index f78678e..5d84f85 100644
--- a/src/qemu_conf.c
+++ b/src/qemu_conf.c
@@ -708,9 +708,8 @@ int qemudBuildCommandLine(virConnectPtr conn,
     do {                                                                \
         ADD_ARG_LIT("-usbdevice");                                      \
         ADD_ARG_SPACE;                                                  \
-        if ((asprintf((char **)&(qargv[qargc++]),                       \
-                      "disk:%s", thisarg)) == -1) {                     \
-            qargv[qargc-1] = NULL;                                      \
+        if ((virAsprintf((char **)&(qargv[qargc++]),                    \
+                         "disk:%s", thisarg)) == -1) {                  \
             goto no_memory;                                             \
         }                                                               \
     } while (0)
@@ -743,7 +742,7 @@ int qemudBuildCommandLine(virConnectPtr conn,
         char *envval;                                                   \
         ADD_ENV_SPACE;                                                  \
         if (val != NULL) {                                              \
-            if (asprintf(&envval, "%s=%s", envname, val) < 0)           \
+            if (virAsprintf(&envval, "%s=%s", envname, val) < 0)        \
                 goto no_memory;                                         \
             qenv[qenvc++] = envval;                                     \
         }                                                               \
@@ -1157,12 +1156,12 @@ int qemudBuildCommandLine(virConnectPtr conn,
         char *display = NULL;
 
         if (vm->def->graphics->data.sdl.xauth &&
-            asprintf(&xauth, "XAUTHORITY=%s",
-                     vm->def->graphics->data.sdl.xauth) < 0)
+            virAsprintf(&xauth, "XAUTHORITY=%s",
+                        vm->def->graphics->data.sdl.xauth) < 0)
             goto no_memory;
         if (vm->def->graphics->data.sdl.display &&
-            asprintf(&display, "DISPLAY=%s",
-                     vm->def->graphics->data.sdl.display) < 0) {
+            virAsprintf(&display, "DISPLAY=%s",
+                        vm->def->graphics->data.sdl.display) < 0) {
             VIR_FREE(xauth);
             goto no_memory;
         }
@@ -1209,19 +1208,18 @@ int qemudBuildCommandLine(virConnectPtr conn,
         if (hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
             hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
             if(hostdev->source.subsys.u.usb.vendor) {
-                    ret = asprintf(&usbdev, "host:%.4x:%.4x",
+                    ret = virAsprintf(&usbdev, "host:%.4x:%.4x",
                                hostdev->source.subsys.u.usb.vendor,
                                hostdev->source.subsys.u.usb.product);
 
             } else {
-                    ret = asprintf(&usbdev, "host:%.3d.%.3d",
+                    ret = virAsprintf(&usbdev, "host:%.3d.%.3d",
                                hostdev->source.subsys.u.usb.bus,
                                hostdev->source.subsys.u.usb.device);
             }
-            if (ret < 0) {
-                usbdev = NULL;
+            if (ret < 0)
                 goto error;
-            }
+
             ADD_ARG_LIT("-usbdevice");
             ADD_ARG_LIT(usbdev);
             VIR_FREE(usbdev);
diff --git a/src/qemu_driver.c b/src/qemu_driver.c
index be7dd48..1e86a2a 100644
--- a/src/qemu_driver.c
+++ b/src/qemu_driver.c
@@ -252,8 +252,8 @@ qemudStartup(void) {
         goto error;
 
     if (!uid) {
-        if (asprintf(&qemu_driver->logDir,
-                     "%s/log/libvirt/qemu", LOCAL_STATE_DIR) == -1)
+        if (virAsprintf(&qemu_driver->logDir,
+                        "%s/log/libvirt/qemu", LOCAL_STATE_DIR) == -1)
             goto out_of_memory;
 
         if ((base = strdup (SYSCONF_DIR "/libvirt")) == NULL)
@@ -269,11 +269,11 @@ qemudStartup(void) {
             goto error;
         }
 
-        if (asprintf(&qemu_driver->logDir,
-                     "%s/.libvirt/qemu/log", pw->pw_dir) == -1)
+        if (virAsprintf(&qemu_driver->logDir,
+                        "%s/.libvirt/qemu/log", pw->pw_dir) == -1)
             goto out_of_memory;
 
-        if (asprintf (&base, "%s/.libvirt", pw->pw_dir) == -1)
+        if (virAsprintf(&base, "%s/.libvirt", pw->pw_dir) == -1)
             goto out_of_memory;
 
         if (virAsprintf(&qemu_driver->stateDir, "%s/qemu/run", base) == -1)
@@ -293,10 +293,10 @@ qemudStartup(void) {
         goto out_of_memory;
     driverConf[sizeof(driverConf)-1] = '\0';
 
-    if (asprintf (&qemu_driver->configDir, "%s/qemu", base) == -1)
+    if (virAsprintf(&qemu_driver->configDir, "%s/qemu", base) == -1)
         goto out_of_memory;
 
-    if (asprintf (&qemu_driver->autostartDir, "%s/qemu/autostart", base) == -1)
+    if (virAsprintf(&qemu_driver->autostartDir, "%s/qemu/autostart", base) == -1)
         goto out_of_memory;
 
     VIR_FREE(base);
@@ -2251,7 +2251,7 @@ static int qemudDomainSave(virDomainPtr dom,
                          "%s", _("out of memory"));
         goto cleanup;
     }
-    if (asprintf (&command, "migrate \"exec:"
+    if (virAsprintf(&command, "migrate \"exec:"
                   "dd of='%s' oflag=append conv=notrunc 2>/dev/null"
                   "\"", safe_path) == -1) {
         qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
@@ -2878,21 +2878,21 @@ static char *qemudDiskDeviceName(const virConnectPtr conn,
     switch (disk->bus) {
         case VIR_DOMAIN_DISK_BUS_IDE:
             if (disk->device== VIR_DOMAIN_DISK_DEVICE_DISK)
-                ret = asprintf(&devname, "ide%d-hd%d", busid, devid);
+                ret = virAsprintf(&devname, "ide%d-hd%d", busid, devid);
             else
-                ret = asprintf(&devname, "ide%d-cd%d", busid, devid);
+                ret = virAsprintf(&devname, "ide%d-cd%d", busid, devid);
             break;
         case VIR_DOMAIN_DISK_BUS_SCSI:
             if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK)
-                ret = asprintf(&devname, "scsi%d-hd%d", busid, devid);
+                ret = virAsprintf(&devname, "scsi%d-hd%d", busid, devid);
             else
-                ret = asprintf(&devname, "scsi%d-cd%d", busid, devid);
+                ret = virAsprintf(&devname, "scsi%d-cd%d", busid, devid);
             break;
         case VIR_DOMAIN_DISK_BUS_FDC:
-            ret = asprintf(&devname, "floppy%d", devid);
+            ret = virAsprintf(&devname, "floppy%d", devid);
             break;
         case VIR_DOMAIN_DISK_BUS_VIRTIO:
-            ret = asprintf(&devname, "virtio%d", devid);
+            ret = virAsprintf(&devname, "virtio%d", devid);
             break;
         default:
             qemudReportError(conn, NULL, NULL, VIR_ERR_NO_SUPPORT,
@@ -2978,7 +2978,7 @@ static int qemudDomainChangeEjectableMedia(virConnectPtr conn,
             VIR_FREE(devname);
             return -1;
         }
-        if (asprintf (&cmd, "change %s \"%s\"", devname, safe_path) == -1) {
+        if (virAsprintf(&cmd, "change %s \"%s\"", devname, safe_path) == -1) {
             qemudReportError(conn, dom, NULL, VIR_ERR_NO_MEMORY, NULL);
             VIR_FREE(safe_path);
             VIR_FREE(devname);
@@ -2986,7 +2986,7 @@ static int qemudDomainChangeEjectableMedia(virConnectPtr conn,
         }
         VIR_FREE(safe_path);
 
-    } else if (asprintf(&cmd, "eject %s", devname) == -1) {
+    } else if (virAsprintf(&cmd, "eject %s", devname) == -1) {
         qemudReportError(conn, dom, NULL, VIR_ERR_NO_MEMORY, NULL);
         VIR_FREE(devname);
         return -1;
@@ -3050,8 +3050,8 @@ static int qemudDomainAttachPciDiskDevice(virConnectPtr conn,
         return -1;
     }
 
-    ret = asprintf(&cmd, "pci_add 0 storage file=%s,if=%s",
-                         safe_path, type);
+    ret = virAsprintf(&cmd, "pci_add 0 storage file=%s,if=%s",
+                      safe_path, type);
     VIR_FREE(safe_path);
     if (ret == -1) {
         qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
@@ -3120,7 +3120,7 @@ static int qemudDomainAttachUsbMassstorageDevice(virConnectPtr conn,
         return -1;
     }
 
-    ret = asprintf(&cmd, "usb_add disk:%s", safe_path);
+    ret = virAsprintf(&cmd, "usb_add disk:%s", safe_path);
     VIR_FREE(safe_path);
     if (ret == -1) {
         qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
@@ -3168,13 +3168,13 @@ static int qemudDomainAttachHostDevice(virConnectPtr conn,
     }
 
     if (dev->data.hostdev->source.subsys.u.usb.vendor) {
-        ret = asprintf(&cmd, "usb_add host:%.4x:%.4x",
-                       dev->data.hostdev->source.subsys.u.usb.vendor,
-                       dev->data.hostdev->source.subsys.u.usb.product);
+        ret = virAsprintf(&cmd, "usb_add host:%.4x:%.4x",
+                          dev->data.hostdev->source.subsys.u.usb.vendor,
+                          dev->data.hostdev->source.subsys.u.usb.product);
     } else {
-        ret = asprintf(&cmd, "usb_add host:%.3d.%.3d",
-                       dev->data.hostdev->source.subsys.u.usb.bus,
-                       dev->data.hostdev->source.subsys.u.usb.device);
+        ret = virAsprintf(&cmd, "usb_add host:%.3d.%.3d",
+                          dev->data.hostdev->source.subsys.u.usb.bus,
+                          dev->data.hostdev->source.subsys.u.usb.device);
     }
     if (ret == -1) {
         qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
@@ -3304,9 +3304,8 @@ static int qemudDomainDetachPciDiskDevice(virConnectPtr conn,
         goto cleanup;
     }
 
-    if (asprintf(&cmd, "pci_del 0 %d", detach->slotnum) < 0) {
+    if (virAsprintf(&cmd, "pci_del 0 %d", detach->slotnum) < 0) {
         qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
-        cmd = NULL;
         goto cleanup;
     }
 
@@ -3977,10 +3976,9 @@ qemudDomainMigratePrepare2 (virConnectPtr dconn,
         }
 
         /* Caller frees */
-        if (asprintf(uri_out, "tcp:%s:%d", hostname, this_port) < 0) {
+        if (virAsprintf(uri_out, "tcp:%s:%d", hostname, this_port) < 0) {
             qemudReportError (dconn, NULL, NULL, VIR_ERR_NO_MEMORY,
                               "%s", strerror (errno));
-            *uri_out = NULL;
             goto cleanup;
         }
     } else {
diff --git a/src/remote_internal.c b/src/remote_internal.c
index 2003a0e..f1608a9 100644
--- a/src/remote_internal.c
+++ b/src/remote_internal.c
@@ -347,7 +347,7 @@ doRemoteOpen (virConnectPtr conn,
 
     /* Remote server defaults to "localhost" if not specified. */
     if (conn->uri && conn->uri->port != 0) {
-        if (asprintf (&port, "%d", conn->uri->port) == -1) goto out_of_memory;
+        if (virAsprintf(&port, "%d", conn->uri->port) == -1) goto out_of_memory;
     } else if (transport == trans_tls) {
         port = strdup (LIBVIRTD_TLS_PORT);
         if (!port) goto out_of_memory;
@@ -582,10 +582,9 @@ doRemoteOpen (virConnectPtr conn,
                     goto failed;
                 }
 
-                if (asprintf (&sockname, "@%s" LIBVIRTD_USER_UNIX_SOCKET, pw->pw_dir) < 0) {
-                    sockname = NULL;
+                if (virAsprintf(&sockname, "@%s" LIBVIRTD_USER_UNIX_SOCKET, pw->pw_dir) < 0)
                     goto out_of_memory;
-                }
+
             } else {
                 if (flags & VIR_DRV_OPEN_REMOTE_RO)
                     sockname = strdup (LIBVIRTD_PRIV_UNIX_SOCKET_RO);
diff --git a/src/stats_linux.c b/src/stats_linux.c
index a079993..b58457d 100644
--- a/src/stats_linux.c
+++ b/src/stats_linux.c
@@ -292,9 +292,9 @@ xenLinuxDomainDeviceID(virConnectPtr conn, int domid, const char *path)
      */
 
     if (strlen(path) >= 5 && STRPREFIX(path, "/dev/"))
-        retval = asprintf(&mod_path, "%s", path);
+        retval = virAsprintf(&mod_path, "%s", path);
     else
-        retval = asprintf(&mod_path, "/dev/%s", path);
+        retval = virAsprintf(&mod_path, "/dev/%s", path);
 
     if (retval < 0) {
         statsErrorFunc (conn, VIR_ERR_NO_MEMORY, __FUNCTION__,
diff --git a/src/storage_backend_iscsi.c b/src/storage_backend_iscsi.c
index b347fa9..7731d07 100644
--- a/src/storage_backend_iscsi.c
+++ b/src/storage_backend_iscsi.c
@@ -180,12 +180,12 @@ virStorageBackendISCSINewLun(virConnectPtr conn, virStoragePoolObjPtr pool,
 
     vol->type = VIR_STORAGE_VOL_BLOCK;
 
-    if (asprintf(&(vol->name), "lun-%d", lun) < 0) {
+    if (virAsprintf(&(vol->name), "lun-%d", lun) < 0) {
         virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("name"));
         goto cleanup;
     }
 
-    if (asprintf(&devpath, "/dev/%s", dev) < 0) {
+    if (virAsprintf(&devpath, "/dev/%s", dev) < 0) {
         virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("devpath"));
         goto cleanup;
     }
diff --git a/src/storage_driver.c b/src/storage_driver.c
index e92f122..0484219 100644
--- a/src/storage_driver.c
+++ b/src/storage_driver.c
@@ -126,8 +126,8 @@ storageDriverStartup(void) {
             goto out_of_memory;
         }
 
-        if (asprintf (&base, "%s/.libvirt", pw->pw_dir) == -1) {
-            storageLog("out of memory in asprintf");
+        if (virAsprintf(&base, "%s/.libvirt", pw->pw_dir) == -1) {
+            storageLog("out of memory in virAsprintf");
             goto out_of_memory;
         }
     }
@@ -140,12 +140,12 @@ storageDriverStartup(void) {
         goto out_of_memory;
     driverConf[sizeof(driverConf)-1] = '\0';
 
-    if (asprintf (&driverState->configDir,
-                  "%s/storage", base) == -1)
+    if (virAsprintf(&driverState->configDir,
+                    "%s/storage", base) == -1)
         goto out_of_memory;
 
-    if (asprintf (&driverState->autostartDir,
-                  "%s/storage/autostart", base) == -1)
+    if (virAsprintf(&driverState->autostartDir,
+                    "%s/storage/autostart", base) == -1)
         goto out_of_memory;
 
     VIR_FREE(base);
diff --git a/src/uml_conf.c b/src/uml_conf.c
index a7c0271..f2bf09f 100644
--- a/src/uml_conf.c
+++ b/src/uml_conf.c
@@ -99,29 +99,29 @@ umlBuildCommandLineChr(virConnectPtr conn,
 
     switch (def->type) {
     case VIR_DOMAIN_CHR_TYPE_NULL:
-        if (asprintf(&ret, "%s%d=null", dev, def->dstPort) < 0) {
+        if (virAsprintf(&ret, "%s%d=null", dev, def->dstPort) < 0) {
             umlReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
             return NULL;
         }
         break;
 
     case VIR_DOMAIN_CHR_TYPE_PTY:
-        if (asprintf(&ret, "%s%d=pts", dev, def->dstPort) < 0) {
+        if (virAsprintf(&ret, "%s%d=pts", dev, def->dstPort) < 0) {
             umlReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
             return NULL;
         }
         break;
 
     case VIR_DOMAIN_CHR_TYPE_DEV:
-        if (asprintf(&ret, "%s%d=tty:%s", dev, def->dstPort,
-                     def->data.file.path) < 0) {
+        if (virAsprintf(&ret, "%s%d=tty:%s", dev, def->dstPort,
+                        def->data.file.path) < 0) {
             umlReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
             return NULL;
         }
         break;
 
     case VIR_DOMAIN_CHR_TYPE_STDIO:
-        if (asprintf(&ret, "%s%d=fd:0,fd:1", dev, def->dstPort) < 0) {
+        if (virAsprintf(&ret, "%s%d=fd:0,fd:1", dev, def->dstPort) < 0) {
             umlReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
             return NULL;
         }
@@ -134,8 +134,8 @@ umlBuildCommandLineChr(virConnectPtr conn,
             return NULL;
         }
 
-        if (asprintf(&ret, "%s%d=port:%s", dev, def->dstPort,
-                     def->data.tcp.service) < 0) {
+        if (virAsprintf(&ret, "%s%d=port:%s", dev, def->dstPort,
+                        def->data.tcp.service) < 0) {
             umlReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
             return NULL;
         }
@@ -204,9 +204,9 @@ int umlBuildCommandLine(virConnectPtr conn,
     do {                                                                \
         char *arg;                                                      \
         ADD_ARG_SPACE;                                                  \
-        if (asprintf(&arg, "%s=%s", key, val) < 0)                      \
+        if (virAsprintf(&arg, "%s=%s", key, val) < 0)                   \
             goto no_memory;                                             \
-        qargv[qargc++] = arg;                                            \
+        qargv[qargc++] = arg;                                           \
     } while (0)
 
 
@@ -238,7 +238,7 @@ int umlBuildCommandLine(virConnectPtr conn,
         char *envval;                                                   \
         ADD_ENV_SPACE;                                                  \
         if (val != NULL) {                                              \
-            if (asprintf(&envval, "%s=%s", envname, val) < 0)           \
+            if (virAsprintf(&envval, "%s=%s", envname, val) < 0)        \
                 goto no_memory;                                         \
             qenv[qenvc++] = envval;                                     \
         }                                                               \
@@ -281,7 +281,7 @@ int umlBuildCommandLine(virConnectPtr conn,
         if (i == 0 && vm->def->console)
             ret = umlBuildCommandLineChr(conn, vm->def->console, "con");
         else
-            if (asprintf(&ret, "con%d=none", i) < 0)
+            if (virAsprintf(&ret, "con%d=none", i) < 0)
                 goto no_memory;
         ADD_ARG(ret);
     }
@@ -295,7 +295,7 @@ int umlBuildCommandLine(virConnectPtr conn,
         if (chr)
             ret = umlBuildCommandLineChr(conn, chr, "ssl");
         else
-            if (asprintf(&ret, "ssl%d=none", i) < 0)
+            if (virAsprintf(&ret, "ssl%d=none", i) < 0)
                 goto no_memory;
         ADD_ARG(ret);
     }
diff --git a/src/uml_driver.c b/src/uml_driver.c
index ea71800..1562042 100644
--- a/src/uml_driver.c
+++ b/src/uml_driver.c
@@ -158,7 +158,7 @@ umlIdentifyOneChrPTY(virConnectPtr conn,
     char *cmd;
     char *res = NULL;
     int retries = 0;
-    if (asprintf(&cmd, "config %s%d", dev, def->dstPort) < 0) {
+    if (virAsprintf(&cmd, "config %s%d", dev, def->dstPort) < 0) {
         umlReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
         return -1;
     }
@@ -327,23 +327,23 @@ umlStartup(void) {
     }
 
     if (!uid) {
-        if (asprintf(&uml_driver->logDir,
-                     "%s/log/libvirt/uml", LOCAL_STATE_DIR) == -1)
+        if (virAsprintf(&uml_driver->logDir,
+                        "%s/log/libvirt/uml", LOCAL_STATE_DIR) == -1)
             goto out_of_memory;
 
         if ((base = strdup (SYSCONF_DIR "/libvirt")) == NULL)
             goto out_of_memory;
     } else {
-        if (asprintf(&uml_driver->logDir,
-                     "%s/.libvirt/uml/log", pw->pw_dir) == -1)
+        if (virAsprintf(&uml_driver->logDir,
+                        "%s/.libvirt/uml/log", pw->pw_dir) == -1)
             goto out_of_memory;
 
-        if (asprintf (&base, "%s/.libvirt", pw->pw_dir) == -1)
+        if (virAsprintf(&base, "%s/.libvirt", pw->pw_dir) == -1)
             goto out_of_memory;
     }
 
-    if (asprintf (&uml_driver->monitorDir,
-                  "%s/.uml", pw->pw_dir) == -1)
+    if (virAsprintf(&uml_driver->monitorDir,
+                    "%s/.uml", pw->pw_dir) == -1)
         goto out_of_memory;
 
     /* Configuration paths are either ~/.libvirt/uml/... (session) or
@@ -353,10 +353,10 @@ umlStartup(void) {
         goto out_of_memory;
     driverConf[sizeof(driverConf)-1] = '\0';
 
-    if (asprintf (&uml_driver->configDir, "%s/uml", base) == -1)
+    if (virAsprintf(&uml_driver->configDir, "%s/uml", base) == -1)
         goto out_of_memory;
 
-    if (asprintf (&uml_driver->autostartDir, "%s/uml/autostart", base) == -1)
+    if (virAsprintf(&uml_driver->autostartDir, "%s/uml/autostart", base) == -1)
         goto out_of_memory;
 
     VIR_FREE(base);
@@ -517,8 +517,8 @@ static int umlReadPidFile(virConnectPtr conn,
     int retries = 0;
 
     vm->pid = -1;
-    if (asprintf(&pidfile, "%s/%s/pid",
-                 driver->monitorDir, vm->def->name) < 0) {
+    if (virAsprintf(&pidfile, "%s/%s/pid",
+                    driver->monitorDir, vm->def->name) < 0) {
         umlReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
         return -1;
     }
@@ -558,8 +558,8 @@ static int umlMonitorAddress(virConnectPtr conn,
                              struct sockaddr_un *addr) {
     char *sockname;
 
-    if (asprintf(&sockname, "%s/%s/mconsole",
-                 driver->monitorDir, vm->def->name) < 0) {
+    if (virAsprintf(&sockname, "%s/%s/mconsole",
+                    driver->monitorDir, vm->def->name) < 0) {
         umlReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
         return -1;
     }
@@ -749,8 +749,8 @@ static int umlStartVMDaemon(virConnectPtr conn,
         return -1;
     }
 
-    if (asprintf(&logfile, "%s/%s.log",
-                 driver->logDir, vm->def->name) < 0) {
+    if (virAsprintf(&logfile, "%s/%s.log",
+                    driver->logDir, vm->def->name) < 0) {
         umlReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
         return -1;
     }
diff --git a/src/util.c b/src/util.c
index 6c5ba40..f324bdf 100644
--- a/src/util.c
+++ b/src/util.c
@@ -922,11 +922,8 @@ int virFileOpenTty(int *ttymaster ATTRIBUTE_UNUSED,
 
 char* virFilePid(const char *dir, const char* name)
 {
-    char* pidfile;
-
-    if (asprintf(&pidfile, "%s/%s.pid", dir, name) < 0) {
-        pidfile = NULL;
-    }
+    char *pidfile;
+    virAsprintf(&pidfile, "%s/%s.pid", dir, name);
     return pidfile;
 }
 
@@ -1170,7 +1167,7 @@ virParseNumber(const char **str)
 /**
  * virAsprintf
  *
- * like asprintf but makes sure *strp == NULL on failure
+ * like glibc's_asprintf but makes sure *strp == NULL on failure
  */
 int
 virAsprintf(char **strp, const char *fmt, ...)
diff --git a/src/veth.c b/src/veth.c
index ff9bf4d..93173e4 100644
--- a/src/veth.c
+++ b/src/veth.c
@@ -203,7 +203,7 @@ int moveInterfaceToNetNs(const char* interface, int pidInNs)
         goto error_out;
     }
 
-    if (asprintf(&pid, "%d", pidInNs) == -1)
+    if (virAsprintf(&pid, "%d", pidInNs) == -1)
         goto error_out;
 
     argv[5] = pid;
diff --git a/src/virsh.c b/src/virsh.c
index 7828272..948d31e 100644
--- a/src/virsh.c
+++ b/src/virsh.c
@@ -3457,19 +3457,19 @@ cmdPoolDiscoverSourcesAs(vshControl * ctl, const vshCmd * cmd ATTRIBUTE_UNUSED)
             }
         }
         ret = port ?
-            asprintf(&srcSpec,
-                     "<source><host name='%.*s' port='%s'/></source>",
-                     (int)hostlen, host, port) :
-            asprintf(&srcSpec,
-                     "<source><host name='%.*s'/></source>",
-                     (int)hostlen, host);
+            virAsprintf(&srcSpec,
+                        "<source><host name='%.*s' port='%s'/></source>",
+                        (int)hostlen, host, port) :
+            virAsprintf(&srcSpec,
+                        "<source><host name='%.*s'/></source>",
+                        (int)hostlen, host);
         if (ret < 0) {
             switch (errno) {
             case ENOMEM:
                 vshError(ctl, FALSE, "%s", _("Out of memory"));
                 break;
             default:
-                vshError(ctl, FALSE, _("asprintf failed (errno %d)"), errno);
+                vshError(ctl, FALSE, _("virAsprintf failed (errno %d)"), errno);
             }
             return FALSE;
         }
@@ -5341,9 +5341,9 @@ editFile (vshControl *ctl, const char *filename)
         return -1;
     }
 
-    if (asprintf (&command, "%s %s", editor, filename) == -1) {
+    if (virAsprintf(&command, "%s %s", editor, filename) == -1) {
         vshError(ctl, FALSE,
-                 _("asprintf: could not create editing command: %s"),
+                 _("virAsprintf: could not create editing command: %s"),
                  strerror (errno));
         return -1;
     }
diff --git a/src/xm_internal.c b/src/xm_internal.c
index 77aa3e1..b6d5c01 100644
--- a/src/xm_internal.c
+++ b/src/xm_internal.c
@@ -2687,16 +2687,14 @@ xenXMDomainBlockPeek (virDomainPtr dom,
 static char *xenXMAutostartLinkName(virDomainPtr dom)
 {
     char *ret;
-    if (asprintf(&ret, "/etc/xen/auto/%s", dom->name) < 0)
-        return NULL;
+    virAsprintf(&ret, "/etc/xen/auto/%s", dom->name);
     return ret;
 }
 
 static char *xenXMDomainConfigName(virDomainPtr dom)
 {
     char *ret;
-    if (asprintf(&ret, "/etc/xen/%s", dom->name) < 0)
-        return NULL;
+    virAsprintf(&ret, "/etc/xen/%s", dom->name);
     return ret;
 }
 
--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]