builtin/worktree: fix leaking derived branch names
There are several heuristics that git-worktree(1) uses to derive the name of the newly created branch when not given explicitly. These heuristics all allocate a new string, but we only end up freeing that string in a subset of cases. Fix the remaining cases where we didn't yet free the derived branch names. While at it, also free `opt_track`, which is being populated via an `OPT_PASSTHRU()`. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Aug 1, 2024 at 12:40 UTC
cd6d7630fa51ad16afa010c6619870cab6c6faba
3 files changed
+6
-3
builtin/worktree.c
+4
-3
@@ -769,7 +769,7 @@ static int add(int ac, const char **av, const char *prefix)
769
char *branch_to_free = NULL;
770
char *new_branch_to_free = NULL;
771
const char *new_branch = NULL;
772
- const char *opt_track = NULL;
772
+ char *opt_track = NULL;
773
const char *lock_reason = NULL;
774
int keep_locked = 0;
775
int used_new_branch_options;
@@ -846,7 +846,7 @@ static int add(int ac, const char **av, const char *prefix)
846
if (opts.orphan && !new_branch) {
847
int n;
848
const char *s = worktree_basename(path, &n);
849
- new_branch = xstrndup(s, n);
849
+ new_branch = new_branch_to_free = xstrndup(s, n);
850
} else if (opts.orphan) {
851
; /* no-op */
852
} else if (opts.detach) {
@@ -875,7 +875,7 @@ static int add(int ac, const char **av, const char *prefix)
875
remote = unique_tracking_name(branch, &oid, NULL);
876
if (remote) {
877
new_branch = branch;
878
- branch = remote;
878
+ branch = new_branch_to_free = remote;
879
}
880
}
881
@@ -923,6 +923,7 @@ static int add(int ac, const char **av, const char *prefix)
923
924
ret = add_worktree(path, branch, &opts);
925
free(path);
926
+ free(opt_track);
927
free(branch_to_free);
928
free(new_branch_to_free);
929
return ret;
t/t2400-worktree-add.sh
+1
@@ -6,6 +6,7 @@ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8
TEST_CREATE_REPO_NO_TEMPLATE=1
9
+TEST_PASSES_SANITIZE_LEAK=true
10
. ./test-lib.sh
11
12
. "$TEST_DIRECTORY"/lib-rebase.sh
t/t9902-completion.sh
+1
@@ -16,6 +16,7 @@ test_untraceable=UnfortunatelyYes
16
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
17
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
18
19
+TEST_PASSES_SANITIZE_LEAK=true
20
. ./lib-bash.sh
21
22
complete ()