summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2018-02-08 10:27:31 +0000
committerPatrick Steinhardt <ps@pks.im>2018-02-08 17:13:54 +0000
commit2eea5f1cb8a75be997636ebc57c8166f05b324d7 (patch)
tree448366ffddd1bda74d17b92ef0012eff2b6402e6
parent848153f386b45363d033c72e155e4001d0217f96 (diff)
downloadlibgit2-2eea5f1cb8a75be997636ebc57c8166f05b324d7.tar.gz
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 9e66590bd (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.
-rw-r--r--src/config_parse.c2
-rw-r--r--tests/config/read.c15
2 files changed, 16 insertions, 1 deletions
diff --git a/src/config_parse.c b/src/config_parse.c
index 981f968bb..49c948c79 100644
--- a/src/config_parse.c
+++ b/src/config_parse.c
@@ -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);
diff --git a/tests/config/read.c b/tests/config/read.c
index 23dfbe7bb..a34455a0c 100644
--- a/tests/config/read.c
+++ b/tests/config/read.c
@@ -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);
+}