Skip to content

Commit 1d718fa

Browse files
committed
config: variables might appear on the same line as a section header
While rare and a machine would typically not generate such a configuration file, it is nevertheless valid to write [foo "bar"] baz = true and we need to deal with that instead of assuming everything is on its own line.
1 parent 1cbc960 commit 1d718fa

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/config_parse.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ static int parse_section_header_ext(git_config_parser *reader, const char *line,
6363
{
6464
int c, rpos;
6565
char *first_quote, *last_quote;
66+
const char *line_start = line;
6667
git_buf buf = GIT_BUF_INIT;
6768
size_t quoted_len, alloc_len, base_name_len = strlen(base_name);
6869

@@ -139,7 +140,7 @@ static int parse_section_header_ext(git_config_parser *reader, const char *line,
139140
}
140141

141142
*section_name = git_buf_detach(&buf);
142-
return 0;
143+
return &line[rpos + 2] - line_start; /* rpos is at the closing quote */
143144

144145
end_error:
145146
git_buf_dispose(&buf);
@@ -209,7 +210,7 @@ static int parse_section_header(git_config_parser *reader, char **section_out)
209210
name[name_length] = 0;
210211
*section_out = name;
211212

212-
return 0;
213+
return pos;
213214

214215
fail_parse:
215216
git__free(line);
@@ -481,10 +482,14 @@ int git_config_parse(
481482
skip_bom(ctx);
482483

483484
for (; ctx->remain_len > 0; git_parse_advance_line(ctx)) {
484-
const char *line_start = parser->ctx.line;
485-
size_t line_len = parser->ctx.line_len;
485+
const char *line_start;
486+
size_t line_len;
486487
char c;
487488

489+
restart:
490+
line_start = ctx->line;
491+
line_len = ctx->line_len;
492+
488493
/*
489494
* Get either first non-whitespace character or, if that does
490495
* not exist, the first whitespace character. This is required
@@ -499,9 +504,24 @@ int git_config_parse(
499504
git__free(current_section);
500505
current_section = NULL;
501506

502-
if ((result = parse_section_header(parser, &current_section)) == 0 && on_section) {
507+
result = parse_section_header(parser, &current_section);
508+
if (result < 0)
509+
break;
510+
511+
git_parse_advance_chars(ctx, result);
512+
513+
if (on_section)
503514
result = on_section(parser, current_section, line_start, line_len, data);
504-
}
515+
/*
516+
* After we've parsed the section header we may not be
517+
* done with the line. If there's still data in there,
518+
* run the next loop with the rest of the current line
519+
* instead of moving forward.
520+
*/
521+
522+
if (!git_parse_peek(&c, ctx, GIT_PARSE_PEEK_SKIP_WHITESPACE))
523+
goto restart;
524+
505525
break;
506526

507527
case '\n': /* comment or whitespace-only */

0 commit comments

Comments
 (0)