Skip to content

Commit 848153f

Browse files
committed
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.
1 parent 5340ca7 commit 848153f

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/config_parse.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ int git_config_parse(
495495
break;
496496

497497
case '\n': /* comment or whitespace-only */
498+
case '\r':
498499
case ' ':
499500
case '\t':
500501
case ';':

tests/config/read.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,3 +703,33 @@ void test_config_read__path(void)
703703
git_buf_free(&expected_path);
704704
git_config_free(cfg);
705705
}
706+
707+
void test_config_read__crlf_style_line_endings(void)
708+
{
709+
git_buf buf = GIT_BUF_INIT;
710+
git_config *cfg;
711+
712+
cl_set_cleanup(&clean_test_config, NULL);
713+
cl_git_mkfile("./testconfig", "[some]\r\n var = value\r\n");
714+
cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
715+
cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var"));
716+
cl_assert_equal_s(buf.ptr, "value");
717+
718+
git_config_free(cfg);
719+
git_buf_free(&buf);
720+
}
721+
722+
void test_config_read__trailing_crlf(void)
723+
{
724+
git_buf buf = GIT_BUF_INIT;
725+
git_config *cfg;
726+
727+
cl_set_cleanup(&clean_test_config, NULL);
728+
cl_git_mkfile("./testconfig", "[some]\r\n var = value\r\n\r\n");
729+
cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
730+
cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var"));
731+
cl_assert_equal_s(buf.ptr, "value");
732+
733+
git_config_free(cfg);
734+
git_buf_free(&buf);
735+
}

0 commit comments

Comments
 (0)