Skip to content

Commit cc395d6

Browse files
pks-tgitster
authored andcommitted
checkout: clarify memory ownership in unique_tracking_name()
The function `unique_tracking_name()` returns an allocated string, but does not clearly indicate this because its return type is `const char *` instead of `char *`. This has led to various callsites where we never free its returned memory at all, which causes memory leaks. Plug those leaks and mark now-passing tests as leak free. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 94e2aa5 commit cc395d6

14 files changed

+34
-20
lines changed

builtin/checkout.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,12 +1275,12 @@ static void setup_new_branch_info_and_source_tree(
12751275
}
12761276
}
12771277

1278-
static const char *parse_remote_branch(const char *arg,
1279-
struct object_id *rev,
1280-
int could_be_checkout_paths)
1278+
static char *parse_remote_branch(const char *arg,
1279+
struct object_id *rev,
1280+
int could_be_checkout_paths)
12811281
{
12821282
int num_matches = 0;
1283-
const char *remote = unique_tracking_name(arg, rev, &num_matches);
1283+
char *remote = unique_tracking_name(arg, rev, &num_matches);
12841284

12851285
if (remote && could_be_checkout_paths) {
12861286
die(_("'%s' could be both a local file and a tracking branch.\n"
@@ -1316,6 +1316,7 @@ static int parse_branchname_arg(int argc, const char **argv,
13161316
const char **new_branch = &opts->new_branch;
13171317
int argcount = 0;
13181318
const char *arg;
1319+
char *remote = NULL;
13191320
int dash_dash_pos;
13201321
int has_dash_dash = 0;
13211322
int i;
@@ -1416,8 +1417,8 @@ static int parse_branchname_arg(int argc, const char **argv,
14161417
recover_with_dwim = 0;
14171418

14181419
if (recover_with_dwim) {
1419-
const char *remote = parse_remote_branch(arg, rev,
1420-
could_be_checkout_paths);
1420+
remote = parse_remote_branch(arg, rev,
1421+
could_be_checkout_paths);
14211422
if (remote) {
14221423
*new_branch = arg;
14231424
arg = remote;
@@ -1459,6 +1460,7 @@ static int parse_branchname_arg(int argc, const char **argv,
14591460
argc--;
14601461
}
14611462

1463+
free(remote);
14621464
return argcount;
14631465
}
14641466

builtin/worktree.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -736,16 +736,14 @@ static int dwim_orphan(const struct add_opts *opts, int opt_track, int remote)
736736
return 1;
737737
}
738738

739-
static const char *dwim_branch(const char *path, const char **new_branch)
739+
static char *dwim_branch(const char *path, char **new_branch)
740740
{
741741
int n;
742742
int branch_exists;
743743
const char *s = worktree_basename(path, &n);
744-
const char *branchname = xstrndup(s, n);
744+
char *branchname = xstrndup(s, n);
745745
struct strbuf ref = STRBUF_INIT;
746746

747-
UNLEAK(branchname);
748-
749747
branch_exists = !strbuf_check_branch_ref(&ref, branchname) &&
750748
refs_ref_exists(get_main_ref_store(the_repository),
751749
ref.buf);
@@ -756,8 +754,7 @@ static const char *dwim_branch(const char *path, const char **new_branch)
756754
*new_branch = branchname;
757755
if (guess_remote) {
758756
struct object_id oid;
759-
const char *remote =
760-
unique_tracking_name(*new_branch, &oid, NULL);
757+
char *remote = unique_tracking_name(*new_branch, &oid, NULL);
761758
return remote;
762759
}
763760
return NULL;
@@ -769,6 +766,8 @@ static int add(int ac, const char **av, const char *prefix)
769766
const char *new_branch_force = NULL;
770767
char *path;
771768
const char *branch;
769+
char *branch_to_free = NULL;
770+
char *new_branch_to_free = NULL;
772771
const char *new_branch = NULL;
773772
const char *opt_track = NULL;
774773
const char *lock_reason = NULL;
@@ -859,16 +858,17 @@ static int add(int ac, const char **av, const char *prefix)
859858
opts.orphan = dwim_orphan(&opts, !!opt_track, 0);
860859
} else if (ac < 2) {
861860
/* DWIM: Guess branch name from path. */
862-
const char *s = dwim_branch(path, &new_branch);
861+
char *s = dwim_branch(path, &new_branch_to_free);
863862
if (s)
864-
branch = s;
863+
branch = branch_to_free = s;
864+
new_branch = new_branch_to_free;
865865

866866
/* DWIM: Infer --orphan when repo has no refs. */
867867
opts.orphan = (!s) && dwim_orphan(&opts, !!opt_track, 1);
868868
} else if (ac == 2) {
869869
struct object_id oid;
870870
struct commit *commit;
871-
const char *remote;
871+
char *remote;
872872

873873
commit = lookup_commit_reference_by_name(branch);
874874
if (!commit) {
@@ -923,6 +923,8 @@ static int add(int ac, const char **av, const char *prefix)
923923

924924
ret = add_worktree(path, branch, &opts);
925925
free(path);
926+
free(branch_to_free);
927+
free(new_branch_to_free);
926928
return ret;
927929
}
928930

checkout.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
4545
return 0;
4646
}
4747

48-
const char *unique_tracking_name(const char *name, struct object_id *oid,
49-
int *dwim_remotes_matched)
48+
char *unique_tracking_name(const char *name, struct object_id *oid,
49+
int *dwim_remotes_matched)
5050
{
5151
struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT;
5252
const char *default_remote = NULL;

checkout.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
* tracking branch. Return the name of the remote if such a branch
99
* exists, NULL otherwise.
1010
*/
11-
const char *unique_tracking_name(const char *name,
12-
struct object_id *oid,
13-
int *dwim_remotes_matched);
11+
char *unique_tracking_name(const char *name,
12+
struct object_id *oid,
13+
int *dwim_remotes_matched);
1414

1515
#endif /* CHECKOUT_H */

t/t2024-checkout-dwim.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ test_description='checkout <branch>
44
55
Ensures that checkout on an unborn branch does what the user expects'
66

7+
TEST_PASSES_SANITIZE_LEAK=true
78
. ./test-lib.sh
89

910
# Is the current branch "refs/heads/$1"?

t/t2060-switch.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test_description='switch basic functionality'
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 'setup' '

t/t3426-rebase-submodule.sh

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

33
test_description='rebase can handle submodules'
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67
. "$TEST_DIRECTORY"/lib-submodule-update.sh
78
. "$TEST_DIRECTORY"/lib-rebase.sh

t/t3512-cherry-pick-submodule.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test_description='cherry-pick can handle submodules'
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
. "$TEST_DIRECTORY"/lib-submodule-update.sh
1011

t/t3513-revert-submodule.sh

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

33
test_description='revert can handle submodules'
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67
. "$TEST_DIRECTORY"/lib-submodule-update.sh
78

t/t3600-rm.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ test_description='Test of the various options to git rm.'
88
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
99
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
1010

11+
TEST_PASSES_SANITIZE_LEAK=true
1112
. ./test-lib.sh
1213

1314
# Setup some files to be removed, some with funny characters

0 commit comments

Comments
 (0)