Skip to content

Commit 5f6519b

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d1c53f6 commit 5f6519b

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

submodule-config.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -899,37 +899,35 @@ static void traverse_tree_submodules(struct repository *r,
899899
{
900900
struct tree_desc tree;
901901
struct submodule_tree_entry *st_entry;
902-
struct name_entry *name_entry;
902+
struct name_entry name_entry;
903903
char *tree_path = NULL;
904904

905-
name_entry = xmalloc(sizeof(*name_entry));
906-
907905
fill_tree_descriptor(r, &tree, treeish_name);
908-
while (tree_entry(&tree, name_entry)) {
906+
while (tree_entry(&tree, &name_entry)) {
909907
if (prefix)
910908
tree_path =
911-
mkpathdup("%s/%s", prefix, name_entry->path);
909+
mkpathdup("%s/%s", prefix, name_entry.path);
912910
else
913-
tree_path = xstrdup(name_entry->path);
911+
tree_path = xstrdup(name_entry.path);
914912

915-
if (S_ISGITLINK(name_entry->mode) &&
913+
if (S_ISGITLINK(name_entry.mode) &&
916914
is_tree_submodule_active(r, root_tree, tree_path)) {
917915
ALLOC_GROW(out->entries, out->entry_nr + 1,
918916
out->entry_alloc);
919917
st_entry = &out->entries[out->entry_nr++];
920918

921919
st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry));
922-
*st_entry->name_entry = *name_entry;
920+
*st_entry->name_entry = name_entry;
923921
st_entry->submodule =
924922
submodule_from_path(r, root_tree, tree_path);
925923
st_entry->repo = xmalloc(sizeof(*st_entry->repo));
926924
if (repo_submodule_init(st_entry->repo, r, tree_path,
927925
root_tree))
928926
FREE_AND_NULL(st_entry->repo);
929927

930-
} else if (S_ISDIR(name_entry->mode))
928+
} else if (S_ISDIR(name_entry.mode))
931929
traverse_tree_submodules(r, root_tree, tree_path,
932-
&name_entry->oid, out);
930+
&name_entry.oid, out);
933931
free(tree_path);
934932
}
935933
}

0 commit comments

Comments
 (0)