config: escape subsection names when creating them This allows us to set options like "some.foo\\ish.var". This closes #830
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
diff --git a/src/config_file.c b/src/config_file.c
index 1f3ebfc..7ced1e5 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -983,9 +983,12 @@ static int write_section(git_filebuf *file, const char *key)
if (dot == NULL) {
git_buf_puts(&buf, key);
} else {
+ char *escaped;
git_buf_put(&buf, key, dot - key);
- /* TODO: escape */
- git_buf_printf(&buf, " \"%s\"", dot + 1);
+ escaped = escape_value(dot + 1);
+ GITERR_CHECK_ALLOC(escaped);
+ git_buf_printf(&buf, " \"%s\"", escaped);
+ git__free(escaped);
}
git_buf_puts(&buf, "]\n");
diff --git a/tests-clar/config/stress.c b/tests-clar/config/stress.c
index 3de1f76..8fbc8b9 100644
--- a/tests-clar/config/stress.c
+++ b/tests-clar/config/stress.c
@@ -59,3 +59,25 @@ void test_config_stress__comments(void)
git_config_free(config);
}
+
+void test_config_stress__escape_subsection_names(void)
+{
+ struct git_config_file *file;
+ git_config *config;
+ const char *str;
+
+ cl_assert(git_path_exists("git-test-config"));
+ cl_git_pass(git_config_file__ondisk(&file, "git-test-config"));
+ cl_git_pass(git_config_new(&config));
+ cl_git_pass(git_config_add_file(config, file, 0));
+
+ cl_git_pass(git_config_set_string(config, "some.sec\\tion.other", "foo"));
+ git_config_free(config);
+
+ cl_git_pass(git_config_file__ondisk(&file, "git-test-config"));
+ cl_git_pass(git_config_new(&config));
+ cl_git_pass(git_config_add_file(config, file, 0));
+
+ cl_git_pass(git_config_get_string(&str, config, "some.sec\\tion.other"));
+ cl_assert(!strcmp("foo", str));
+}