From: "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Johannes Schindelin <johannes.schindelin@gmx.de>,
Johannes Schindelin <johannes.schindelin@gmx.de>
Subject: [PATCH 05/11] submodule-config: avoid memory leak
Date: Wed, 15 Jun 2022 23:35:39 +0000 [thread overview]
Message-ID: <591166e07d87fdb5efc2769d3e2963e3f0412720.1655336146.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1264.git.1655336146.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>
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>
---
submodule-config.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/submodule-config.c b/submodule-config.c
index ce3beaf5d4f..51ecbe901ec 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);
--
gitgitgadget
next prev parent reply other threads:[~2022-06-15 23:41 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-15 23:35 [PATCH 00/11] Coverity fixes Johannes Schindelin via GitGitGadget
2022-06-15 23:35 ` [PATCH 01/11] mingw: avoid accessing uninitialized memory in `is_executable()` Johannes Schindelin via GitGitGadget
2022-06-16 4:07 ` Junio C Hamano
2022-06-16 19:53 ` René Scharfe
2022-06-16 20:13 ` Junio C Hamano
2022-06-16 20:20 ` Junio C Hamano
2022-06-15 23:35 ` [PATCH 02/11] fsmonitor: avoid memory leak in `fsm_settings__get_incompatible_msg()` Johannes Schindelin via GitGitGadget
2022-06-16 4:10 ` Junio C Hamano
2022-06-15 23:35 ` [PATCH 03/11] submodule--helper: avoid memory leak in `update_submodule()` Johannes Schindelin via GitGitGadget
2022-06-16 4:23 ` Junio C Hamano
2022-06-16 17:51 ` Glen Choo
2022-06-15 23:35 ` [PATCH 04/11] get_oid_with_context_1(): avoid use-after-free Johannes Schindelin via GitGitGadget
2022-06-16 4:29 ` Junio C Hamano
2022-06-15 23:35 ` Johannes Schindelin via GitGitGadget [this message]
2022-06-16 4:36 ` [PATCH 05/11] submodule-config: avoid memory leak Junio C Hamano
2022-06-16 18:09 ` Glen Choo
2022-06-15 23:35 ` [PATCH 06/11] pack-redundant: avoid using uninitialized memory Johannes Schindelin via GitGitGadget
2022-06-16 4:53 ` Junio C Hamano
2022-06-15 23:35 ` [PATCH 07/11] submodule--helper: avoid memory leak when fetching submodules Johannes Schindelin via GitGitGadget
2022-06-16 4:55 ` Junio C Hamano
2022-06-15 23:35 ` [PATCH 08/11] read_index_from(): avoid memory leak Johannes Schindelin via GitGitGadget
2022-06-17 21:27 ` Tom Levy
2022-06-15 23:35 ` [PATCH 09/11] pack-mtimes: avoid closing a bogus file descriptor Johannes Schindelin via GitGitGadget
2022-06-16 20:43 ` Taylor Blau
2022-06-15 23:35 ` [PATCH 10/11] relative_url(): fix incorrect condition Johannes Schindelin via GitGitGadget
2022-06-16 5:02 ` Junio C Hamano
2022-06-16 13:09 ` Ævar Arnfjörð Bjarmason
2022-06-16 17:55 ` Junio C Hamano
2022-06-16 16:41 ` Junio C Hamano
2022-06-15 23:35 ` [PATCH 11/11] bug_fl(): add missing `va_end()` call Johannes Schindelin via GitGitGadget
2022-06-16 4:53 ` Jeff King
2022-06-16 5:00 ` Junio C Hamano
2022-06-16 13:02 ` Ævar Arnfjörð Bjarmason
2022-06-16 18:03 ` Junio C Hamano
2022-06-16 19:20 ` Jeff King
2022-06-16 20:04 ` [PATCH] bug_fl(): correctly initialize trace2 va_list Jeff King
2022-06-16 20:11 ` [PATCH 11/11] bug_fl(): add missing `va_end()` call Junio C Hamano
2022-06-16 4:05 ` [PATCH 00/11] Coverity fixes Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=591166e07d87fdb5efc2769d3e2963e3f0412720.1655336146.git.gitgitgadget@gmail.com \
--to=gitgitgadget@gmail.com \
--cc=git@vger.kernel.org \
--cc=johannes.schindelin@gmx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).