From ab54d5f15239940d4e3d142c553d7e55ec0ff13b Mon Sep 17 00:00:00 2001 From: John Ferlan Date: Mon, 24 Apr 2017 12:50:12 -0400 Subject: [PATCH] secret: Clean up virSecretObjListExport logic 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 --- src/conf/virsecretobj.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/conf/virsecretobj.c b/src/conf/virsecretobj.c index 55624f4c1a..5626387969 100644 --- a/src/conf/virsecretobj.c +++ b/src/conf/virsecretobj.c @@ -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; }