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 <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ævar Arnfjörð Bjarmason committed
Feb 7, 2023 at 00:07 UTC
9f24f3c719ccb4553f97a861c37cbedcc7357762
3 files changed
+5
-3
builtin/worktree.c
+3
-3
@@ -173,7 +173,7 @@ static void prune_worktrees(void)
173
{
174
struct strbuf reason = STRBUF_INIT;
175
struct strbuf main_path = STRBUF_INIT;
176
- struct string_list kept = STRING_LIST_INIT_NODUP;
176
+ struct string_list kept = STRING_LIST_INIT_DUP;
177
DIR *dir = opendir(git_path("worktrees"));
178
struct dirent *d;
179
if (!dir)
@@ -184,14 +184,14 @@ static void prune_worktrees(void)
184
if (should_prune_worktree(d->d_name, &reason, &path, expire))
185
prune_worktree(d->d_name, reason.buf);
186
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);
188
}
189
closedir(dir);
190
191
strbuf_add_absolute_path(&main_path, get_git_common_dir());
192
/* massage main worktree absolute path to match 'gitdir' content */
193
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));
195
prune_dups(&kept);
196
string_list_clear(&kept, 1);
197
t/t2401-worktree-prune.sh
+1
@@ -5,6 +5,7 @@ test_description='prune $GIT_DIR/worktrees'
5
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8
+TEST_PASSES_SANITIZE_LEAK=true
9
. ./test-lib.sh
10
11
test_expect_success initialize '
t/t2406-worktree-repair.sh
+1
@@ -2,6 +2,7 @@
2
3
test_description='test git worktree repair'
4
5
+TEST_PASSES_SANITIZE_LEAK=true
6
. ./test-lib.sh
7
8
test_expect_success setup '