Skip to content

Commit f535592

Browse files
dschogitster
authored andcommitted
submodule-config: avoid memory leak
In 961b130 (branch: add --recurse-submodules option for branch creation, 2022-01-28), a funny pattern was introduced where first some struct is `xmalloc()`ed, then we resize an array whose element type is the same struct, and then the first struct's contents are copied into the last element of that array. Crucially, the `xmalloc()`ed memory never gets released. Let's avoid that memory leak and that memory allocation dance altogether by first reallocating the array, then using a pointer to the last array element to go forward. Reported by Coverity. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5a09991 commit f535592

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

submodule-config.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,10 @@ static void traverse_tree_submodules(struct repository *r,
756756

757757
if (S_ISGITLINK(name_entry->mode) &&
758758
is_tree_submodule_active(r, root_tree, tree_path)) {
759-
st_entry = xmalloc(sizeof(*st_entry));
759+
ALLOC_GROW(out->entries, out->entry_nr + 1,
760+
out->entry_alloc);
761+
st_entry = &out->entries[out->entry_nr++];
762+
760763
st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry));
761764
*st_entry->name_entry = *name_entry;
762765
st_entry->submodule =
@@ -766,9 +769,6 @@ static void traverse_tree_submodules(struct repository *r,
766769
root_tree))
767770
FREE_AND_NULL(st_entry->repo);
768771

769-
ALLOC_GROW(out->entries, out->entry_nr + 1,
770-
out->entry_alloc);
771-
out->entries[out->entry_nr++] = *st_entry;
772772
} else if (S_ISDIR(name_entry->mode))
773773
traverse_tree_submodules(r, root_tree, tree_path,
774774
&name_entry->oid, out);

0 commit comments

Comments
 (0)