Skip to content

Commit cd6d763

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

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

builtin/worktree.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ static int add(int ac, const char **av, const char *prefix)
769769
char *branch_to_free = NULL;
770770
char *new_branch_to_free = NULL;
771771
const char *new_branch = NULL;
772-
const char *opt_track = NULL;
772+
char *opt_track = NULL;
773773
const char *lock_reason = NULL;
774774
int keep_locked = 0;
775775
int used_new_branch_options;
@@ -846,7 +846,7 @@ static int add(int ac, const char **av, const char *prefix)
846846
if (opts.orphan && !new_branch) {
847847
int n;
848848
const char *s = worktree_basename(path, &n);
849-
new_branch = xstrndup(s, n);
849+
new_branch = new_branch_to_free = xstrndup(s, n);
850850
} else if (opts.orphan) {
851851
; /* no-op */
852852
} else if (opts.detach) {
@@ -875,7 +875,7 @@ static int add(int ac, const char **av, const char *prefix)
875875
remote = unique_tracking_name(branch, &oid, NULL);
876876
if (remote) {
877877
new_branch = branch;
878-
branch = remote;
878+
branch = new_branch_to_free = remote;
879879
}
880880
}
881881

@@ -923,6 +923,7 @@ static int add(int ac, const char **av, const char *prefix)
923923

924924
ret = add_worktree(path, branch, &opts);
925925
free(path);
926+
free(opt_track);
926927
free(branch_to_free);
927928
free(new_branch_to_free);
928929
return ret;

t/t2400-worktree-add.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
66
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
77

88
TEST_CREATE_REPO_NO_TEMPLATE=1
9+
TEST_PASSES_SANITIZE_LEAK=true
910
. ./test-lib.sh
1011

1112
. "$TEST_DIRECTORY"/lib-rebase.sh

t/t9902-completion.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ test_untraceable=UnfortunatelyYes
1616
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
1717
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
1818

19+
TEST_PASSES_SANITIZE_LEAK=true
1920
. ./lib-bash.sh
2021

2122
complete ()

0 commit comments

Comments
 (0)