Skip to content

Commit db3572b

Browse files
authored
Merge pull request libgit2#6165 from libgit2/ethomson/includeif
config: handle empty conditional in includeIf
2 parents 0bf6723 + 715bfe1 commit db3572b

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/config_file.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -724,14 +724,25 @@ static const struct {
724724
static int parse_conditional_include(config_file_parse_data *parse_data, const char *section, const char *file)
725725
{
726726
char *condition;
727-
size_t i;
727+
size_t section_len, i;
728728
int error = 0, matches;
729729

730730
if (!parse_data->repo || !file)
731731
return 0;
732732

733-
condition = git__substrdup(section + strlen("includeIf."),
734-
strlen(section) - strlen("includeIf.") - strlen(".path"));
733+
section_len = strlen(section);
734+
735+
/*
736+
* We checked that the string starts with `includeIf.` and ends
737+
* in `.path` to get here. Make sure it consists of more.
738+
*/
739+
if (section_len < CONST_STRLEN("includeIf.") + CONST_STRLEN(".path"))
740+
return 0;
741+
742+
condition = git__substrdup(section + CONST_STRLEN("includeIf."),
743+
section_len - CONST_STRLEN("includeIf.") - CONST_STRLEN(".path"));
744+
745+
GIT_ERROR_CHECK_ALLOC(condition);
735746

736747
for (i = 0; i < ARRAY_SIZE(conditions); i++) {
737748
if (git__prefixcmp(condition, conditions[i].prefix))

tests/config/conditionals.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,28 @@ void test_config_conditionals__onbranch(void)
148148
assert_condition_includes("onbranch", "dir*", false);
149149
assert_condition_includes("onbranch", "dir/*", false);
150150
}
151+
152+
void test_config_conditionals__empty(void)
153+
{
154+
git_buf value = GIT_BUF_INIT;
155+
git_str buf = GIT_STR_INIT;
156+
git_config *cfg;
157+
158+
cl_git_pass(git_str_puts(&buf, "[includeIf]\n"));
159+
cl_git_pass(git_str_puts(&buf, "path = other\n"));
160+
161+
cl_git_mkfile("empty_standard_repo/.git/config", buf.ptr);
162+
cl_git_mkfile("empty_standard_repo/.git/other", "[foo]\nbar=baz\n");
163+
_repo = cl_git_sandbox_reopen();
164+
165+
git_str_dispose(&buf);
166+
167+
cl_git_pass(git_repository_config(&cfg, _repo));
168+
169+
cl_git_fail_with(GIT_ENOTFOUND,
170+
git_config_get_string_buf(&value, cfg, "foo.bar"));
171+
172+
git_str_dispose(&buf);
173+
git_buf_dispose(&value);
174+
git_config_free(cfg);
175+
}

0 commit comments

Comments
 (0)