Skip to content

Commit 69cbeaa

Browse files
peffdscho
authored andcommitted
merge-recursive: copy $GITHEAD strings
If $GITHEAD_1234abcd is set in the environment, we use its value as a "better branch name" in generating conflict markers. However, we pick these better names early in the process, and the return value from getenv() is not guaranteed to stay valid. Let's make a copy of the returned string. And to make memory management easier, let's just always return an allocated string from better_branch_name(), so we know that it must always be freed. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5bb5a8b commit 69cbeaa

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

builtin/merge-recursive.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
static const char builtin_merge_recursive_usage[] =
88
"git %s <base>... -- <head> <remote> ...";
99

10-
static const char *better_branch_name(const char *branch)
10+
static char *better_branch_name(const char *branch)
1111
{
1212
static char githead_env[8 + GIT_MAX_HEXSZ + 1];
1313
char *name;
1414

1515
if (strlen(branch) != the_hash_algo->hexsz)
16-
return branch;
16+
return xstrdup(branch);
1717
xsnprintf(githead_env, sizeof(githead_env), "GITHEAD_%s", branch);
1818
name = getenv(githead_env);
19-
return name ? name : branch;
19+
return xstrdup(name ? name : branch);
2020
}
2121

2222
int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
@@ -26,6 +26,7 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
2626
int i, failed;
2727
struct object_id h1, h2;
2828
struct merge_options o;
29+
char *better1, *better2;
2930
struct commit *result;
3031

3132
init_merge_options(&o);
@@ -70,13 +71,17 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
7071
if (get_oid(o.branch2, &h2))
7172
die(_("could not resolve ref '%s'"), o.branch2);
7273

73-
o.branch1 = better_branch_name(o.branch1);
74-
o.branch2 = better_branch_name(o.branch2);
74+
o.branch1 = better1 = better_branch_name(o.branch1);
75+
o.branch2 = better2 = better_branch_name(o.branch2);
7576

7677
if (o.verbosity >= 3)
7778
printf(_("Merging %s with %s\n"), o.branch1, o.branch2);
7879

7980
failed = merge_recursive_generic(&o, &h1, &h2, bases_count, bases, &result);
81+
82+
free(better1);
83+
free(better2);
84+
8085
if (failed < 0)
8186
return 128; /* die() error code */
8287
return failed;

0 commit comments

Comments
 (0)