All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] make config --add behave correctly for empty and NULL values
@ 2014-08-18 10:17 Tanay Abhra
  2014-08-18 12:33 ` Matthieu Moy
  2014-08-18 18:18 ` Junio C Hamano
  0 siblings, 2 replies; 12+ messages in thread
From: Tanay Abhra @ 2014-08-18 10:17 UTC (permalink / raw)
  To: git; +Cc: Tanay Abhra, Ramkumar Ramachandra, Matthieu Moy

Currently if we have a config file like,
[foo]
        baz
        bar =

and we try something like, "git config --add foo.baz roll", Git will
segfault. Moreover, for "git config --add foo.bar roll", it will
overwrite the original value instead of appending after the existing
empty value.

The problem lies with the regexp used for simulating --add in
`git_config_set_multivar_in_file()`, "^$", which in ideal case should
not match with any string but is true for empty strings. Instead use a
regexp like "a^" which can not be true for any string, empty or not.

For removing the segfault add a check for NULL values in `matches()` in
config.c.

Signed-off-by: Tanay Abhra <tanayabh@gmail.com>
---
 builtin/config.c        |  2 +-
 config.c                |  2 +-
 t/t1303-wacky-config.sh | 20 ++++++++++++++++++++
 3 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/builtin/config.c b/builtin/config.c
index fcd8474..b9e7dce 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -586,7 +586,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 		check_argc(argc, 2, 2);
 		value = normalize_value(argv[0], argv[1]);
 		return git_config_set_multivar_in_file(given_config_source.file,
-						       argv[0], value, "^$", 0);
+						       argv[0], value, "a^", 0);
 	}
 	else if (actions == ACTION_REPLACE_ALL) {
 		check_write();
diff --git a/config.c b/config.c
index 058505c..67a7729 100644
--- a/config.c
+++ b/config.c
@@ -1231,7 +1231,7 @@ static int matches(const char *key, const char *value)
 	return !strcmp(key, store.key) &&
 		(store.value_regex == NULL ||
 		 (store.do_not_match ^
-		  !regexec(store.value_regex, value, 0, NULL, 0)));
+		  (value && !regexec(store.value_regex, value, 0, NULL, 0))));
 }
 
 static int store_aux(const char *key, const char *value, void *cb)
diff --git a/t/t1303-wacky-config.sh b/t/t1303-wacky-config.sh
index 3a2c819..3b92083 100755
--- a/t/t1303-wacky-config.sh
+++ b/t/t1303-wacky-config.sh
@@ -111,4 +111,24 @@ test_expect_success 'unset many entries' '
 	test_must_fail git config section.key
 '
 
+test_expect_success '--add appends new value after existing empty value' '
+	cat >expect <<-\EOF &&
+
+
+	fool
+	roll
+	EOF
+	cp .git/config .git/config.old &&
+	test_when_finished "mv .git/config.old .git/config" &&
+	cat >.git/config <<-\EOF &&
+	[foo]
+		baz
+		baz =
+		baz = fool
+	EOF
+	git config --add foo.baz roll &&
+	git config --get-all foo.baz >output &&
+	test_cmp expect output
+'
+
 test_done
-- 
1.9.0.GIT

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

* Re: [PATCH] make config --add behave correctly for empty and NULL values
  2014-08-18 10:17 [PATCH] make config --add behave correctly for empty and NULL values Tanay Abhra
@ 2014-08-18 12:33 ` Matthieu Moy
  2014-08-18 18:18 ` Junio C Hamano
  1 sibling, 0 replies; 12+ messages in thread
From: Matthieu Moy @ 2014-08-18 12:33 UTC (permalink / raw)
  To: Tanay Abhra; +Cc: git, Ramkumar Ramachandra

Tanay Abhra <tanayabh@gmail.com> writes:

> Currently if we have a config file like,
> [foo]
>         baz
>         bar =
>
> and we try something like, "git config --add foo.baz roll", Git will
> segfault. Moreover, for "git config --add foo.bar roll", it will
> overwrite the original value instead of appending after the existing
> empty value.
>
> The problem lies with the regexp used for simulating --add in
> `git_config_set_multivar_in_file()`, "^$", which in ideal case should
> not match with any string but is true for empty strings. Instead use a
> regexp like "a^" which can not be true for any string, empty or not.
>
> For removing the segfault add a check for NULL values in `matches()` in
> config.c.

I would have prefered two separate patches (or even better, 3, the first
one being "demonstrate failure of ..." with test_expect_failure) for
each issues.

But the fixes are straightforward, and the test actually test what it
has to test, so I think we can keep the patch as-is.

Thanks,

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

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

* Re: [PATCH] make config --add behave correctly for empty and NULL values
  2014-08-18 10:17 [PATCH] make config --add behave correctly for empty and NULL values Tanay Abhra
  2014-08-18 12:33 ` Matthieu Moy
@ 2014-08-18 18:18 ` Junio C Hamano
  2014-08-19  5:17   ` Jeff King
  1 sibling, 1 reply; 12+ messages in thread
From: Junio C Hamano @ 2014-08-18 18:18 UTC (permalink / raw)
  To: Tanay Abhra; +Cc: git, Ramkumar Ramachandra, Matthieu Moy

Tanay Abhra <tanayabh@gmail.com> writes:

> Currently if we have a config file like,
> [foo]
>         baz
>         bar =
>
> and we try something like, "git config --add foo.baz roll", Git will
> segfault.

Thanks; this is a good find.

This is a tangent, but people please stop starting their sentence
with a somewhat irritating "Currently"; it does not help both
current and future readers very much without some mention of version
numbers.

I suspect this bug dates back to pretty much day one of "git config"
(dates at least back to 1.5.3).

> The problem lies with the regexp used for simulating --add in
> `git_config_set_multivar_in_file()`, "^$", which in ideal case should
> not match with any string but is true for empty strings. Instead use a
> regexp like "a^" which can not be true for any string, empty or not.

Yuck, but we cannot pass NULL or some other special value that look
more meaningful to signal the fact that we do not want to match
anything, so this seems to be the easiest way out.  

Are we sure that "a^", which cannot be true for any string, will not
be caught by anybody's regcomp() as an error?  I know regcomp()
accepts the expression and regexec() fails to match with GNU libc,
but that is not the whole of the world.

At least, please make it clear for those who read this code later
what is going on with this magic "a^", perhaps with

	#define REGEXP_THAT_NEVER_MATCHES "a^"
	...
        return git_config_set_multivar_in_file(given_config_source.file,
                                              argv[0], value,
                                              REGEXP_THAT_NEVER_MATCHES, 0);
and/or with in-code comment.

	/*
         * set_multivar_in_file() removes existing values that match
         * the value_regexp argument and then adds this new value;
         * pass a pattern that never matches anything, as we do not
         * want to remove any existing value.
         */
	return git_config_set_multivar_in_file(given_config_source.file,
                                              argv[0], value,
                                              REGEXP_THAT_NEVER_MATCHES, 0);

To be honest, I'd rather see this done "right", by giving an option
to the caller to tell the function not to call regcomp/regexec in
matches().

 * Define a global exported via cache.h and defined in config.c

	extern const char CONFIG_SET_MULTIVAR_NO_REPLACE[];

   and pass it from this calling site, instead of an arbitrary
   literal string e.g. "a^"

 * Add a bit to the "store" struct, e.g. "unsigned value_never_matches:1";

 * In git_config_set_multivar_in_file() implementation, check for
   this constant address and set store.value_never_matches to true;

 * in matches(), check this bit and always return "No, this existing
   value do not match" when it is set.

or something like that.

> For removing the segfault add a check for NULL values in `matches()` in
> config.c.

The fact that you do a check is important, but it equally if not
more important what you do with the result.  "Check for a NULL and
consider it as not matching" is probably what you meant, but I'd
like to double check.

> Signed-off-by: Tanay Abhra <tanayabh@gmail.com>
> ---
>  builtin/config.c        |  2 +-
>  config.c                |  2 +-
>  t/t1303-wacky-config.sh | 20 ++++++++++++++++++++
>  3 files changed, 22 insertions(+), 2 deletions(-)
>
> diff --git a/builtin/config.c b/builtin/config.c
> index fcd8474..b9e7dce 100644
> --- a/builtin/config.c
> +++ b/builtin/config.c
> @@ -586,7 +586,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
>  		check_argc(argc, 2, 2);
>  		value = normalize_value(argv[0], argv[1]);
>  		return git_config_set_multivar_in_file(given_config_source.file,
> -						       argv[0], value, "^$", 0);
> +						       argv[0], value, "a^", 0);
>  	}
>  	else if (actions == ACTION_REPLACE_ALL) {
>  		check_write();
> diff --git a/config.c b/config.c
> index 058505c..67a7729 100644
> --- a/config.c
> +++ b/config.c
> @@ -1231,7 +1231,7 @@ static int matches(const char *key, const char *value)
>  	return !strcmp(key, store.key) &&
>  		(store.value_regex == NULL ||
>  		 (store.do_not_match ^
> -		  !regexec(store.value_regex, value, 0, NULL, 0)));
> +		  (value && !regexec(store.value_regex, value, 0, NULL, 0))));
>  }
>  
>  static int store_aux(const char *key, const char *value, void *cb)
> diff --git a/t/t1303-wacky-config.sh b/t/t1303-wacky-config.sh
> index 3a2c819..3b92083 100755
> --- a/t/t1303-wacky-config.sh
> +++ b/t/t1303-wacky-config.sh
> @@ -111,4 +111,24 @@ test_expect_success 'unset many entries' '
>  	test_must_fail git config section.key
>  '
>  
> +test_expect_success '--add appends new value after existing empty value' '
> +	cat >expect <<-\EOF &&
> +
> +
> +	fool
> +	roll
> +	EOF
> +	cp .git/config .git/config.old &&
> +	test_when_finished "mv .git/config.old .git/config" &&
> +	cat >.git/config <<-\EOF &&
> +	[foo]
> +		baz
> +		baz =
> +		baz = fool
> +	EOF
> +	git config --add foo.baz roll &&
> +	git config --get-all foo.baz >output &&
> +	test_cmp expect output
> +'
> +
>  test_done

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

* Re: [PATCH] make config --add behave correctly for empty and NULL values
  2014-08-18 18:18 ` Junio C Hamano
@ 2014-08-19  5:17   ` Jeff King
  2014-08-19  6:03     ` Junio C Hamano
  0 siblings, 1 reply; 12+ messages in thread
From: Jeff King @ 2014-08-19  5:17 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Tanay Abhra, git, Ramkumar Ramachandra, Matthieu Moy

On Mon, Aug 18, 2014 at 11:18:52AM -0700, Junio C Hamano wrote:

> Are we sure that "a^", which cannot be true for any string, will not
> be caught by anybody's regcomp() as an error?  I know regcomp()
> accepts the expression and regexec() fails to match with GNU libc,
> but that is not the whole of the world.

We do support negation (ourselves) in the regexp, so "!$foo" would work,
where "$foo" is some regexp that always matches. But that may be digging
ourselves the opposite hole, trying to find a pattern that reliably
matches everything.

> To be honest, I'd rather see this done "right", by giving an option
> to the caller to tell the function not to call regcomp/regexec in
> matches().

Yeah, that was my first thought, too on seeing the patch. I even worked
up an example before reading your message, but:

>  * Define a global exported via cache.h and defined in config.c
> 
> 	extern const char CONFIG_SET_MULTIVAR_NO_REPLACE[];
> 
>    and pass it from this calling site, instead of an arbitrary
>    literal string e.g. "a^"
> 
>  * Add a bit to the "store" struct, e.g. "unsigned value_never_matches:1";
> 
>  * In git_config_set_multivar_in_file() implementation, check for
>    this constant address and set store.value_never_matches to true;
> 
>  * in matches(), check this bit and always return "No, this existing
>    value do not match" when it is set.

I just used

  #define CONFIG_REGEX_NONE ((void *)1)

as my magic sentinel value, both for the string and compiled regex
versions. Adding a bit to the store struct is a lot less disgusting and
error-prone. So I won't share mine here. :)

-Peff

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

* Re: [PATCH] make config --add behave correctly for empty and NULL values
  2014-08-19  5:17   ` Jeff King
@ 2014-08-19  6:03     ` Junio C Hamano
  2014-08-19  6:20       ` Jeff King
  0 siblings, 1 reply; 12+ messages in thread
From: Junio C Hamano @ 2014-08-19  6:03 UTC (permalink / raw)
  To: Jeff King; +Cc: Tanay Abhra, git, Ramkumar Ramachandra, Matthieu Moy

Jeff King <peff@peff.net> writes:

> I just used
>
>   #define CONFIG_REGEX_NONE ((void *)1)
>
> as my magic sentinel value, both for the string and compiled regex
> versions. Adding a bit to the store struct is a lot less disgusting and
> error-prone. So I won't share mine here. :)

Actually, I wrote something like that aloud but did not type it out
;-).  Great minds think alike.

We already have some code paths that use ((void *)1) as a special
pointer value, so in that sense I would say it is not the end of the
world if you added a new one.  At the end-user level (i.e. people
who write callers to set-multivar-in-file function), I actually like
your idea of inventing our own string syntax and parse it at the
place where we strip '!' out and remember that the pattern's match
status needs to be negated.  For example, instead of "a^" (to which
I cannot say with confidence that no implementation would match the
not-at-the-beginning caret literally), I would not mind if we taught
set-multivar-in-file that we use "!*" as a mark to tell "this
pattern never matches", and have it assign your "never matches"
mark, i.e. (void *)1, to store.value_regex.  Then matches() would
become

	static int matches(const char *key, const char *value)
        {
        	if (strcmp(key, store.key))
                	return 0; /* not ours */
		if (!store.value_regex)
                	return 1; /* always matches */
		if (store.value_regex == CONFIG_REGEX_NONE)
                	return 0; /* never matches */
		return store.do_not_match ^
                	!regexec(store.value_regex, value, 0, NULL, 0);
	}

or something like that, and the ugly "magic" will be localized,
which may make it more palatable.

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

* Re: [PATCH] make config --add behave correctly for empty and NULL values
  2014-08-19  6:03     ` Junio C Hamano
@ 2014-08-19  6:20       ` Jeff King
  2014-09-11 23:35         ` Junio C Hamano
  0 siblings, 1 reply; 12+ messages in thread
From: Jeff King @ 2014-08-19  6:20 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Tanay Abhra, git, Ramkumar Ramachandra, Matthieu Moy

On Mon, Aug 18, 2014 at 11:03:51PM -0700, Junio C Hamano wrote:

> We already have some code paths that use ((void *)1) as a special
> pointer value, so in that sense I would say it is not the end of the
> world if you added a new one.

No, but if you use it to replace the regexp, you end up having to check
for it in the code paths that regfree() the result. I think a separate
bit is nicer for that reason.

> At the end-user level (i.e. people who write callers to
> set-multivar-in-file function), I actually like your idea of inventing
> our own string syntax and parse it at the place where we strip '!' out
> and remember that the pattern's match status needs to be negated.

I thought at first that "!" by itself might be fine, but that is
actually a valid regex. And we may feed user input directly to the
function, so we have to be careful not to get too clever.

> For example, instead of "a^" (to which
> I cannot say with confidence that no implementation would match the
> not-at-the-beginning caret literally), I would not mind if we taught
> set-multivar-in-file that we use "!*" as a mark to tell "this
> pattern never matches",

That would work, but I think CONFIG_REGEX_NONE or similar is a bit less
cryptic, and not any harder to implement.

Here is the patch I wrote, for reference (I also think breaking the
"matches" function into a series of conditionals, as you showed, is way
more readable):

diff --git a/builtin/config.c b/builtin/config.c
index b9e7dce..7bba516 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -586,7 +586,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 		check_argc(argc, 2, 2);
 		value = normalize_value(argv[0], argv[1]);
 		return git_config_set_multivar_in_file(given_config_source.file,
-						       argv[0], value, "a^", 0);
+						       argv[0], value,
+						       CONFIG_REGEX_NONE, 0);
 	}
 	else if (actions == ACTION_REPLACE_ALL) {
 		check_write();
diff --git a/cache.h b/cache.h
index fcb511d..dcf3a2a 100644
--- a/cache.h
+++ b/cache.h
@@ -1281,6 +1281,8 @@ extern int update_server_info(int);
 #define CONFIG_INVALID_PATTERN 6
 #define CONFIG_GENERIC_ERROR 7
 
+#define CONFIG_REGEX_NONE ((void *)1)
+
 struct git_config_source {
 	unsigned int use_stdin:1;
 	const char *file;
diff --git a/config.c b/config.c
index 67a7729..1199faf 100644
--- a/config.c
+++ b/config.c
@@ -1229,6 +1229,7 @@ static struct {
 static int matches(const char *key, const char *value)
 {
 	return !strcmp(key, store.key) &&
+		store.value_regex != CONFIG_REGEX_NONE &&
 		(store.value_regex == NULL ||
 		 (store.do_not_match ^
 		  (value && !regexec(store.value_regex, value, 0, NULL, 0))));
@@ -1493,6 +1494,8 @@ out_free_ret_1:
 /*
  * If value==NULL, unset in (remove from) config,
  * if value_regex!=NULL, disregard key/value pairs where value does not match.
+ * if value_regex==CONFIG_REGEX_NONE, do not match any existing values
+ *     (only add a new one)
  * if multi_replace==0, nothing, or only one matching key/value is replaced,
  *     else all matching key/values (regardless how many) are removed,
  *     before the new pair is written.
@@ -1576,6 +1579,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
 
 		if (value_regex == NULL)
 			store.value_regex = NULL;
+		else if (value_regex == CONFIG_REGEX_NONE)
+			store.value_regex = CONFIG_REGEX_NONE;
 		else {
 			if (value_regex[0] == '!') {
 				store.do_not_match = 1;
@@ -1607,7 +1612,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
 		if (git_config_from_file(store_aux, config_filename, NULL)) {
 			error("invalid config file %s", config_filename);
 			free(store.key);
-			if (store.value_regex != NULL) {
+			if (store.value_regex != NULL &&
+			    store.value_regex != CONFIG_REGEX_NONE) {
 				regfree(store.value_regex);
 				free(store.value_regex);
 			}
@@ -1616,7 +1622,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
 		}
 
 		free(store.key);
-		if (store.value_regex != NULL) {
+		if (store.value_regex != NULL &&
+		    store.value_regex != CONFIG_REGEX_NONE) {
 			regfree(store.value_regex);
 			free(store.value_regex);
 		}

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

* Re: [PATCH] make config --add behave correctly for empty and NULL values
  2014-08-19  6:20       ` Jeff King
@ 2014-09-11 23:35         ` Junio C Hamano
  2014-09-12  2:29           ` Jeff King
  2014-09-12  7:23           ` [PATCH v2 1/2] document irregular config --add behaviour " Tanay Abhra
  0 siblings, 2 replies; 12+ messages in thread
From: Junio C Hamano @ 2014-09-11 23:35 UTC (permalink / raw)
  To: Jeff King; +Cc: Tanay Abhra, git, Ramkumar Ramachandra, Matthieu Moy

Jeff King <peff@peff.net> writes:

> Here is the patch I wrote, for reference (I also think breaking the
> "matches" function into a series of conditionals, as you showed, is way
> more readable):

OK, while reviewing the today's issue of "What's cooking" and making
topics graduate to 'master', I got annoyed that the bottom of jch
branch still needed to be kept.  Let's do this.

-- >8 --
From: Jeff King <peff@peff.net>
Date: Tue, 19 Aug 2014 02:20:00 -0400
Subject: [PATCH] config: avoid a funny sentinel value "a^"

Introduce CONFIG_REGEX_NONE as a more explicit sentinel value to say
"we do not want to replace any existing entry" and use it in the
implementation of "git config --add".

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/config.c |  3 ++-
 cache.h          |  2 ++
 config.c         | 23 +++++++++++++++++------
 3 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/builtin/config.c b/builtin/config.c
index 8224699..bf1aa6b 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -599,7 +599,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 		check_argc(argc, 2, 2);
 		value = normalize_value(argv[0], argv[1]);
 		return git_config_set_multivar_in_file(given_config_source.file,
-						       argv[0], value, "a^", 0);
+						       argv[0], value,
+						       CONFIG_REGEX_NONE, 0);
 	}
 	else if (actions == ACTION_REPLACE_ALL) {
 		check_write();
diff --git a/cache.h b/cache.h
index c708062..8356168 100644
--- a/cache.h
+++ b/cache.h
@@ -1233,6 +1233,8 @@ extern int update_server_info(int);
 #define CONFIG_INVALID_PATTERN 6
 #define CONFIG_GENERIC_ERROR 7
 
+#define CONFIG_REGEX_NONE ((void *)1)
+
 struct git_config_source {
 	unsigned int use_stdin:1;
 	const char *file;
diff --git a/config.c b/config.c
index ffe0104..2e709bf 100644
--- a/config.c
+++ b/config.c
@@ -1230,10 +1230,15 @@ static struct {
 
 static int matches(const char *key, const char *value)
 {
-	return !strcmp(key, store.key) &&
-		(store.value_regex == NULL ||
-		 (store.do_not_match ^
-		  (value && !regexec(store.value_regex, value, 0, NULL, 0))));
+	if (strcmp(key, store.key))
+		return 0; /* not ours */
+	if (!store.value_regex)
+		return 1; /* always matches */
+	if (store.value_regex == CONFIG_REGEX_NONE)
+		return 0; /* never matches */
+
+	return store.do_not_match ^
+		(value && !regexec(store.value_regex, value, 0, NULL, 0));
 }
 
 static int store_aux(const char *key, const char *value, void *cb)
@@ -1495,6 +1500,8 @@ out_free_ret_1:
 /*
  * If value==NULL, unset in (remove from) config,
  * if value_regex!=NULL, disregard key/value pairs where value does not match.
+ * if value_regex==CONFIG_REGEX_NONE, do not match any existing values
+ *     (only add a new one)
  * if multi_replace==0, nothing, or only one matching key/value is replaced,
  *     else all matching key/values (regardless how many) are removed,
  *     before the new pair is written.
@@ -1578,6 +1585,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
 
 		if (value_regex == NULL)
 			store.value_regex = NULL;
+		else if (value_regex == CONFIG_REGEX_NONE)
+			store.value_regex = CONFIG_REGEX_NONE;
 		else {
 			if (value_regex[0] == '!') {
 				store.do_not_match = 1;
@@ -1609,7 +1618,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
 		if (git_config_from_file(store_aux, config_filename, NULL)) {
 			error("invalid config file %s", config_filename);
 			free(store.key);
-			if (store.value_regex != NULL) {
+			if (store.value_regex != NULL &&
+			    store.value_regex != CONFIG_REGEX_NONE) {
 				regfree(store.value_regex);
 				free(store.value_regex);
 			}
@@ -1618,7 +1628,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
 		}
 
 		free(store.key);
-		if (store.value_regex != NULL) {
+		if (store.value_regex != NULL &&
+		    store.value_regex != CONFIG_REGEX_NONE) {
 			regfree(store.value_regex);
 			free(store.value_regex);
 		}
-- 
2.1.0-466-g6597b3e

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

* Re: [PATCH] make config --add behave correctly for empty and NULL values
  2014-09-11 23:35         ` Junio C Hamano
@ 2014-09-12  2:29           ` Jeff King
  2014-09-12  7:23           ` [PATCH v2 1/2] document irregular config --add behaviour " Tanay Abhra
  1 sibling, 0 replies; 12+ messages in thread
From: Jeff King @ 2014-09-12  2:29 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Tanay Abhra, git, Ramkumar Ramachandra, Matthieu Moy

On Thu, Sep 11, 2014 at 04:35:33PM -0700, Junio C Hamano wrote:

> Jeff King <peff@peff.net> writes:
> 
> > Here is the patch I wrote, for reference (I also think breaking the
> > "matches" function into a series of conditionals, as you showed, is way
> > more readable):
> 
> OK, while reviewing the today's issue of "What's cooking" and making
> topics graduate to 'master', I got annoyed that the bottom of jch
> branch still needed to be kept.  Let's do this.
> 
> -- >8 --
> From: Jeff King <peff@peff.net>
> Date: Tue, 19 Aug 2014 02:20:00 -0400
> Subject: [PATCH] config: avoid a funny sentinel value "a^"
> 
> Introduce CONFIG_REGEX_NONE as a more explicit sentinel value to say
> "we do not want to replace any existing entry" and use it in the
> implementation of "git config --add".
> 
> Signed-off-by: Jeff King <peff@peff.net>
> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Looks good, and adding my signoff is fine. Thanks.

-Peff

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

* [PATCH v2 1/2] document irregular config --add behaviour for empty and NULL values
  2014-09-11 23:35         ` Junio C Hamano
  2014-09-12  2:29           ` Jeff King
@ 2014-09-12  7:23           ` Tanay Abhra
  2014-09-12  7:25             ` [PATCH v2 2/2] make config --add behave correctly " Tanay Abhra
  1 sibling, 1 reply; 12+ messages in thread
From: Tanay Abhra @ 2014-09-12  7:23 UTC (permalink / raw)
  To: Junio C Hamano, Jeff King; +Cc: git, Ramkumar Ramachandra, Matthieu Moy

If we have a config file like,
[foo]
        baz
        bar =

and we try something like, "git config --add foo.baz roll", Git will
segfault. Moreover, for "git config --add foo.bar roll", it will
overwrite the original value instead of appending after the existing
empty value. Document these deficiencies in form of a test.

Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Tanay Abhra <tanayabh@gmail.com>
---

Sorry for this very late reply. I was stuck in a flood affected region
with no internet connectivity for the past week. I am safe now. :)

FWIW, here is the reroll with a set bit in the store struct and an exported
global. I could have done the reroll as you have done, but Jeff had mentioned
that he liked the version with a bit flag more. But you can choose the version
that seems better to you.

 t/t1303-wacky-config.sh | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/t/t1303-wacky-config.sh b/t/t1303-wacky-config.sh
index 3a2c819..e5c0f07 100755
--- a/t/t1303-wacky-config.sh
+++ b/t/t1303-wacky-config.sh
@@ -111,4 +111,24 @@ test_expect_success 'unset many entries' '
 	test_must_fail git config section.key
 '

+test_expect_failure '--add appends new value after existing empty value' '
+	cat >expect <<-\EOF &&
+
+
+	fool
+	roll
+	EOF
+	cp .git/config .git/config.old &&
+	test_when_finished "mv .git/config.old .git/config" &&
+	cat >.git/config <<-\EOF &&
+	[foo]
+		baz
+		baz =
+		baz = fool
+	EOF
+	git config --add foo.baz roll &&
+	git config --get-all foo.baz >output &&
+	test_cmp expect output
+'
+
 test_done
-- 
1.9.0.GIT

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

* [PATCH v2 2/2] make config --add behave correctly for empty and NULL values
  2014-09-12  7:23           ` [PATCH v2 1/2] document irregular config --add behaviour " Tanay Abhra
@ 2014-09-12  7:25             ` Tanay Abhra
  2014-09-12  8:15               ` Matthieu Moy
  0 siblings, 1 reply; 12+ messages in thread
From: Tanay Abhra @ 2014-09-12  7:25 UTC (permalink / raw)
  To: Junio C Hamano, Jeff King; +Cc: git, Ramkumar Ramachandra, Matthieu Moy

The problem lies with the regexp used for simulating --add in
`git_config_set_multivar_in_file()`, "^$", which in ideal case should
not match with any string but is true for empty strings. Instead use a
sentinel value CONFIG_REGEX_NONE to say "we do not want to replace any
existing entry" and use it in the implementation of "git config --add".

For removing the segfault add a check for NULL values in `matches()` in
config.c.

Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Tanay Abhra <tanayabh@gmail.com>
---
 builtin/config.c        |  2 +-
 cache.h                 |  2 ++
 config.c                | 21 +++++++++++++++++----
 t/t1303-wacky-config.sh |  2 +-
 4 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/builtin/config.c b/builtin/config.c
index aba7135..195664b 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -611,7 +611,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 		check_argc(argc, 2, 2);
 		value = normalize_value(argv[0], argv[1]);
 		return git_config_set_multivar_in_file(given_config_source.file,
-						       argv[0], value, "^$", 0);
+						       argv[0], value, CONFIG_REGEX_NONE, 0);
 	}
 	else if (actions == ACTION_REPLACE_ALL) {
 		check_write();
diff --git a/cache.h b/cache.h
index dfa1a56..a09217d 100644
--- a/cache.h
+++ b/cache.h
@@ -1284,6 +1284,8 @@ extern int update_server_info(int);
 #define CONFIG_INVALID_PATTERN 6
 #define CONFIG_GENERIC_ERROR 7

+extern const char CONFIG_REGEX_NONE[];
+
 struct git_config_source {
 	unsigned int use_stdin:1;
 	const char *file;
diff --git a/config.c b/config.c
index 83c913a..20476e0 100644
--- a/config.c
+++ b/config.c
@@ -46,6 +46,8 @@ static int zlib_compression_seen;
  */
 static struct config_set the_config_set;

+const char CONFIG_REGEX_NONE[] = "a^";
+
 static int config_file_fgetc(struct config_source *conf)
 {
 	return fgetc(conf->u.file);
@@ -1607,14 +1609,20 @@ static struct {
 	unsigned int offset_alloc;
 	enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
 	int seen;
+	unsigned value_never_matches:1;
 } store;

 static int matches(const char *key, const char *value)
 {
-	return !strcmp(key, store.key) &&
-		(store.value_regex == NULL ||
-		 (store.do_not_match ^
-		  !regexec(store.value_regex, value, 0, NULL, 0)));
+	if (strcmp(key, store.key))
+		return 0; /* not ours */
+	if (!store.value_regex)
+		return 1; /* always matches */
+	if (store.value_never_matches)
+		return 0; /* never matches */
+
+	return store.do_not_match ^
+	(value && !regexec(store.value_regex, value, 0, NULL, 0));
 }

 static int store_aux(const char *key, const char *value, void *cb)
@@ -1876,6 +1884,8 @@ out_free_ret_1:
 /*
  * If value==NULL, unset in (remove from) config,
  * if value_regex!=NULL, disregard key/value pairs where value does not match.
+ * if value_regex==CONFIG_REGEX_NONE, do not match any existing values
+ * (only add a new one)
  * if multi_replace==0, nothing, or only one matching key/value is replaced,
  *     else all matching key/values (regardless how many) are removed,
  *     before the new pair is written.
@@ -1966,6 +1976,9 @@ int git_config_set_multivar_in_file(const char *config_filename,
 			} else
 				store.do_not_match = 0;

+			if (value_regex == CONFIG_REGEX_NONE)
+				store.value_never_matches = 1;
+
 			store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
 			if (regcomp(store.value_regex, value_regex,
 					REG_EXTENDED)) {
diff --git a/t/t1303-wacky-config.sh b/t/t1303-wacky-config.sh
index e5c0f07..3b92083 100755
--- a/t/t1303-wacky-config.sh
+++ b/t/t1303-wacky-config.sh
@@ -111,7 +111,7 @@ test_expect_success 'unset many entries' '
 	test_must_fail git config section.key
 '

-test_expect_failure '--add appends new value after existing empty value' '
+test_expect_success '--add appends new value after existing empty value' '
 	cat >expect <<-\EOF &&


-- 
1.9.0.GIT

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

* Re: [PATCH v2 2/2] make config --add behave correctly for empty and NULL values
  2014-09-12  7:25             ` [PATCH v2 2/2] make config --add behave correctly " Tanay Abhra
@ 2014-09-12  8:15               ` Matthieu Moy
  2014-09-12 17:29                 ` Junio C Hamano
  0 siblings, 1 reply; 12+ messages in thread
From: Matthieu Moy @ 2014-09-12  8:15 UTC (permalink / raw)
  To: Tanay Abhra; +Cc: Junio C Hamano, Jeff King, git, Ramkumar Ramachandra

Tanay Abhra <tanayabh@gmail.com> writes:

> +const char CONFIG_REGEX_NONE[] = "a^";

I have a slight preference for this version (no magic (void *)1 value,
and belts-and-suspenders solution since someone actually using the regex
should still get a correct behavior.

But I'm fine with both Junio/Peff's version or this one.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

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

* Re: [PATCH v2 2/2] make config --add behave correctly for empty and NULL values
  2014-09-12  8:15               ` Matthieu Moy
@ 2014-09-12 17:29                 ` Junio C Hamano
  0 siblings, 0 replies; 12+ messages in thread
From: Junio C Hamano @ 2014-09-12 17:29 UTC (permalink / raw)
  To: Matthieu Moy; +Cc: Tanay Abhra, Jeff King, git, Ramkumar Ramachandra

Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:

> Tanay Abhra <tanayabh@gmail.com> writes:
>
>> +const char CONFIG_REGEX_NONE[] = "a^";
>
> I have a slight preference for this version (no magic (void *)1 value,
> and belts-and-suspenders solution since someone actually using the regex
> should still get a correct behavior.
>
> But I'm fine with both Junio/Peff's version or this one.

I do not care too deeply either way, to be honest.

But if we were to redo this in the right way, I suspect that the
best solution may be to correct the root cause, which is the design
mistake in the git_config_set_multivar_in_file API.  The function
takes a regexp (possibly NULL) and a multi_replace bit, and with
that expresses these three combinations:

    - a non-NULL regexp means only the existing ones that match are
      subject to replacement;

    - NULL regexp means all of the existing ones that match are
      subject to replacement;

    - multi-replace bit controls which ones among the replacement
      candidates are replaced (either the first one or all).

But we actually want to express three, not two, different handling
for the existing entries.  Either (1) use the regexp to decide which
ones are subject to replacement, (2) declare all of them are subject
to replacement, or (3) declare none of them are to be replaced.  The
last one cannot be expressed without coming up with a trick to say
"I am giving a regexp that hopefully will not match anything as a
workaround because otherwise you will replace all of them but what I
really want to say is I do not want you to replace anything", and
this thread discusses a fix to the bug in the implementation that
failed to come up with a "hopefully will not match anything"
pattern.  And we are still discussing to fix a better workaround.

Instead of polishing the workaround, wouldn't it be better to make
it unnecessary to work it around?  For exaple, we could turn the
last parameter to the function into an "unsigned flag" with two
bits, CONFIG_SET_USE_REGEXP_TO_FILTER (if set, use the regexp to
filter which of the existing entries to be replaced) and
CONFIG_SET_REPLACE_MULTI (if set, replace all the eligible ones),
and the result would be conceptually a lot cleaner, no?

Some notes:

 - Because most callers expect "replace" behaviour, instead of
   adding CONFIG_SET_USE_REGEXP_TO_FILTER to the majority of
   existing callers, a new flag CONFIG_SET_JUST_APPEND (which is
   exactly the negation of the USE_REGEXP_TO_FILTER) would be a more
   practical thing to introduce.

 - We can keep using value_regexp==NULL (under !JUST_APPEND) to mean
   value_regexp=".*", i.e. matches anything, as a short-hand.

Hmm?

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

end of thread, other threads:[~2014-09-12 17:29 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-18 10:17 [PATCH] make config --add behave correctly for empty and NULL values Tanay Abhra
2014-08-18 12:33 ` Matthieu Moy
2014-08-18 18:18 ` Junio C Hamano
2014-08-19  5:17   ` Jeff King
2014-08-19  6:03     ` Junio C Hamano
2014-08-19  6:20       ` Jeff King
2014-09-11 23:35         ` Junio C Hamano
2014-09-12  2:29           ` Jeff King
2014-09-12  7:23           ` [PATCH v2 1/2] document irregular config --add behaviour " Tanay Abhra
2014-09-12  7:25             ` [PATCH v2 2/2] make config --add behave correctly " Tanay Abhra
2014-09-12  8:15               ` Matthieu Moy
2014-09-12 17:29                 ` Junio C Hamano

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.