Skip to content

Commit 43e7bf7

Browse files
authored
Merge pull request libgit2#4750 from nelhage/nelhage-config-no-section
config_file: Don't crash on options without a section
2 parents 227ace0 + 6698e05 commit 43e7bf7

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

src/config_file.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,8 +1035,15 @@ static int read_on_variable(
10351035
GIT_UNUSED(line);
10361036
GIT_UNUSED(line_len);
10371037

1038-
git_buf_puts(&buf, current_section);
1039-
git_buf_putc(&buf, '.');
1038+
if (current_section) {
1039+
/* TODO: Once warnings lang, we should likely warn
1040+
* here. Git appears to warn in most cases if it sees
1041+
* un-namespaced config options.
1042+
*/
1043+
git_buf_puts(&buf, current_section);
1044+
git_buf_putc(&buf, '.');
1045+
}
1046+
10401047
for (c = var_name; *c; c++)
10411048
git_buf_putc(&buf, git__tolower(*c));
10421049

tests/config/read.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,3 +758,36 @@ void test_config_read__bom(void)
758758
git_config_free(cfg);
759759
git_buf_dispose(&buf);
760760
}
761+
762+
static int read_nosection_cb(const git_config_entry *entry, void *payload) {
763+
int *seen = (int*)payload;
764+
if (strcmp(entry->name, "key") == 0) {
765+
(*seen)++;
766+
}
767+
return 0;
768+
}
769+
770+
/* This would ideally issue a warning, if we had a way to do so. */
771+
void test_config_read__nosection(void)
772+
{
773+
git_config *cfg;
774+
git_buf buf = GIT_BUF_INIT;
775+
int seen = 0;
776+
777+
cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config-nosection")));
778+
779+
/*
780+
* Given a key with no section, we do not allow reading it,
781+
* but we do include it in an iteration over the config
782+
* store. This appears to match how git's own APIs (and
783+
* git-config(1)) behave.
784+
*/
785+
786+
cl_git_fail_with(git_config_get_string_buf(&buf, cfg, "key"), GIT_EINVALIDSPEC);
787+
788+
cl_git_pass(git_config_foreach(cfg, read_nosection_cb, &seen));
789+
cl_assert_equal_i(seen, 1);
790+
791+
git_buf_dispose(&buf);
792+
git_config_free(cfg);
793+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
key = value

0 commit comments

Comments
 (0)