[Ocfs2-tools-devel] [PATCH] Fix memory leak problems in o2cb_ctl.c

piaojun piaojun at huawei.com
Mon Mar 9 05:08:41 PDT 2015


Six memory leak problems were found in o2cb_ctl.c. The details of the
problems are described below:

1. In find_objects_for_type(), the memory allocated in
   o2cb_cluster_get_name(cluster) and o2cb_node_get_name(node) should be
   freed after use.

2. In run_info_clusters(), the memory allocated in
   o2cb_cluster_get_name(cluster) should be freed after use, so 'name' is a
   substitution for o2cb_cluster_get_name(cluster) to avoid memory leak.

3. In run_info_nodes(), the memory allocated in
   o2cb_node_get_name(node) and o2cb_cluster_get_name(cluster) should be
   freed after use.

4. In online_cluster(), the memory allocated for 'name' should be freed
   before the function return.

5. In run_create_nodes(), the memory allocated for 'number' should be freed
   before the function return.

6. In main(), the memory allocated for 'ctxt.oc_objects' should be freed
   before the function return.

Signed-off-by: Jun Piao <piaojun at huawei.com>
Reviewed-by: Alex Chen <alex.chen at huawei.com>

---
 o2cb_ctl/o2cb_ctl.c | 44 +++++++++++++++++++++++++++++++++-----------
 1 file changed, 33 insertions(+), 11 deletions(-)

diff --git a/o2cb_ctl/o2cb_ctl.c b/o2cb_ctl/o2cb_ctl.c
index 51c1bb2..b20bca7 100644
--- a/o2cb_ctl/o2cb_ctl.c
+++ b/o2cb_ctl/o2cb_ctl.c
@@ -186,6 +186,22 @@ static void clear_attrs(O2CBContext *ctxt)
     g_list_free(ctxt->oc_attrs);
 }  /* clear_attrs() */

+static void clear_objects(O2CBContext *ctxt)
+{
+    GList *list;
+    list = ctxt->oc_objects;
+    if (!list)
+        return;
+
+    while (list)
+    {
+        g_free(list->data);
+        list->data = NULL;
+        list = list->next;
+    }
+
+    g_list_free(ctxt->oc_objects);
+}

 static gboolean attr_set(O2CBContext *ctxt, const gchar *attr_name)
 {
@@ -515,7 +531,7 @@ static gint find_objects_for_type(O2CBContext *ctxt)
     JIterator *c_iter, *n_iter;
     O2CBCluster *cluster;
     O2CBNode *node;
-
+    gchar *object = NULL;
     c_iter = o2cb_config_get_clusters(ctxt->oc_config);

     if (!c_iter)
@@ -530,7 +546,9 @@ static gint find_objects_for_type(O2CBContext *ctxt)

         if (ctxt->oc_type == O2CB_TYPE_CLUSTER)
         {
-            add_object(ctxt, o2cb_cluster_get_name(cluster));
+            object = o2cb_cluster_get_name(cluster);
+            add_object(ctxt, object);
+            g_free(object);
         }
         else if (ctxt->oc_type == O2CB_TYPE_NODE)
         {
@@ -544,7 +562,9 @@ static gint find_objects_for_type(O2CBContext *ctxt)
             while (j_iterator_has_more(n_iter))
             {
                 node = j_iterator_get_next(n_iter);
-                add_object(ctxt, o2cb_node_get_name(node));
+                object = o2cb_node_get_name(node);
+                add_object(ctxt, object);
+                g_free(object);
             }
             j_iterator_free(n_iter);
         }
@@ -643,7 +663,7 @@ static gint run_info_clusters(O2CBContext *ctxt)
             return -ENOENT;
         }

-        fprintf(stdout, format, o2cb_cluster_get_name(cluster),
+        fprintf(stdout, format, name,
                 o2cb_cluster_get_node_count(cluster),
                 "configured");

@@ -661,7 +681,7 @@ static gint run_info_nodes(O2CBContext *ctxt)
     O2CBNode *node;
     gchar *name;
     gchar *format;
-
+    gchar *cluster_name = NULL;

     if (ctxt->oc_compact_info)
     {
@@ -706,15 +726,15 @@ static gint run_info_nodes(O2CBContext *ctxt)
             fprintf(stderr, "Node \"%s\" does not exist\n", name);
             return -ENOENT;
         }
-
+        cluster_name = o2cb_cluster_get_name(cluster);
         fprintf(stdout, format,
-                o2cb_node_get_name(node),
-                o2cb_cluster_get_name(cluster),
+                name,
+                cluster_name,
                 o2cb_node_get_number(node),
                 o2cb_node_get_ip_string(node),
                 o2cb_node_get_port(node),
                 "configured");
-
+        g_free(cluster_name);
         list = list->next;
     }

@@ -911,7 +931,8 @@ static gint online_cluster(O2CBContext *ctxt, O2CBCluster *cluster)
     }

 out_error:
-
+    if (name)
+        g_free(name);
     return rc;
 }  /* online_cluster() */

@@ -1286,6 +1307,7 @@ static gint run_create_nodes(O2CBContext *ctxt)
             com_err(PROGNAME, err, "while determining if node %s is local",
                     name);
             rc = -EINVAL;
+            g_free(number);
             goto out;
         }

@@ -1418,7 +1440,7 @@ gint main(gint argc, gchar *argv[])
     if (ctxt.oc_config)
         o2cb_config_free(ctxt.oc_config);
     clear_attrs(&ctxt);
-
+    clear_objects(&ctxt);
 out_error:
     return rc;
 }  /* main() */
-- 1.8.4.3




More information about the Ocfs2-tools-devel mailing list