Skip to content

Commit 9f24f3c

Browse files
avargitster
authored andcommitted
worktree: fix a trivial leak in prune_worktrees()
We were leaking both the "struct strbuf" in prune_worktrees(), as well as the "path" we got from should_prune_worktree(). Since these were the only two uses of the "struct string_list" let's change it to a "DUP" and push these to it with "string_list_append_nodup()". For the string_list_append_nodup() we could also string_list_append() the main_path.buf, and then strbuf_release(&main_path) right away. But doing it this way avoids an allocation, as we already have the "struct strbuf" prepared for appending to "kept". Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 90428dd commit 9f24f3c

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

builtin/worktree.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ static void prune_worktrees(void)
173173
{
174174
struct strbuf reason = STRBUF_INIT;
175175
struct strbuf main_path = STRBUF_INIT;
176-
struct string_list kept = STRING_LIST_INIT_NODUP;
176+
struct string_list kept = STRING_LIST_INIT_DUP;
177177
DIR *dir = opendir(git_path("worktrees"));
178178
struct dirent *d;
179179
if (!dir)
@@ -184,14 +184,14 @@ static void prune_worktrees(void)
184184
if (should_prune_worktree(d->d_name, &reason, &path, expire))
185185
prune_worktree(d->d_name, reason.buf);
186186
else if (path)
187-
string_list_append(&kept, path)->util = xstrdup(d->d_name);
187+
string_list_append_nodup(&kept, path)->util = xstrdup(d->d_name);
188188
}
189189
closedir(dir);
190190

191191
strbuf_add_absolute_path(&main_path, get_git_common_dir());
192192
/* massage main worktree absolute path to match 'gitdir' content */
193193
strbuf_strip_suffix(&main_path, "/.");
194-
string_list_append(&kept, strbuf_detach(&main_path, NULL));
194+
string_list_append_nodup(&kept, strbuf_detach(&main_path, NULL));
195195
prune_dups(&kept);
196196
string_list_clear(&kept, 1);
197197

t/t2401-worktree-prune.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test_description='prune $GIT_DIR/worktrees'
55
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
66
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
77

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

1011
test_expect_success initialize '

t/t2406-worktree-repair.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
test_description='test git worktree repair'
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67

78
test_expect_success setup '

0 commit comments

Comments
 (0)