Commit 848153f386b45363d033c72e155e4001d0217f96

Patrick Steinhardt 2018-02-08T10:02:29

config_parse: handle empty lines with CRLF Currently, the configuration parser will fail reading empty lines with just an CRLF-style line ending. Special-case the '\r' character in order to handle it the same as Unix-style line endings. Add tests to spot this regression in the future.

diff --git a/src/config_parse.c b/src/config_parse.c
index 2b9669c..981f968 100644
--- a/src/config_parse.c
+++ b/src/config_parse.c
@@ -495,6 +495,7 @@ int git_config_parse(
 			break;
 
 		case '\n': /* comment or whitespace-only */
+		case '\r':
 		case ' ':
 		case '\t':
 		case ';':
diff --git a/tests/config/read.c b/tests/config/read.c
index 25a4fca..23dfbe7 100644
--- a/tests/config/read.c
+++ b/tests/config/read.c
@@ -703,3 +703,33 @@ void test_config_read__path(void)
 	git_buf_free(&expected_path);
 	git_config_free(cfg);
 }
+
+void test_config_read__crlf_style_line_endings(void)
+{
+	git_buf buf = GIT_BUF_INIT;
+	git_config *cfg;
+
+	cl_set_cleanup(&clean_test_config, NULL);
+	cl_git_mkfile("./testconfig", "[some]\r\n var = value\r\n");
+	cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
+	cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var"));
+	cl_assert_equal_s(buf.ptr, "value");
+
+	git_config_free(cfg);
+	git_buf_free(&buf);
+}
+
+void test_config_read__trailing_crlf(void)
+{
+	git_buf buf = GIT_BUF_INIT;
+	git_config *cfg;
+
+	cl_set_cleanup(&clean_test_config, NULL);
+	cl_git_mkfile("./testconfig", "[some]\r\n var = value\r\n\r\n");
+	cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
+	cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var"));
+	cl_assert_equal_s(buf.ptr, "value");
+
+	git_config_free(cfg);
+	git_buf_free(&buf);
+}