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:24:01 +0000 (11:24 -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>
src/lttng-tracker-id.c

index 205c4af4d9dffb5e2af65f0a0a5c9f78d967f106..a1d84b54fc1f69537fced8ce248007f7cc63f8d6 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.026509 seconds and 4 git commands to generate.