git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] show-branch: fix segfault when showbranch.default exists

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 9, 2009, 06:26 UTC
Message-ID
<7vfxe9udln.fsf@alter.siamese.dyndns.org>

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 <gitster@pobox.com>
---
 builtin-show-branch.c |   14 +++++++++++---
 1 files changed, 11 insertions(+), 3 deletions(-)
diff --git a/builtin-show-branch.c b/builtin-show-branch.c
index 01bea3b..baec9ed 100644
--- a/builtin-show-branch.c
+++ b/builtin-show-branch.c
@@ -565,7 +565,15 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
 	if (!strcmp(var, "showbranch.default")) {
 		if (!value)
 			return config_error_nonbool(var);
-		if (default_alloc <= default_num + 1) {
+		/*
+		 * default_arg is now passed to parse_options(), so we need to
+		 * mimick the real argv a bit better.
+		 */
+		if (!default_num) {
+			default_alloc = 20;
+			default_arg = xcalloc(default_alloc, sizeof(*default_arg));
+			default_arg[default_num++] = "show-branch";
+		} else if (default_alloc <= default_num + 1) {
 			default_alloc = default_alloc * 3 / 2 + 20;
 			default_arg = xrealloc(default_arg, sizeof *default_arg * default_alloc);
 		}
@@ -692,8 +700,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 
 	/* If nothing is specified, try the default first */
 	if (ac == 1 && default_num) {
-		ac = default_num + 1;
-		av = default_arg - 1; /* ick; we would not address av[0] */
+		ac = default_num;
+		av = default_arg;
 	}
 
 	ac = parse_options(ac, av, prefix, builtin_show_branch_options,
Next: Stephen Boyd
Message 1 of 12 in “show-branch: fix segfault when showbranch.default exists”
  1. show-branch: fix segfault when showbranch.default existsJunio C Hamano, Jun 9, 2009
  2. Stephen BoydJun 9, 2009
  3. Pierre HabouzitJun 9, 2009
  4. Junio C HamanoJun 9, 2009
  5. Pierre HabouzitJun 9, 2009
  6. branch managementHarry Duin, Jun 9, 2009
  7. Alex RiesenJun 9, 2009
  8. Harry DuinJun 10, 2009
  9. Jakub NarebskiJun 10, 2009
  10. Nicolas PitreJun 10, 2009
  11. Linus TorvaldsJun 10, 2009
  12. Daniel BarkalowJun 10, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.