Fix: ressource leak in id tracker
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 19 Nov 2020 16:03:17 +0000 (11:03 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 19 Nov 2020 16:26:22 +0000 (11:26 -0500)
Memory leak found by Coverity:

CID 1412251 (#2 of 2): Resource leak (RESOURCE_LEAK)
21. leaked_storage: Variable head going out of scope leaks the storage it points to.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
lttng-tracker-id.c

index 9a4b98b2ef7ef17ffc834b77cc9305c5205346e6..327ae3462dadcc7119fca2a22094e2a6831b022e 100644 (file)
@@ -75,6 +75,7 @@ int lttng_id_tracker_add(struct lttng_id_tracker *lf, int id)
        struct lttng_id_tracker_rcu *p = lf->p;
        uint32_t hash = hash_32(id, 32);
        bool allocated = false;
+       int ret;
 
        if (!p) {
                p = lttng_id_tracker_rcu_create();
@@ -84,18 +85,28 @@ int lttng_id_tracker_add(struct lttng_id_tracker *lf, int id)
        }
        head = &p->id_hash[hash & (LTTNG_ID_TABLE_SIZE - 1)];
        lttng_hlist_for_each_entry(e, head, hlist) {
-               if (id == e->id)
-                       return -EEXIST;
+               if (id == e->id) {
+                       ret = -EEXIST;
+                       goto error;
+               }
        }
        e = kmalloc(sizeof(struct lttng_id_hash_node), GFP_KERNEL);
-       if (!e)
-               return -ENOMEM;
+       if (!e) {
+               ret = -ENOMEM;
+               goto error;
+       }
        e->id = id;
        hlist_add_head_rcu(&e->hlist, head);
        if (allocated) {
                rcu_assign_pointer(lf->p, p);
        }
        return 0;
+
+error:
+       if (allocated) {
+               kfree(p);
+       }
+       return ret;
 }
 
 static
This page took 0.02703 seconds and 4 git commands to generate.