Skip to content

Commit 666643f

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c758416 commit 666643f

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test_description='compare full workdir to sparse workdir'
55
GIT_TEST_SPLIT_INDEX=0
66
GIT_TEST_SPARSE_INDEX=
77

8+
TEST_PASSES_SANITIZE_LEAK=true
89
. ./test-lib.sh
910

1011
test_expect_success 'setup' '

wt-status.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ static int add_file_to_list(const struct object_id *oid,
717717
static void wt_status_collect_changes_initial(struct wt_status *s)
718718
{
719719
struct index_state *istate = s->repo->index;
720+
struct strbuf base = STRBUF_INIT;
720721
int i;
721722

722723
for (i = 0; i < istate->cache_nr; i++) {
@@ -735,17 +736,18 @@ static void wt_status_collect_changes_initial(struct wt_status *s)
735736
* expanding the trees to find the elements that are new in this
736737
* tree and marking them with DIFF_STATUS_ADDED.
737738
*/
738-
struct strbuf base = STRBUF_INIT;
739739
struct pathspec ps = { 0 };
740740
struct tree *tree = lookup_tree(istate->repo, &ce->oid);
741741

742742
ps.recursive = 1;
743743
ps.has_wildcard = 1;
744744
ps.max_depth = -1;
745745

746+
strbuf_reset(&base);
746747
strbuf_add(&base, ce->name, ce->ce_namelen);
747748
read_tree_at(istate->repo, tree, &base, 0, &ps,
748749
add_file_to_list, s);
750+
749751
continue;
750752
}
751753

@@ -772,6 +774,8 @@ static void wt_status_collect_changes_initial(struct wt_status *s)
772774
s->committable = 1;
773775
}
774776
}
777+
778+
strbuf_release(&base);
775779
}
776780

777781
static void wt_status_collect_untracked(struct wt_status *s)

0 commit comments

Comments
 (0)