]> xenbits.xensource.com Git - libvirt.git/commitdiff
nwfilterStateInitialize: Simplify and fix error handling
authorTim Wiederhake <twiederh@redhat.com>
Fri, 8 Apr 2022 11:54:09 +0000 (13:54 +0200)
committerTim Wiederhake <twiederh@redhat.com>
Fri, 8 Apr 2022 14:43:16 +0000 (16:43 +0200)
Under certain circumstances nwfilterStateInitialize could leak memory:
If e.g. the call to virNWFilterConfLayerInit fails, the error path
err_techdrivers_shutdown does not free the previously allocated memory
held in driver->stateDir.

Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/nwfilter/nwfilter_driver.c

index bf17c5ea669ff42421a36498c76a60382ea2929c..b66ba227375d8a8852bd490a2fbaef8ef33fbaca 100644 (file)
@@ -210,18 +210,17 @@ nwfilterStateInitialize(bool privileged,
     if (root != NULL) {
         virReportError(VIR_ERR_INVALID_ARG, "%s",
                        _("Driver does not support embedded mode"));
-        return -1;
+        return VIR_DRV_STATE_INIT_ERROR;
     }
 
-    if (virGDBusHasSystemBus() &&
-        !(sysbus = virGDBusGetSystemBus()))
+    if (virGDBusHasSystemBus() && !(sysbus = virGDBusGetSystemBus()))
         return VIR_DRV_STATE_INIT_ERROR;
 
     driver = g_new0(virNWFilterDriverState, 1);
 
     driver->lockFD = -1;
     if (virMutexInitRecursive(&driver->updateLock) < 0)
-        goto err_free_driverstate;
+        goto error;
 
     driver->updateLockInitialized = true;
     driver->privileged = privileged;
@@ -248,18 +247,19 @@ nwfilterStateInitialize(bool privileged,
         goto error;
 
     if (virNWFilterIPAddrMapInit() < 0)
-        goto err_free_driverstate;
+        goto error;
+
     if (virNWFilterLearnInit() < 0)
-        goto err_exit_ipaddrmapshutdown;
+        goto error;
+
     if (virNWFilterDHCPSnoopInit() < 0)
-        goto err_exit_learnshutdown;
+        goto error;
 
     if (virNWFilterTechDriversInit(privileged) < 0)
-        goto err_dhcpsnoop_shutdown;
+        goto error;
 
-    if (virNWFilterConfLayerInit(virNWFilterTriggerRebuildImpl,
-                                 driver) < 0)
-        goto err_techdrivers_shutdown;
+    if (virNWFilterConfLayerInit(virNWFilterTriggerRebuildImpl, driver) < 0)
+        goto error;
 
     /*
      * startup the DBus late so we don't get a reload signal while
@@ -297,22 +297,6 @@ nwfilterStateInitialize(bool privileged,
 
  error:
     nwfilterStateCleanupLocked();
-
-    return VIR_DRV_STATE_INIT_ERROR;
-
- err_techdrivers_shutdown:
-    virNWFilterTechDriversShutdown();
- err_dhcpsnoop_shutdown:
-    virNWFilterDHCPSnoopShutdown();
- err_exit_learnshutdown:
-    virNWFilterLearnShutdown();
- err_exit_ipaddrmapshutdown:
-    virNWFilterIPAddrMapShutdown();
-
- err_free_driverstate:
-    virNWFilterObjListFree(driver->nwfilters);
-    g_clear_pointer(&driver, g_free);
-
     return VIR_DRV_STATE_INIT_ERROR;
 }