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

[libvirt] [PATCH] qemu: use virAsprintf instead of PATH_MAX



* src/qemu/qemu_driver.c (qemudLogFD, qemudLogReadFD)
(qemudStartup, qemudGetProcessInfo)
(qemudDomainDetachPciDiskDevice)
(qemudDomainDetachSCSIDiskDevice): Use heap instead of stack.
---
 src/qemu/qemu_driver.c |   72 ++++++++++++++++++++++++------------------------
 1 files changed, 36 insertions(+), 36 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 75f4563..6739b89 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -744,13 +744,11 @@ static int qemuCgroupControllerActive(struct qemud_driver *driver,
 static int
 qemudLogFD(struct qemud_driver *driver, const char* name, bool append)
 {
-    char logfile[PATH_MAX];
+    char *logfile;
     mode_t logmode;
-    int ret, fd = -1;
+    int fd = -1;

-    if ((ret = snprintf(logfile, sizeof(logfile), "%s/%s.log",
-                        driver->logDir, name))
-        < 0 || ret >= sizeof(logfile)) {
+    if (virAsprintf(&logfile, "%s/%s.log", driver->logDir, name) < 0) {
         virReportOOMError();
         return -1;
     }
@@ -766,8 +764,10 @@ qemudLogFD(struct qemud_driver *driver, const char* name, bool append)
         virReportSystemError(errno,
                              _("failed to create logfile %s"),
                              logfile);
+        VIR_FREE(logfile);
         return -1;
     }
+    VIR_FREE(logfile);
     if (virSetCloseExec(fd) < 0) {
         virReportSystemError(errno, "%s",
                              _("Unable to set VM logfile close-on-exec flag"));
@@ -781,30 +781,28 @@ qemudLogFD(struct qemud_driver *driver, const char* name, bool append)
 static int
 qemudLogReadFD(const char* logDir, const char* name, off_t pos)
 {
-    char logfile[PATH_MAX];
+    char *logfile;
     mode_t logmode = O_RDONLY;
-    int ret, fd = -1;
+    int fd = -1;

-    if ((ret = snprintf(logfile, sizeof(logfile), "%s/%s.log", logDir, name))
-        < 0 || ret >= sizeof(logfile)) {
+    if (virAsprintf(&logfile, "%s/%s.log", logDir, name) < 0) {
         qemuReportError(VIR_ERR_INTERNAL_ERROR,
                         _("failed to build logfile name %s/%s.log"),
                         logDir, name);
         return -1;
     }

-
     if ((fd = open(logfile, logmode)) < 0) {
         virReportSystemError(errno,
                              _("failed to create logfile %s"),
                              logfile);
-        return -1;
+        goto cleanup;
     }
     if (virSetCloseExec(fd) < 0) {
         virReportSystemError(errno, "%s",
                              _("Unable to set VM logfile close-on-exec flag"));
         VIR_FORCE_CLOSE(fd);
-        return -1;
+        goto cleanup;
     }
     if (pos < 0 || lseek(fd, pos, SEEK_SET) < 0) {
         virReportSystemError(pos < 0 ? 0 : errno,
@@ -812,6 +810,9 @@ qemudLogReadFD(const char* logDir, const char* name, off_t pos)
                              (long long) pos, logfile);
         VIR_FORCE_CLOSE(fd);
     }
+
+cleanup:
+    VIR_FREE(logfile);
     return fd;
 }

@@ -1721,7 +1722,7 @@ cleanup:
 static int
 qemudStartup(int privileged) {
     char *base = NULL;
-    char driverConf[PATH_MAX];
+    char *driverConf = NULL;
     int rc;
     virConnectPtr conn = NULL;

@@ -1850,14 +1851,9 @@ qemudStartup(int privileged) {
     /* Configuration paths are either ~/.libvirt/qemu/... (session) or
      * /etc/libvirt/qemu/... (system).
      */
-    if (snprintf (driverConf, sizeof(driverConf), "%s/qemu.conf", base) == -1)
-        goto out_of_memory;
-    driverConf[sizeof(driverConf)-1] = '\0';
-
-    if (virAsprintf(&qemu_driver->configDir, "%s/qemu", base) == -1)
-        goto out_of_memory;
-
-    if (virAsprintf(&qemu_driver->autostartDir, "%s/qemu/autostart", base) == -1)
+    if (virAsprintf(&driverConf, "%s/qemu.conf", base) < 0 ||
+        virAsprintf(&qemu_driver->configDir, "%s/qemu", base) < 0 ||
+        virAsprintf(&qemu_driver->autostartDir, "%s/qemu/autostart", base) < 0)
         goto out_of_memory;

     VIR_FREE(base);
@@ -1872,6 +1868,7 @@ qemudStartup(int privileged) {
     if (qemudLoadDriverConfig(qemu_driver, driverConf) < 0) {
         goto error;
     }
+    VIR_FREE(driverConf);

     if (qemudSecurityInit(qemu_driver) < 0)
         goto error;
@@ -1984,6 +1981,7 @@ error:
     if (conn)
         virConnectClose(conn);
     VIR_FREE(base);
+    VIR_FREE(driverConf);
     qemudShutdown();
     return -1;
 }
@@ -4552,18 +4550,20 @@ cleanup:
 }


-static int qemudGetProcessInfo(unsigned long long *cpuTime, int *lastCpu, int pid, int tid) {
-    char proc[PATH_MAX];
+static int
+qemudGetProcessInfo(unsigned long long *cpuTime, int *lastCpu, int pid,
+                    int tid) {
+    char *proc;
     FILE *pidinfo;
     unsigned long long usertime, systime;
     int cpu;
     int ret;

     if (tid)
-        ret = snprintf(proc, sizeof(proc), "/proc/%d/task/%d/stat", pid, tid);
+        ret = virAsprintf(&proc, "/proc/%d/task/%d/stat", pid, tid);
     else
-        ret = snprintf(proc, sizeof(proc), "/proc/%d/stat", pid);
-    if (ret >= (int)sizeof(proc)) {
+        ret = virAsprintf(&proc, "/proc/%d/stat", pid);
+    if (ret < 0) {
         errno = E2BIG;
         return -1;
     }
@@ -4574,8 +4574,10 @@ static int qemudGetProcessInfo(unsigned long long *cpuTime, int *lastCpu, int pi
             *cpuTime = 0;
         if (lastCpu)
             *lastCpu = 0;
+        VIR_FREE(proc);
         return 0;
     }
+    VIR_FREE(proc);

     /* See 'man proc' for information about what all these fields are. We're
      * only interested in a very few of them */
@@ -9033,7 +9035,7 @@ static int qemudDomainDetachPciDiskDevice(struct qemud_driver *driver,
     virDomainDiskDefPtr detach = NULL;
     qemuDomainObjPrivatePtr priv = vm->privateData;
     virCgroupPtr cgroup = NULL;
-    char drivestr[PATH_MAX];
+    char *drivestr = NULL;

     i = qemudFindDisk(vm->def, dev->data.disk->dst);

@@ -9063,10 +9065,8 @@ static int qemudDomainDetachPciDiskDevice(struct qemud_driver *driver,

     /* build the actual drive id string as the disk->info.alias doesn't
      * contain the QEMU_DRIVE_HOST_PREFIX that is passed to qemu */
-    if ((ret = snprintf(drivestr, sizeof(drivestr), "%s%s",
-                   QEMU_DRIVE_HOST_PREFIX,
-                   detach->info.alias))
-        < 0 || ret >= sizeof(drivestr)) {
+    if (virAsprintf(&drivestr, QEMU_DRIVE_HOST_PREFIX "%s",
+                    detach->info.alias) < 0) {
         virReportOOMError();
         goto cleanup;
     }
@@ -9124,6 +9124,7 @@ static int qemudDomainDetachPciDiskDevice(struct qemud_driver *driver,
     ret = 0;

 cleanup:
+    VIR_FREE(drivestr);
     return ret;
 }

@@ -9136,7 +9137,7 @@ static int qemudDomainDetachSCSIDiskDevice(struct qemud_driver *driver,
     virDomainDiskDefPtr detach = NULL;
     qemuDomainObjPrivatePtr priv = vm->privateData;
     virCgroupPtr cgroup = NULL;
-    char drivestr[PATH_MAX];
+    char *drivestr = NULL;

     i = qemudFindDisk(vm->def, dev->data.disk->dst);

@@ -9165,10 +9166,8 @@ static int qemudDomainDetachSCSIDiskDevice(struct qemud_driver *driver,

     /* build the actual drive id string as the disk->info.alias doesn't
      * contain the QEMU_DRIVE_HOST_PREFIX that is passed to qemu */
-    if ((ret = snprintf(drivestr, sizeof(drivestr), "%s%s",
-                   QEMU_DRIVE_HOST_PREFIX,
-                   detach->info.alias))
-        < 0 || ret >= sizeof(drivestr)) {
+    if (virAsprintf(&drivestr, QEMU_DRIVE_HOST_PREFIX "%s",
+                    detach->info.alias) < 0) {
         virReportOOMError();
         goto cleanup;
     }
@@ -9207,6 +9206,7 @@ static int qemudDomainDetachSCSIDiskDevice(struct qemud_driver *driver,

 cleanup:
     virCgroupFree(&cgroup);
+    VIR_FREE(drivestr);
     return ret;
 }

-- 
1.7.3.2


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