diff options
author | Johannes Schindelin <johannes.schindelin@gmx.de> | 2022-06-16 01:35:39 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2022-06-16 22:22:03 +0200 |
commit | f53559227ccb600f4fdd1bfe08e1164a5aed60b5 (patch) | |
tree | 09c614f7823f06f921e5eba4035f26eff86b6fa2 /submodule-config.c | |
parent | fsmonitor: avoid memory leak in `fsm_settings__get_incompatible_msg()` (diff) | |
download | git-f53559227ccb600f4fdd1bfe08e1164a5aed60b5.tar.xz git-f53559227ccb600f4fdd1bfe08e1164a5aed60b5.zip |
submodule-config: avoid memory leak
In 961b130d20c9 (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 <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'submodule-config.c')
-rw-r--r-- | submodule-config.c | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/submodule-config.c b/submodule-config.c index ce3beaf5d4..51ecbe901e 100644 --- a/submodule-config.c +++ b/submodule-config.c @@ -756,7 +756,10 @@ static void traverse_tree_submodules(struct repository *r, if (S_ISGITLINK(name_entry->mode) && is_tree_submodule_active(r, root_tree, tree_path)) { - st_entry = xmalloc(sizeof(*st_entry)); + 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->submodule = @@ -766,9 +769,6 @@ static void traverse_tree_submodules(struct repository *r, root_tree)) FREE_AND_NULL(st_entry->repo); - ALLOC_GROW(out->entries, out->entry_nr + 1, - out->entry_alloc); - out->entries[out->entry_nr++] = *st_entry; } else if (S_ISDIR(name_entry->mode)) traverse_tree_submodules(r, root_tree, tree_path, &name_entry->oid, out); |