git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] handle_options(): do not miscount how many arguments were used
@ 2011-05-24 21:15 Junio C Hamano
  2011-05-24 21:18 ` [PATCH] Allow built-ins to also use -c var=val via alias Junio C Hamano
  0 siblings, 1 reply; 10+ messages in thread
From: Junio C Hamano @ 2011-05-24 21:15 UTC (permalink / raw)
  To: git; +Cc: Jeff King, Kazuki Tsujimoto, Alex Riesen

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>
---

 * This fixes 8b1fa77 (Allow passing of configuration parameters in the
   command line, 2010-03-26), and when applied there, the new test passes,
   but if applied to newer codebase, the test fails for a different
   reason, for which another fix will be sent out separately.

 git.c                  |    6 ++----
 t/t1300-repo-config.sh |   10 ++++++++++
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/git.c b/git.c
index 1753811..55c2eda 100644
--- a/git.c
+++ b/git.c
@@ -53,7 +53,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;
 
 	if (!getenv("GIT_ASKPASS") && getenv("SSH_ASKPASS"))
 		setenv("GIT_ASKPASS", getenv("SSH_ASKPASS"), 1);
@@ -106,7 +106,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)
@@ -146,9 +145,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)
diff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh
index 64f0508..5f6766d 100755
--- a/t/t1300-repo-config.sh
+++ b/t/t1300-repo-config.sh
@@ -832,4 +832,14 @@ test_expect_success 'git -c "key=value" support' '
 	test_must_fail git -c core.name=value config name
 '
 
+test_expect_success 'alias to give a configuration value' '
+	echo "foo and space " >foo &&
+	git diff HEAD >foo.patch &&
+	git checkout foo &&
+	git config alias.aw "-c apply.whitespace=fix apply" &&
+	git aw foo.patch &&
+	echo "foo and space" >expect &&
+	test_cmp expect foo
+'
+
 test_done
-- 
1.7.5.2.459.g67e41

^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2011-05-24 22:50 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-05-24 21:15 [PATCH] handle_options(): do not miscount how many arguments were used Junio C Hamano
2011-05-24 21:18 ` [PATCH] Allow built-ins to also use -c var=val via alias Junio C Hamano
2011-05-24 21:46   ` Jeff King
2011-05-24 21:52     ` Jeff King
2011-05-24 21:57       ` Jeff King
2011-05-24 22:49         ` Jeff King
2011-05-24 22:49           ` [PATCH 1/4] config: make environment parsing routines static Jeff King
2011-05-24 22:49           ` [PATCH 2/4] git_config: don't peek at global config_parameters Jeff King
2011-05-24 22:49           ` [PATCH 3/4] config: always parse GIT_CONFIG_PARAMETERS during git_config Jeff King
2011-05-24 22:50           ` [PATCH 4/4] handle_options(): do not miscount how many arguments were used Jeff King

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).