]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virNWFilterIncludeDefToRuleInst: Refactor cleanup
authorPeter Krempa <pkrempa@redhat.com>
Wed, 4 Aug 2021 09:28:35 +0000 (11:28 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 6 Aug 2021 06:53:26 +0000 (08:53 +0200)
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 <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/nwfilter/nwfilter_gentech_driver.c

index c9ffa3083903451c62c7601ceef1c24ce027aae2..ecba16d55c130187dc1ee65c6037ff4a87790a30 100644 (file)
@@ -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;
 }