Commit 2eea5f1c by Patrick Steinhardt

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 9e66590b (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.
parent 848153f3
......@@ -217,7 +217,7 @@ static int skip_bom(git_parse_ctx *parser)
{
git_buf buf = GIT_BUF_INIT_CONST(parser->content, parser->content_len);
git_bom_t bom;
int bom_offset = git_buf_text_detect_bom(&bom, &buf, parser->content_len);
int bom_offset = git_buf_text_detect_bom(&bom, &buf, 0);
if (bom == GIT_BOM_UTF8)
git_parse_advance_chars(parser, bom_offset);
......
......@@ -733,3 +733,18 @@ void test_config_read__trailing_crlf(void)
git_config_free(cfg);
git_buf_free(&buf);
}
void test_config_read__bom(void)
{
git_buf buf = GIT_BUF_INIT;
git_config *cfg;
cl_set_cleanup(&clean_test_config, NULL);
cl_git_mkfile("./testconfig", "\xEF\xBB\xBF[some]\n var = value\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);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment