Re: [PATCH] Reserve a slot for argv[0] in default_arg.
From: Petter Urkedal <hidden>
Date: 2016-08-13 23:25:08
Subsystem:
the rest · Maintainer:
Linus Torvalds
On 2009-10-04, Jeff King wrote:
Thanks, your fix looks sane. But I am curious about whether we are triggering some glibc pickiness that is in your setup, or if we are somehow violating the assumption that we only ever look at default_arg[1] and beyond.
I had a look at parse-options.c. In parse_options_start, argv is assigned to ctx->out, which is overwritten from index 0 in parse_options_end. This will show the problem:
diff --git a/builtin-show-branch.c b/builtin-show-branch.c
index 3510a86..1c587ad 100644
--- a/builtin-show-branch.c
+++ b/builtin-show-branch.c@@ -691,6 +691,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) showbranch_use_color = git_use_color_default; /* If nothing is specified, try the default first */ + printf("default_arg = %p\n", default_arg); if (ac == 1 && default_num) { ac = default_num + 1; av = default_arg - 1; /* ick; we would not address av[0] */
diff --git a/parse-options.c b/parse-options.c
index f559411..267e752 100644
--- a/parse-options.c
+++ b/parse-options.c@@ -435,6 +435,7 @@ unknown: int parse_options_end(struct parse_opt_ctx_t *ctx) { + printf("Assigning to %p\n", ctx->out + ctx->cpidx); memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out)); ctx->out[ctx->cpidx + ctx->argc] = NULL; return ctx->cpidx + ctx->argc;