submodule-config: fix leaking name entry when traversing submodules

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>

Patrick Steinhardt committed Aug 14, 2024 at 08:52 UTC 5f6519b62c636edfff4e1ffd6591917a53616b42
1 file changed +8 -10
submodule-config.c
+8 -10
@@ -899,27 +899,25 @@ static void traverse_tree_submodules(struct repository *r,
899 {
900 struct tree_desc tree;
901 struct submodule_tree_entry *st_entry;
902 - struct name_entry *name_entry;
902 + struct name_entry name_entry;
903 char *tree_path = NULL;
904
905 - name_entry = xmalloc(sizeof(*name_entry));
906 -
905 fill_tree_descriptor(r, &tree, treeish_name);
908 - while (tree_entry(&tree, name_entry)) {
906 + while (tree_entry(&tree, &name_entry)) {
907 if (prefix)
908 tree_path =
911 - mkpathdup("%s/%s", prefix, name_entry->path);
909 + mkpathdup("%s/%s", prefix, name_entry.path);
910 else
913 - tree_path = xstrdup(name_entry->path);
911 + tree_path = xstrdup(name_entry.path);
912
915 - if (S_ISGITLINK(name_entry->mode) &&
913 + if (S_ISGITLINK(name_entry.mode) &&
914 is_tree_submodule_active(r, root_tree, tree_path)) {
915 ALLOC_GROW(out->entries, out->entry_nr + 1,
916 out->entry_alloc);
917 st_entry = &out->entries[out->entry_nr++];
918
919 st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry));
922 - *st_entry->name_entry = *name_entry;
920 + *st_entry->name_entry = name_entry;
921 st_entry->submodule =
922 submodule_from_path(r, root_tree, tree_path);
923 st_entry->repo = xmalloc(sizeof(*st_entry->repo));
@@ -927,9 +925,9 @@ static void traverse_tree_submodules(struct repository *r,
925 root_tree))
926 FREE_AND_NULL(st_entry->repo);
927
930 - } else if (S_ISDIR(name_entry->mode))
928 + } else if (S_ISDIR(name_entry.mode))
929 traverse_tree_submodules(r, root_tree, tree_path,
932 - &name_entry->oid, out);
930 + &name_entry.oid, out);
931 free(tree_path);
932 }
933 }