Skip to content

Commit 73546c0

Browse files
committed
handle_options(): do not miscount how many arguments were used
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 <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 06eb708 commit 73546c0

File tree

2 files changed

+3
-5
lines changed

2 files changed

+3
-5
lines changed

git.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static void commit_pager_choice(void) {
5555

5656
static int handle_options(const char ***argv, int *argc, int *envchanged)
5757
{
58-
int handled = 0;
58+
const char **orig_argv = *argv;
5959

6060
while (*argc > 0) {
6161
const char *cmd = (*argv)[0];
@@ -105,7 +105,6 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
105105
*envchanged = 1;
106106
(*argv)++;
107107
(*argc)--;
108-
handled++;
109108
} else if (!prefixcmp(cmd, "--git-dir=")) {
110109
setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1);
111110
if (envchanged)
@@ -145,9 +144,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
145144

146145
(*argv)++;
147146
(*argc)--;
148-
handled++;
149147
}
150-
return handled;
148+
return (*argv) - orig_argv;
151149
}
152150

153151
static int handle_alias(int *argcp, const char ***argv)

t/t1300-repo-config.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ test_expect_success 'git -c "key=value" support' '
854854
test_must_fail git -c core.name=value config name
855855
'
856856

857-
test_expect_failure 'git -c works with aliases of builtins' '
857+
test_expect_success 'git -c works with aliases of builtins' '
858858
git config alias.checkconfig "-c foo.check=bar config foo.check" &&
859859
echo bar >expect &&
860860
git checkconfig >actual &&

0 commit comments

Comments
 (0)