Skip to content

Commit

Permalink
handle_options(): do not miscount how many arguments were used
Browse files Browse the repository at this point in the history
The handle_options() function advances the base of the argument array and
returns the number of arguments it used. The caller in handle_alias()
wants to reallocate the argv array it passes to this function, and
attempts to do so by subtracting the returned value to compensate for the
change handle_options() makes to the new_argv.

But handle_options() did not correctly count when "-c <config=value>" is
given, causing a wrong pointer to be passed to realloc().

Fix it by saving the original argv at the beginning of handle_options(),
and return the difference between the final value of argv, which will
relieve the places that move the array pointer from the additional burden
of keeping track of "handled" counter.

Noticed-by: Kazuki Tsujimoto
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Junio C Hamano committed May 24, 2011
1 parent 06eb708 commit 73546c0
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
6 changes: 2 additions & 4 deletions git.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static void commit_pager_choice(void) {

static int handle_options(const char ***argv, int *argc, int *envchanged)
{
int handled = 0;
const char **orig_argv = *argv;

while (*argc > 0) {
const char *cmd = (*argv)[0];
Expand Down Expand Up @@ -105,7 +105,6 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
*envchanged = 1;
(*argv)++;
(*argc)--;
handled++;
} else if (!prefixcmp(cmd, "--git-dir=")) {
setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1);
if (envchanged)
Expand Down Expand Up @@ -145,9 +144,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)

(*argv)++;
(*argc)--;
handled++;
}
return handled;
return (*argv) - orig_argv;
}

static int handle_alias(int *argcp, const char ***argv)
Expand Down
2 changes: 1 addition & 1 deletion t/t1300-repo-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ test_expect_success 'git -c "key=value" support' '
test_must_fail git -c core.name=value config name
'

test_expect_failure 'git -c works with aliases of builtins' '
test_expect_success 'git -c works with aliases of builtins' '
git config alias.checkconfig "-c foo.check=bar config foo.check" &&
echo bar >expect &&
git checkconfig >actual &&
Expand Down

0 comments on commit 73546c0

Please sign in to comment.