Skip to content

Commit 615d2de

Browse files
ttaylorrgitster
authored andcommitted
config.c: avoid segfault with --fixed-value and valueless config
When using `--fixed-value` with a key whose value is left empty (implied as being "true"), 'git config' may crash when invoked like either of: $ git config set --file=config --value=value --fixed-value \ section.key pattern $ git config --file=config --fixed-value section.key value pattern The original bugreport[1] bisects to 00bbdde (builtin/config: introduce "set" subcommand, 2024-05-06), which is a red-herring, since the original bugreport uses the new 'git config set' invocation. The behavior likely bisects back to c90702a (config: plumb --fixed-value into config API, 2020-11-25), which introduces the new --fixed-value option in the first place. Looking at the relevant frame from a failed process's coredump, the crash appears in config.c::matches() like so: (gdb) up #1 0x000055b3e8b06022 in matches (key=0x55b3ea894360 "section.key", value=0x0, store=0x7ffe99076eb0) at config.c:2884 2884 return !strcmp(store->fixed_value, value); where we are trying to compare the `--fixed-value` argument to `value`, which is NULL. Avoid attempting to match `--fixed-value` for configuration keys with no explicit value. A future patch could consider the empty value to mean "true", "yes", "on", etc. when invoked with `--type=bool`, but let's punt on that for now in the name of avoiding the segfault. [1]: https://lore.kernel.org/git/CANrWfmTek1xErBLrnoyhHN+gWU+rw14y6SQ+abZyzGoaBjmiKA@mail.gmail.com/ Reported-by: Han Jiang <jhcarl0814@gmail.com> Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent dbecc61 commit 615d2de

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

config.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2876,7 +2876,7 @@ static int matches(const char *key, const char *value,
28762876
{
28772877
if (strcmp(key, store->key))
28782878
return 0; /* not ours */
2879-
if (store->fixed_value)
2879+
if (store->fixed_value && value)
28802880
return !strcmp(store->fixed_value, value);
28812881
if (!store->value_pattern)
28822882
return 1; /* always matches */

t/t1300-config.sh

+9
Original file line numberDiff line numberDiff line change
@@ -2434,6 +2434,15 @@ test_expect_success '--get and --get-all with --fixed-value' '
24342434
test_must_fail git config --file=config --get-regexp --fixed-value fixed+ non-existent
24352435
'
24362436

2437+
test_expect_success '--fixed-value with value-less configuration' '
2438+
test_when_finished rm -f config &&
2439+
cat >config <<-\EOF &&
2440+
[section]
2441+
key
2442+
EOF
2443+
git config --file=config --fixed-value section.key value pattern
2444+
'
2445+
24372446
test_expect_success 'includeIf.hasconfig:remote.*.url' '
24382447
git init hasremoteurlTest &&
24392448
test_when_finished "rm -rf hasremoteurlTest" &&

0 commit comments

Comments
 (0)