Commit 5d9cfa07ac62ad15ebb669b01e723a990450383e

Carlos Martín Nieto 2012-07-20T17:52:53

config: escape subsection names when creating them This allows us to set options like "some.foo\\ish.var". This closes #830

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));
+}