Skip to content

Commit 2eea5f1

Browse files
committed
config_parse: fix reading files with BOM
The function `skip_bom` is being used to detect and skip BOM marks previously to parsing a configuration file. To do so, it simply uses `git_buf_text_detect_bom`. But since the refactoring to use the parser interface in commit 9e66590 (config_parse: use common parser interface, 2017-07-21), the BOM detection was actually broken. The issue stems from a misunderstanding of `git_buf_text_detect_bom`. It was assumed that its third parameter limits the length of the character sequence that is to be analyzed, while in fact it was an offset at which we want to detect the BOM. Fix the parameter to be `0` instead of the buffer length, as we always want to check the beginning of the configuration file.
1 parent 848153f commit 2eea5f1

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/config_parse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ static int skip_bom(git_parse_ctx *parser)
217217
{
218218
git_buf buf = GIT_BUF_INIT_CONST(parser->content, parser->content_len);
219219
git_bom_t bom;
220-
int bom_offset = git_buf_text_detect_bom(&bom, &buf, parser->content_len);
220+
int bom_offset = git_buf_text_detect_bom(&bom, &buf, 0);
221221

222222
if (bom == GIT_BOM_UTF8)
223223
git_parse_advance_chars(parser, bom_offset);

tests/config/read.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,3 +733,18 @@ void test_config_read__trailing_crlf(void)
733733
git_config_free(cfg);
734734
git_buf_free(&buf);
735735
}
736+
737+
void test_config_read__bom(void)
738+
{
739+
git_buf buf = GIT_BUF_INIT;
740+
git_config *cfg;
741+
742+
cl_set_cleanup(&clean_test_config, NULL);
743+
cl_git_mkfile("./testconfig", "\xEF\xBB\xBF[some]\n var = value\n");
744+
cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
745+
cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var"));
746+
cl_assert_equal_s(buf.ptr, "value");
747+
748+
git_config_free(cfg);
749+
git_buf_free(&buf);
750+
}

0 commit comments

Comments
 (0)