summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2024-09-30 11:13:24 +0200
committerJunio C Hamano <gitster@pobox.com>2024-09-30 20:23:03 +0200
commit666643fa89e4386c0f4eabd1112e8b3af0cb9cc1 (patch)
tree4bae05b501fa4eed53e7641520a2cef439923e1d
parentshell: fix leaking strings (diff)
downloadgit-666643fa89e4386c0f4eabd1112e8b3af0cb9cc1.tar.xz
git-666643fa89e4386c0f4eabd1112e8b3af0cb9cc1.zip
wt-status: fix leaking buffer with sparse directories
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rwxr-xr-xt/t1092-sparse-checkout-compatibility.sh1
-rw-r--r--wt-status.c6
2 files changed, 6 insertions, 1 deletions
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)