]> git.ipfire.org Git - thirdparty/git.git/commitdiff
submodule-config: fix leaking name entry when traversing submodules
authorPatrick Steinhardt <ps@pks.im>
Wed, 14 Aug 2024 06:52:12 +0000 (08:52 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 14 Aug 2024 17:07:58 +0000 (10:07 -0700)
We traverse through submodules in the tree via `tree_entry()`, passing
to it a `struct name_entry` that it is supposed to populate with the
tree entry's contents. We unnecessarily allocate this variable instead
of passing a variable that is allocated on the stack, and the ultimately
don't even free that variable. This is unnecessary and leaks memory.

Convert the variable to instead be allocated on the stack to plug the
memory leak.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
submodule-config.c

index 9b0bb0b9f441a0413c0c8ea316a4b03de678c912..c8f2bb2bdd3ef201cc70cf9afd296a14c978c366 100644 (file)
@@ -899,27 +899,25 @@ static void traverse_tree_submodules(struct repository *r,
 {
        struct tree_desc tree;
        struct submodule_tree_entry *st_entry;
-       struct name_entry *name_entry;
+       struct name_entry name_entry;
        char *tree_path = NULL;
 
-       name_entry = xmalloc(sizeof(*name_entry));
-
        fill_tree_descriptor(r, &tree, treeish_name);
-       while (tree_entry(&tree, name_entry)) {
+       while (tree_entry(&tree, &name_entry)) {
                if (prefix)
                        tree_path =
-                               mkpathdup("%s/%s", prefix, name_entry->path);
+                               mkpathdup("%s/%s", prefix, name_entry.path);
                else
-                       tree_path = xstrdup(name_entry->path);
+                       tree_path = xstrdup(name_entry.path);
 
-               if (S_ISGITLINK(name_entry->mode) &&
+               if (S_ISGITLINK(name_entry.mode) &&
                    is_tree_submodule_active(r, root_tree, tree_path)) {
                        ALLOC_GROW(out->entries, out->entry_nr + 1,
                                   out->entry_alloc);
                        st_entry = &out->entries[out->entry_nr++];
 
                        st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry));
-                       *st_entry->name_entry = *name_entry;
+                       *st_entry->name_entry = name_entry;
                        st_entry->submodule =
                                submodule_from_path(r, root_tree, tree_path);
                        st_entry->repo = xmalloc(sizeof(*st_entry->repo));
@@ -927,9 +925,9 @@ static void traverse_tree_submodules(struct repository *r,
                                                root_tree))
                                FREE_AND_NULL(st_entry->repo);
 
-               } else if (S_ISDIR(name_entry->mode))
+               } else if (S_ISDIR(name_entry.mode))
                        traverse_tree_submodules(r, root_tree, tree_path,
-                                                &name_entry->oid, out);
+                                                &name_entry.oid, out);
                free(tree_path);
        }
 }