Skip to content

Commit 1bbdf15

Browse files
authored
Merge pull request libgit2#5527 from libgit2/ethomson/config_unreadable
Handle unreadable configuration files
2 parents 89ddd0f + 9df6922 commit 1bbdf15

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/config_file.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ static int config_file_open(git_config_backend *cfg, git_config_level_t level, c
111111
if (!git_path_exists(b->file.path))
112112
return 0;
113113

114+
/*
115+
* git silently ignores configuration files that are not
116+
* readable. We emulate that behavior. This is particularly
117+
* important for sandboxed applications on macOS where the
118+
* git configuration files may not be readable.
119+
*/
120+
if (p_access(b->file.path, R_OK) < 0)
121+
return GIT_ENOTFOUND;
122+
114123
if (res < 0 || (res = config_file_read(b->entries, repo, &b->file, level, 0)) < 0) {
115124
git_config_entries_free(b->entries);
116125
b->entries = NULL;

tests/config/read.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,23 @@ void test_config_read__invalid_quoted_third_section(void)
849849
git_config_free(cfg);
850850
}
851851

852+
void test_config_read__unreadable_file_ignored(void)
853+
{
854+
git_buf buf = GIT_BUF_INIT;
855+
git_config *cfg;
856+
int ret;
857+
858+
cl_set_cleanup(&clean_test_config, NULL);
859+
cl_git_mkfile("./testconfig", "[some] var = value\n[some \"OtheR\"] var = value");
860+
cl_git_pass(p_chmod("./testconfig", 0));
861+
862+
ret = git_config_open_ondisk(&cfg, "./test/config");
863+
cl_assert(ret == 0 || ret == GIT_ENOTFOUND);
864+
865+
git_config_free(cfg);
866+
git_buf_dispose(&buf);
867+
}
868+
852869
void test_config_read__single_line(void)
853870
{
854871
git_buf buf = GIT_BUF_INIT;

0 commit comments

Comments
 (0)