]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
secret: Clean up virSecretObjListExport logic
authorJohn Ferlan <jferlan@redhat.com>
Mon, 24 Apr 2017 16:50:12 +0000 (12:50 -0400)
committerJohn Ferlan <jferlan@redhat.com>
Tue, 25 Apr 2017 19:33:22 +0000 (15:33 -0400)
Shorten the time needed to keep the list lock and alter the cleanup
path to be more of an error path.

Utilize the the virObjectListFree function to handle the calls for
virObjectUnref on each list element and the VIR_FREE of the list
instead of open coding it.

Change the name of the virHashForEach callback to match the name
of the Export function with the Callback added onto it.

Signed-off-by: John Ferlan <jferlan@redhat.com>
src/conf/virsecretobj.c

index 55624f4c1a02ef88e6026a521b7f78c78a3bf9bd..5626387969a6704ea6196c7436fcf81895a1a4c9 100644 (file)
@@ -527,9 +527,9 @@ struct virSecretObjListData {
 };
 
 static int
-virSecretObjListPopulate(void *payload,
-                         const void *name ATTRIBUTE_UNUSED,
-                         void *opaque)
+virSecretObjListExportCallback(void *payload,
+                               const void *name ATTRIBUTE_UNUSED,
+                               void *opaque)
 {
     struct virSecretObjListData *data = opaque;
     virSecretObjPtr obj = payload;
@@ -573,7 +573,6 @@ virSecretObjListExport(virConnectPtr conn,
                        virSecretObjListACLFilter filter,
                        unsigned int flags)
 {
-    int ret = -1;
     struct virSecretObjListData data = {
         .conn = conn, .secrets = NULL,
         .filter = filter, .flags = flags,
@@ -581,30 +580,28 @@ virSecretObjListExport(virConnectPtr conn,
 
     virObjectLock(secretobjs);
     if (secrets &&
-        VIR_ALLOC_N(data.secrets, virHashSize(secretobjs->objs) + 1) < 0)
-        goto cleanup;
+        VIR_ALLOC_N(data.secrets, virHashSize(secretobjs->objs) + 1) < 0) {
+        virObjectUnlock(secretobjs);
+        return -1;
+    }
 
-    virHashForEach(secretobjs->objs, virSecretObjListPopulate, &data);
+    virHashForEach(secretobjs->objs, virSecretObjListExportCallback, &data);
+    virObjectUnlock(secretobjs);
 
     if (data.error)
-        goto cleanup;
+        goto error;
 
     if (data.secrets) {
         /* trim the array to the final size */
         ignore_value(VIR_REALLOC_N(data.secrets, data.nsecrets + 1));
         *secrets = data.secrets;
-        data.secrets = NULL;
     }
 
-    ret = data.nsecrets;
+    return data.nsecrets;
 
- cleanup:
-    virObjectUnlock(secretobjs);
-    while (data.secrets && data.nsecrets)
-        virObjectUnref(data.secrets[--data.nsecrets]);
-
-    VIR_FREE(data.secrets);
-    return ret;
+ error:
+    virObjectListFree(data.secrets);
+    return -1;
 }