From 0b6888451feb216c74393982968460328d7db6b7 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Wed, 4 Aug 2021 11:28:35 +0200 Subject: [PATCH] virNWFilterIncludeDefToRuleInst: Refactor cleanup Use automatic memory freeing for 'tmpvars' and move the allocation of tmpvars earlier so that we are guaranteed that 'obj' will always be appended to 'inst->filters' and thus don't need cleanup for it. By moving the reset of 'inst' to the block when virNWFilterDefToInst fails we can get rid of the rest of the cleanup section and remove the 'ret' variable. Signed-off-by: Peter Krempa Reviewed-by: Michal Privoznik --- src/nwfilter/nwfilter_gentech_driver.c | 32 +++++++++++--------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/nwfilter/nwfilter_gentech_driver.c b/src/nwfilter/nwfilter_gentech_driver.c index c9ffa30839..ecba16d55c 100644 --- a/src/nwfilter/nwfilter_gentech_driver.c +++ b/src/nwfilter/nwfilter_gentech_driver.c @@ -281,20 +281,20 @@ virNWFilterIncludeDefToRuleInst(virNWFilterDriverState *driver, virNWFilterInst *inst) { virNWFilterObj *obj; - GHashTable *tmpvars = NULL; + g_autoptr(GHashTable) tmpvars = NULL; virNWFilterDef *childdef; virNWFilterDef *newChilddef; - int ret = -1; VIR_DEBUG("Instantiating filter %s", inc->filterref); - if (!(obj = virNWFilterObjListFindInstantiateFilter(driver->nwfilters, - inc->filterref))) - goto cleanup; /* create a temporary hashmap for depth-first tree traversal */ - if (!(tmpvars = virNWFilterCreateVarsFrom(inc->params, - vars))) - goto cleanup; + if (!(tmpvars = virNWFilterCreateVarsFrom(inc->params, vars))) + return -1; + + /* 'obj' is always appended to 'inst->filters' thus we don't unlock it */ + if (!(obj = virNWFilterObjListFindInstantiateFilter(driver->nwfilters, + inc->filterref))) + return -1; childdef = virNWFilterObjGetDef(obj); @@ -311,24 +311,18 @@ virNWFilterIncludeDefToRuleInst(virNWFilterDriverState *driver, } VIR_APPEND_ELEMENT(inst->filters, inst->nfilters, obj); - obj = NULL; if (virNWFilterDefToInst(driver, childdef, tmpvars, useNewFilter, foundNewFilter, - inst) < 0) - goto cleanup; - - ret = 0; - cleanup: - if (ret < 0) + inst) < 0) { virNWFilterInstReset(inst); - virHashFree(tmpvars); - if (obj) - virNWFilterObjUnlock(obj); - return ret; + return -1; + } + + return 0; }