make config --add behave correctly for empty and NULL values

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Tanay Abhra committed Aug 18, 2014 at 03:17 UTC c8466645edd1413c7efed824f5bddac457eb77f9
3 files changed +22 -2
builtin/config.c
+1 -1
@@ -599,7 +599,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
599 check_argc(argc, 2, 2);
600 value = normalize_value(argv[0], argv[1]);
601 return git_config_set_multivar_in_file(given_config_source.file,
602 - argv[0], value, "^$", 0);
602 + argv[0], value, "a^", 0);
603 }
604 else if (actions == ACTION_REPLACE_ALL) {
605 check_write();
config.c
+1 -1
@@ -1233,7 +1233,7 @@ static int matches(const char *key, const char *value)
1233 return !strcmp(key, store.key) &&
1234 (store.value_regex == NULL ||
1235 (store.do_not_match ^
1236 - !regexec(store.value_regex, value, 0, NULL, 0)));
1236 + (value && !regexec(store.value_regex, value, 0, NULL, 0))));
1237 }
1238
1239 static int store_aux(const char *key, const char *value, void *cb)
t/t1303-wacky-config.sh
+20
@@ -111,4 +111,24 @@ test_expect_success 'unset many entries' '
111 test_must_fail git config section.key
112 '
113
114 +test_expect_success '--add appends new value after existing empty value' '
115 + cat >expect <<-\EOF &&
116 +
117 +
118 + fool
119 + roll
120 + EOF
121 + cp .git/config .git/config.old &&
122 + test_when_finished "mv .git/config.old .git/config" &&
123 + cat >.git/config <<-\EOF &&
124 + [foo]
125 + baz
126 + baz =
127 + baz = fool
128 + EOF
129 + git config --add foo.baz roll &&
130 + git config --get-all foo.baz >output &&
131 + test_cmp expect output
132 +'
133 +
134 test_done