BUG/MINOR: namespace: handle a possible strdup() failure
authorIlia Shipitsin <chipitsine@gmail.com>
Tue, 3 Dec 2024 16:10:21 +0000 (17:10 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 20 Mar 2025 10:22:11 +0000 (11:22 +0100)
This defect was found by the coccinelle script "unchecked-strdup.cocci".
It can be backported to all supported branches.

(cherry picked from commit abee5468509a77537015a7e5dc37268d4d564e03)
Signed-off-by: Willy Tarreau <w@1wt.eu>
(cherry picked from commit 1a61a186ea032c676934dbe846655b78bd3be9d2)
Signed-off-by: Willy Tarreau <w@1wt.eu>

src/namespace.c

index 9cc85a3..38464bd 100644 (file)
@@ -89,13 +89,23 @@ struct netns_entry* netns_store_insert(const char *ns_name)
 
        entry = calloc(1, sizeof(*entry));
        if (!entry)
-               goto out;
+               goto err_close_fd;
        entry->fd = fd;
        entry->node.key = strdup(ns_name);
+       if (!entry->node.key)
+               goto err_free_entry;
+
        entry->name_len = strlen(ns_name);
        ebis_insert(&namespace_tree_root, &entry->node);
 out:
        return entry;
+
+/* free all allocated stuff and return entry */
+err_free_entry:
+       ha_free(&entry);
+err_close_fd:
+       close(fd);
+       return entry;
 }
 
 const struct netns_entry* netns_store_lookup(const char *ns_name, size_t ns_name_len)