]> xenbits.xensource.com Git - libvirt.git/commitdiff
virpcimock: Use VIR_AUTOFREE()
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 13 Aug 2019 11:50:48 +0000 (13:50 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Sat, 17 Aug 2019 09:18:08 +0000 (11:18 +0200)
It saves us couple of lines.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Tested-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
tests/virpcimock.c

index 3577d1bd41250e23ea5fc835b69ceb621f58017b..eb2831492eec3574fcd521a303044e7aaef63d4e 100644 (file)
@@ -173,7 +173,7 @@ make_file(const char *path,
           ssize_t len)
 {
     int fd = -1;
-    char *filepath = NULL;
+    VIR_AUTOFREE(char *) filepath = NULL;
     if (value && len == -1)
         len = strlen(value);
 
@@ -187,7 +187,6 @@ make_file(const char *path,
         ABORT("Unable to write: %s", filepath);
 
     VIR_FORCE_CLOSE(fd);
-    VIR_FREE(filepath);
 }
 
 static void
@@ -195,15 +194,13 @@ make_symlink(const char *path,
           const char *name,
           const char *target)
 {
-    char *filepath = NULL;
+    VIR_AUTOFREE(char *) filepath = NULL;
 
     if (virAsprintfQuiet(&filepath, "%s/%s", path, name) < 0)
         ABORT_OOM();
 
     if (symlink(target, filepath) < 0)
         ABORT("Unable to create symlink filepath -> target");
-
-    VIR_FREE(filepath);
 }
 
 static int
@@ -214,7 +211,7 @@ pci_read_file(const char *path,
 {
     int ret = -1;
     int fd = -1;
-    char *newpath;
+    VIR_AUTOFREE(char *) newpath = NULL;
 
     if (virAsprintfQuiet(&newpath, "%s/%s",
                          fakesysfspcidir,
@@ -238,7 +235,6 @@ pci_read_file(const char *path,
 
     ret = 0;
  cleanup:
-    VIR_FREE(newpath);
     real_close(fd);
     return ret;
 }
@@ -336,10 +332,10 @@ static void
 pci_device_new_from_stub(const struct pciDevice *data)
 {
     struct pciDevice *dev;
-    char *devpath;
-    char *id;
+    VIR_AUTOFREE(char *) devpath = NULL;
+    VIR_AUTOFREE(char *) id = NULL;
     char *c;
-    char *configSrc;
+    VIR_AUTOFREE(char *) configSrc = NULL;
     char tmp[256];
     struct stat sb;
     bool configSrcExists = false;
@@ -364,7 +360,6 @@ pci_device_new_from_stub(const struct pciDevice *data)
         virAsprintfQuiet(&devpath, "%s/devices/%s", fakesysfspcidir, data->id) < 0)
         ABORT_OOM();
 
-    VIR_FREE(id);
     memcpy(dev, data, sizeof(*dev));
 
     if (virFileMakePath(devpath) < 0)
@@ -381,14 +376,13 @@ pci_device_new_from_stub(const struct pciDevice *data)
          * file, and parallel VPATH builds must not stomp on the
          * original; besides, 'make distcheck' requires the original
          * to be read-only */
-        char *buf;
+        VIR_AUTOFREE(char *) buf = NULL;
         ssize_t len;
 
         if ((len = virFileReadAll(configSrc, 4096, &buf)) < 0)
             ABORT("Unable to read config file '%s'", configSrc);
 
         make_file(devpath, "config", buf, len);
-        VIR_FREE(buf);
     } else {
         /* If there's no config data in the virpcitestdata dir, create a dummy
          * config file */
@@ -428,9 +422,6 @@ pci_device_new_from_stub(const struct pciDevice *data)
 
     if (VIR_APPEND_ELEMENT_QUIET(pciDevices, nPCIDevices, dev) < 0)
         ABORT_OOM();
-
-    VIR_FREE(devpath);
-    VIR_FREE(configSrc);
 }
 
 static struct pciDevice *
@@ -484,7 +475,7 @@ pci_driver_new(const char *name, int fail, ...)
     struct pciDriver *driver;
     va_list args;
     int vendor, device;
-    char *driverpath;
+    VIR_AUTOFREE(char *) driverpath = NULL;
 
     if (VIR_ALLOC_QUIET(driver) < 0 ||
         VIR_STRDUP_QUIET(driver->name, name) < 0 ||
@@ -520,8 +511,6 @@ pci_driver_new(const char *name, int fail, ...)
 
     if (VIR_APPEND_ELEMENT_QUIET(pciDrivers, nPCIDrivers, driver) < 0)
         ABORT_OOM();
-
-    VIR_FREE(driverpath);
 }
 
 static struct pciDriver *
@@ -587,13 +576,13 @@ static int
 pci_driver_bind(struct pciDriver *driver,
                 struct pciDevice *dev)
 {
-    int ret = -1;
-    char *devpath = NULL, *driverpath = NULL;
+    VIR_AUTOFREE(char *) devpath = NULL;
+    VIR_AUTOFREE(char *) driverpath = NULL;
 
     if (dev->driver) {
         /* Device already bound */
         errno = ENODEV;
-        return ret;
+        return -1;
     }
 
     /* Make symlink under device tree */
@@ -602,11 +591,11 @@ pci_driver_bind(struct pciDriver *driver,
         virAsprintfQuiet(&driverpath, "%s/drivers/%s",
                          fakesysfspcidir, driver->name) < 0) {
         errno = ENOMEM;
-        goto cleanup;
+        return -1;
     }
 
     if (symlink(driverpath, devpath) < 0)
-        goto cleanup;
+        return -1;
 
     /* Make symlink under driver tree */
     VIR_FREE(devpath);
@@ -616,31 +605,27 @@ pci_driver_bind(struct pciDriver *driver,
         virAsprintfQuiet(&driverpath, "%s/drivers/%s/%s",
                          fakesysfspcidir, driver->name, dev->id) < 0) {
         errno = ENOMEM;
-        goto cleanup;
+        return -1;
     }
 
     if (symlink(devpath, driverpath) < 0)
-        goto cleanup;
+        return -1;
 
     dev->driver = driver;
-    ret = 0;
- cleanup:
-    VIR_FREE(devpath);
-    VIR_FREE(driverpath);
-    return ret;
+    return 0;
 }
 
 static int
 pci_driver_unbind(struct pciDriver *driver,
                   struct pciDevice *dev)
 {
-    int ret = -1;
-    char *devpath = NULL, *driverpath = NULL;
+    VIR_AUTOFREE(char *) devpath = NULL;
+    VIR_AUTOFREE(char *) driverpath = NULL;
 
     if (dev->driver != driver) {
         /* Device not bound to the @driver */
         errno = ENODEV;
-        return ret;
+        return -1;
     }
 
     /* Make symlink under device tree */
@@ -649,19 +634,15 @@ pci_driver_unbind(struct pciDriver *driver,
         virAsprintfQuiet(&driverpath, "%s/drivers/%s/%s",
                          fakesysfspcidir, driver->name, dev->id) < 0) {
         errno = ENOMEM;
-        goto cleanup;
+        return -1;
     }
 
     if (unlink(devpath) < 0 ||
         unlink(driverpath) < 0)
-        goto cleanup;
+        return -1;
 
     dev->driver = NULL;
-    ret = 0;
- cleanup:
-    VIR_FREE(devpath);
-    VIR_FREE(driverpath);
-    return ret;
+    return 0;
 }
 
 static int
@@ -911,20 +892,15 @@ init_env(void)
 int
 access(const char *path, int mode)
 {
-    int ret;
+    VIR_AUTOFREE(char *) newpath = NULL;
 
     init_syms();
 
-    if (STRPREFIX(path, SYSFS_PCI_PREFIX)) {
-        char *newpath;
-        if (getrealpath(&newpath, path) < 0)
-            return -1;
-        ret = real_access(newpath, mode);
-        VIR_FREE(newpath);
-    } else {
-        ret = real_access(path, mode);
-    }
-    return ret;
+    if (STRPREFIX(path, SYSFS_PCI_PREFIX) &&
+        getrealpath(&newpath, path) < 0)
+        return -1;
+
+    return real_access(newpath ? newpath : path, mode);
 }
 
 
@@ -943,7 +919,7 @@ int
 open(const char *path, int flags, ...)
 {
     int ret;
-    char *newpath = NULL;
+    VIR_AUTOFREE(char *) newpath = NULL;
 
     init_syms();
 
@@ -970,7 +946,6 @@ open(const char *path, int flags, ...)
         ret = -1;
     }
 
-    VIR_FREE(newpath);
     return ret;
 }
 
@@ -1009,8 +984,7 @@ __open_2(const char *path, int flags)
 DIR *
 opendir(const char *path)
 {
-    DIR *ret;
-    char *newpath = NULL;
+    VIR_AUTOFREE(char *) newpath = NULL;
 
     init_syms();
 
@@ -1018,10 +992,7 @@ opendir(const char *path)
         getrealpath(&newpath, path) < 0)
         return NULL;
 
-    ret = real_opendir(newpath ? newpath : path);
-
-    VIR_FREE(newpath);
-    return ret;
+    return real_opendir(newpath ? newpath : path);
 }
 
 int
@@ -1035,23 +1006,15 @@ close(int fd)
 char *
 virFileCanonicalizePath(const char *path)
 {
-    char *ret;
+    VIR_AUTOFREE(char *) newpath = NULL;
 
     init_syms();
 
-    if (STRPREFIX(path, SYSFS_PCI_PREFIX)) {
-        char *newpath;
-
-        if (getrealpath(&newpath, path) < 0)
-            return NULL;
-
-        ret = real_virFileCanonicalizePath(newpath);
-        VIR_FREE(newpath);
-    } else {
-        ret = real_virFileCanonicalizePath(path);
-    }
+    if (STRPREFIX(path, SYSFS_PCI_PREFIX) &&
+        getrealpath(&newpath, path) < 0)
+        return NULL;
 
-    return ret;
+    return real_virFileCanonicalizePath(newpath ? newpath : path);
 }
 
 # include "virmockstathelpers.c"