mesh: Fix clean up of node's DBUS assosiated resources
authorInga Stotland <inga.stotland@intel.com>
Wed, 3 Jul 2019 23:02:44 +0000 (16:02 -0700)
committerAnupam Roy <anupam.r@samsung.com>
Tue, 17 Dec 2019 15:20:51 +0000 (20:50 +0530)
This consolidates multiple places where node's object paths,
interfaces, etc. are de-allocated, into one routine:
free_node_dbus_resources().
This also addresses memory leaks assosiated with inconsistent freeing
of object path strings.

Change-Id: If11ec87748384b43dd7f6b7ef06d88439cf0323c
Signed-off-by: Anupam Roy <anupam.r@samsung.com>
mesh/node.c

index 3c0e64c..580d310 100644 (file)
@@ -228,34 +228,46 @@ static void element_free(void *data)
        l_free(element);
 }
 
-static void free_node_resources(void *data)
+static void free_node_dbus_resources(struct mesh_node *node)
 {
-       struct mesh_node *node = data;
+       if (!node)
+               return;
 
-       /* Unregister io callbacks */
-       if (node->net)
-               mesh_net_detach(node->net);
-       mesh_net_free(node->net);
+       if (node->disc_watch) {
+               l_dbus_remove_watch(dbus_get_bus(), node->disc_watch);
+               node->disc_watch = 0;
+       }
 
-       l_queue_destroy(node->elements, element_free);
-       l_free(node->comp);
-       l_free(node->app_path);
+       l_queue_foreach(node->elements, free_element_path, NULL);
        l_free(node->owner);
-       l_free(node->node_path);
-
-       if (node->disc_watch)
-               l_dbus_remove_watch(dbus_get_bus(), node->disc_watch);
+       node->owner = NULL;
+       l_free(node->app_path);
+       node->app_path = NULL;
 
        if (node->path) {
                l_dbus_object_remove_interface(dbus_get_bus(), node->path,
                                                        MESH_NODE_INTERFACE);
 
                l_dbus_object_remove_interface(dbus_get_bus(), node->path,
-                                              MESH_MANAGEMENT_INTERFACE);
+                                               MESH_MANAGEMENT_INTERFACE);
+               l_free(node->path);
+               node->path = NULL;
        }
+}
 
-       l_free(node->path);
+static void free_node_resources(void *data)
+{
+       struct mesh_node *node = data;
 
+       /* Unregister io callbacks */
+       if (node->net)
+               mesh_net_detach(node->net);
+       mesh_net_free(node->net);
+
+       l_queue_destroy(node->elements, element_free);
+       l_free(node->comp);
+
+       free_node_dbus_resources(node);
        l_free(node);
 }
 
@@ -1035,24 +1047,9 @@ static void app_disc_cb(struct l_dbus *bus, void *user_data)
        l_info("App %s disconnected (%u)", node->owner, node->disc_watch);
 
        node->disc_watch = 0;
-
-       l_queue_foreach(node->elements, free_element_path, NULL);
-
-       l_free(node->owner);
-       node->owner = NULL;
-
-       if (node->path) {
-               l_dbus_object_remove_interface(dbus_get_bus(), node->path,
-                                                       MESH_NODE_INTERFACE);
-
-               l_dbus_object_remove_interface(dbus_get_bus(), node->path,
-                                               MESH_MANAGEMENT_INTERFACE);
-               l_free(node->app_path);
-               node->app_path = NULL;
-       }
+       free_node_dbus_resources(node);
 }
 
-
 static bool validate_model_property(struct node_element *ele,
                                        struct l_dbus_message_iter *property,
                                        uint8_t *num_models, bool vendor)
@@ -1613,14 +1610,9 @@ fail:
                /* Handle failed Attach request */
                node_ready_func_t cb = req->cb;
 
-               l_queue_foreach(node->elements, free_element_path, NULL);
-               l_free(node->app_path);
-               node->app_path = NULL;
+               free_node_dbus_resources(node);
 
-               l_free(node->owner);
-               node->owner = NULL;
                cb(req->user_data, MESH_ERROR_FAILED, node);
-
        } else {
                /* Handle failed Join and Create requests */
                if (node)