Skip to content

Commit dff05bc

Browse files
committed
Multiline config values not preserved on saving
(fixes issue libgit2#6088) Signed-off-by: Sven Strickroth <email@cs-ware.de>
1 parent 854164a commit dff05bc

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/config_parse.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ static int unescape_line(
325325
return 0;
326326
}
327327

328-
static int parse_multiline_variable(git_config_parser *reader, git_str *value, int in_quotes)
328+
static int parse_multiline_variable(git_config_parser *reader, git_str *value, int in_quotes, size_t *line_len)
329329
{
330330
int quote_count;
331331
bool multiline = true;
@@ -338,6 +338,10 @@ static int parse_multiline_variable(git_config_parser *reader, git_str *value, i
338338
git_parse_advance_line(&reader->ctx);
339339
line = git__strndup(reader->ctx.line, reader->ctx.line_len);
340340
GIT_ERROR_CHECK_ALLOC(line);
341+
if (GIT_ADD_SIZET_OVERFLOW(line_len, *line_len, reader->ctx.line_len)) {
342+
error = -1;
343+
goto out;
344+
}
341345

342346
/*
343347
* We've reached the end of the file, there is no continuation.
@@ -415,7 +419,7 @@ static int parse_name(
415419
return 0;
416420
}
417421

418-
static int parse_variable(git_config_parser *reader, char **var_name, char **var_value)
422+
static int parse_variable(git_config_parser *reader, char **var_name, char **var_value, size_t *line_len)
419423
{
420424
const char *value_start = NULL;
421425
char *line = NULL, *name = NULL, *value = NULL;
@@ -449,7 +453,7 @@ static int parse_variable(git_config_parser *reader, char **var_name, char **var
449453
git_str_attach(&multi_value, value, 0);
450454
value = NULL;
451455

452-
if (parse_multiline_variable(reader, &multi_value, quote_count % 2) < 0 ||
456+
if (parse_multiline_variable(reader, &multi_value, quote_count % 2, line_len) < 0 ||
453457
git_str_oom(&multi_value)) {
454458
error = -1;
455459
git_str_dispose(&multi_value);
@@ -554,7 +558,7 @@ int git_config_parse(
554558
break;
555559

556560
default: /* assume variable declaration */
557-
if ((result = parse_variable(parser, &var_name, &var_value)) == 0 && on_variable) {
561+
if ((result = parse_variable(parser, &var_name, &var_value, &line_len)) == 0 && on_variable) {
558562
result = on_variable(parser, current_section, var_name, var_value, line_start, line_len, payload);
559563
git__free(var_name);
560564
git__free(var_value);

tests/config/write.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ void test_config_write__initialize(void)
88
cl_fixture_sandbox("config/config9");
99
cl_fixture_sandbox("config/config15");
1010
cl_fixture_sandbox("config/config17");
11+
cl_fixture_sandbox("config/config22");
1112
}
1213

1314
void test_config_write__cleanup(void)
1415
{
1516
cl_fixture_cleanup("config9");
1617
cl_fixture_cleanup("config15");
1718
cl_fixture_cleanup("config17");
19+
cl_fixture_cleanup("config22");
1820
}
1921

2022
void test_config_write__replace_value(void)
@@ -743,3 +745,20 @@ void test_config_write__preserve_case(void)
743745

744746
git_config_free(cfg);
745747
}
748+
749+
void test_config_write__write_config_file_with_multi_line_value(void)
750+
{
751+
git_config* cfg;
752+
git_buf buf = GIT_BUF_INIT;
753+
754+
cl_git_pass(git_config_open_ondisk(&cfg, "config22"));
755+
cl_git_pass(git_config_get_string_buf(&buf, cfg, "alias.m"));
756+
cl_assert_equal_s("cmd ;; ;; bar", buf.ptr);
757+
cl_git_pass(git_config_set_string(cfg, "sOMe.ThInG", "foo"));
758+
git_buf_dispose(&buf);
759+
cl_git_pass(git_config_get_string_buf(&buf, cfg, "alias.m"));
760+
cl_assert_equal_s("cmd ;; ;; bar", buf.ptr);
761+
git_buf_dispose(&buf);
762+
763+
git_config_free(cfg);
764+
}

0 commit comments

Comments
 (0)