git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Subject: [PATCH 05/23] wt-status: fix leaking buffer with sparse directories
Date: Mon, 30 Sep 2024 11:13:24 +0200	[thread overview]
Message-ID: <4196fea159a98ff9a25ca0f65a9c669f45155a7b.1727687410.git.ps@pks.im> (raw)
In-Reply-To: <cover.1727687410.git.ps@pks.im>

When hitting a sparse directory in `wt_status_collect_changes_initial()`
we use a `struct strbuf` to assemble the directory's name. We never free
that buffer though, causing a memory leak.

Fix the leak by releasing the buffer. While at it, move the buffer
outside of the loop and reset it to save on some wasteful allocations.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 t/t1092-sparse-checkout-compatibility.sh | 1 +
 wt-status.c                              | 6 +++++-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/t/t1092-sparse-checkout-compatibility.sh b/t/t1092-sparse-checkout-compatibility.sh
index eb32da2a7f..55efafe4e0 100755
--- a/t/t1092-sparse-checkout-compatibility.sh
+++ b/t/t1092-sparse-checkout-compatibility.sh
@@ -5,6 +5,7 @@ test_description='compare full workdir to sparse workdir'
 GIT_TEST_SPLIT_INDEX=0
 GIT_TEST_SPARSE_INDEX=
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup' '
diff --git a/wt-status.c b/wt-status.c
index 6a6397ca8f..6a8c05d1cf 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -717,6 +717,7 @@ static int add_file_to_list(const struct object_id *oid,
 static void wt_status_collect_changes_initial(struct wt_status *s)
 {
 	struct index_state *istate = s->repo->index;
+	struct strbuf base = STRBUF_INIT;
 	int i;
 
 	for (i = 0; i < istate->cache_nr; i++) {
@@ -735,7 +736,6 @@ static void wt_status_collect_changes_initial(struct wt_status *s)
 			 * expanding the trees to find the elements that are new in this
 			 * tree and marking them with DIFF_STATUS_ADDED.
 			 */
-			struct strbuf base = STRBUF_INIT;
 			struct pathspec ps = { 0 };
 			struct tree *tree = lookup_tree(istate->repo, &ce->oid);
 
@@ -743,9 +743,11 @@ static void wt_status_collect_changes_initial(struct wt_status *s)
 			ps.has_wildcard = 1;
 			ps.max_depth = -1;
 
+			strbuf_reset(&base);
 			strbuf_add(&base, ce->name, ce->ce_namelen);
 			read_tree_at(istate->repo, tree, &base, 0, &ps,
 				     add_file_to_list, s);
+
 			continue;
 		}
 
@@ -772,6 +774,8 @@ static void wt_status_collect_changes_initial(struct wt_status *s)
 			s->committable = 1;
 		}
 	}
+
+	strbuf_release(&base);
 }
 
 static void wt_status_collect_untracked(struct wt_status *s)
-- 
2.46.2.852.g229c0bf0e5.dirty


  parent reply	other threads:[~2024-09-30  9:13 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-30  9:13 [PATCH 00/23] Memory leak fixes (pt.8) Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 01/23] builtin/annotate: fix leaking args vector Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 02/23] read-cache: fix leaking hash context in `do_write_index()` Patrick Steinhardt
2024-09-30 14:05   ` Derrick Stolee
2024-09-30  9:13 ` [PATCH 03/23] scalar: fix leaking repositories Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 04/23] shell: fix leaking strings Patrick Steinhardt
2024-09-30  9:13 ` Patrick Steinhardt [this message]
2024-09-30  9:13 ` [PATCH 06/23] submodule: fix leaking submodule entry list Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 07/23] builtin/stash: fix leaking `pathspec_from_file` Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 08/23] builtin/pack-redundant: fix various memory leaks Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 09/23] builtin/clone: fix leaking repo state when cloning with bundle URIs Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 10/23] t/helper: fix leaking repository in partial-clone helper Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 11/23] builtin/revert: fix leaking `gpg_sign` and `strategy` config Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 12/23] diff: improve lifecycle management of diff queues Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 13/23] line-log: fix several memory leaks Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 14/23] pseudo-merge: fix various " Patrick Steinhardt
2024-09-30 16:21   ` Taylor Blau
2024-09-30  9:13 ` [PATCH 15/23] pseudo-merge: fix leaking strmap keys Patrick Steinhardt
2024-09-30 21:22   ` Taylor Blau
2024-10-07  9:41     ` Patrick Steinhardt
2024-10-08  8:54       ` Jeff King
2024-10-09  7:06         ` Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 16/23] pack-bitmap-write: fix leaking OID array Patrick Steinhardt
2024-09-30 21:22   ` Taylor Blau
2024-09-30  9:14 ` [PATCH 17/23] midx-write: fix leaking buffer Patrick Steinhardt
2024-09-30 21:27   ` Taylor Blau
2024-10-07  9:41     ` Patrick Steinhardt
2024-09-30  9:14 ` [PATCH 18/23] revision: fix memory leaks when rewriting parents Patrick Steinhardt
2024-09-30  9:14 ` [PATCH 19/23] revision: fix leaking saved parents Patrick Steinhardt
2024-09-30  9:14 ` [PATCH 20/23] pack-write: fix return parameter of `write_rev_file_order()` Patrick Steinhardt
2024-09-30  9:14 ` [PATCH 21/23] t/helper: fix leaks in proc-receive helper Patrick Steinhardt
2024-09-30  9:14 ` [PATCH 22/23] remote: fix leaking push reports Patrick Steinhardt
2024-09-30  9:14 ` [PATCH 23/23] builtin/send-pack: fix leaking list of push options Patrick Steinhardt

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4196fea159a98ff9a25ca0f65a9c669f45155a7b.1727687410.git.ps@pks.im \
    --to=ps@pks.im \
    --cc=git@vger.kernel.org \
    /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.
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 NNTP newsgroup(s).