Skip to content

Commit c846664

Browse files
tanayabhgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 32f5660 commit c846664

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

builtin/config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
599599
check_argc(argc, 2, 2);
600600
value = normalize_value(argv[0], argv[1]);
601601
return git_config_set_multivar_in_file(given_config_source.file,
602-
argv[0], value, "^$", 0);
602+
argv[0], value, "a^", 0);
603603
}
604604
else if (actions == ACTION_REPLACE_ALL) {
605605
check_write();

config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ static int matches(const char *key, const char *value)
12331233
return !strcmp(key, store.key) &&
12341234
(store.value_regex == NULL ||
12351235
(store.do_not_match ^
1236-
!regexec(store.value_regex, value, 0, NULL, 0)));
1236+
(value && !regexec(store.value_regex, value, 0, NULL, 0))));
12371237
}
12381238

12391239
static int store_aux(const char *key, const char *value, void *cb)

t/t1303-wacky-config.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,24 @@ test_expect_success 'unset many entries' '
111111
test_must_fail git config section.key
112112
'
113113

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+
114134
test_done

0 commit comments

Comments
 (0)