[PATCH 29/39] virResctrlAllocGetUnused: Use g_autoptr for variables of virResctrlAlloc type

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

 



Refactor the handling of variables so that the cleanup section can be
sanitized.

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 src/util/virresctrl.c | 32 ++++++++++++--------------------
 1 file changed, 12 insertions(+), 20 deletions(-)

diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c
index c66cf4b087..53c202f99f 100644
--- a/src/util/virresctrl.c
+++ b/src/util/virresctrl.c
@@ -1874,8 +1874,8 @@ virResctrlAllocNewFromInfo(virResctrlInfoPtr info)
 virResctrlAllocPtr
 virResctrlAllocGetUnused(virResctrlInfoPtr resctrl)
 {
-    virResctrlAllocPtr ret = NULL;
-    virResctrlAllocPtr alloc = NULL;
+    g_autoptr(virResctrlAlloc) ret = NULL;
+    g_autoptr(virResctrlAlloc) alloc_default = NULL;
     struct dirent *ent = NULL;
     g_autoptr(DIR) dirp = NULL;
     int rv = -1;
@@ -1890,17 +1890,18 @@ virResctrlAllocGetUnused(virResctrlInfoPtr resctrl)
     if (!ret)
         return NULL;

-    alloc = virResctrlAllocGetDefault(resctrl);
-    if (!alloc)
-        goto error;
+    alloc_default = virResctrlAllocGetDefault(resctrl);
+    if (!alloc_default)
+        return NULL;

-    virResctrlAllocSubtract(ret, alloc);
-    virObjectUnref(alloc);
+    virResctrlAllocSubtract(ret, alloc_default);

     if (virDirOpen(&dirp, SYSFS_RESCTRL_PATH) < 0)
-        goto error;
+        return NULL;

     while ((rv = virDirRead(dirp, &ent, SYSFS_RESCTRL_PATH)) > 0) {
+        g_autoptr(virResctrlAlloc) alloc = NULL;
+
         if (STREQ(ent->d_name, "info"))
             continue;

@@ -1912,24 +1913,15 @@ virResctrlAllocGetUnused(virResctrlInfoPtr resctrl)
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("Could not read schemata file for group %s"),
                            ent->d_name);
-            goto error;
+            return NULL;
         }

         virResctrlAllocSubtract(ret, alloc);
-        virObjectUnref(alloc);
-        alloc = NULL;
     }
     if (rv < 0)
-        goto error;
-
- cleanup:
-    virObjectUnref(alloc);
-    return ret;
+        return NULL;

- error:
-    virObjectUnref(ret);
-    ret = NULL;
-    goto cleanup;
+    return g_steal_pointer(&ret);
 }


-- 
2.29.2




[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]

  Powered by Linux