Skip to content

Commit 3af1cae

Browse files
committed
show-branch: fix segfault when showbranch.default exists
When running "git show-branch" without any parameter in a repository that has showbranch.default defined, we used to rely on the fact that our handcrafted option parsing loop never looked at av[0]. The array of default strings had the first real command line argument in default_arg[0], but the option parser wanted to look at the array starting at av[1], so we assigned the address of -1th element to av to force the loop start working from default_arg[0]. This no longer worked since 5734365 (show-branch: migrate to parse-options API, 2009-05-21), as parse_options_start() saved the incoming &av[0] in its ctx->out and later in parse_options_end() it did memmove to ctx->out (with ctx->cpidx == 0), overwriting the memory before default_arg[] array. I am not sure if this is a bug in parse_options(), or a bug in the caller, and tonight I do not have enough concentration to figure out which. In any case, this patch works the issue around. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5734365 commit 3af1cae

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

builtin-show-branch.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,15 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
565565
if (!strcmp(var, "showbranch.default")) {
566566
if (!value)
567567
return config_error_nonbool(var);
568-
if (default_alloc <= default_num + 1) {
568+
/*
569+
* default_arg is now passed to parse_options(), so we need to
570+
* mimick the real argv a bit better.
571+
*/
572+
if (!default_num) {
573+
default_alloc = 20;
574+
default_arg = xcalloc(default_alloc, sizeof(*default_arg));
575+
default_arg[default_num++] = "show-branch";
576+
} else if (default_alloc <= default_num + 1) {
569577
default_alloc = default_alloc * 3 / 2 + 20;
570578
default_arg = xrealloc(default_arg, sizeof *default_arg * default_alloc);
571579
}
@@ -693,8 +701,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
693701

694702
/* If nothing is specified, try the default first */
695703
if (ac == 1 && default_num) {
696-
ac = default_num + 1;
697-
av = default_arg - 1; /* ick; we would not address av[0] */
704+
ac = default_num;
705+
av = default_arg;
698706
}
699707

700708
ac = parse_options(ac, av, builtin_show_branch_options,

t/t3202-show-branch-octopus.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,12 @@ test_expect_success 'show-branch with more than 8 branches' '
5656
5757
'
5858

59+
test_expect_success 'show-branch with showbranch.default' '
60+
for i in $numbers; do
61+
git config --add showbranch.default branch$i
62+
done &&
63+
git show-branch >out &&
64+
test_cmp expect out
65+
'
66+
5967
test_done

0 commit comments

Comments
 (0)