Skip to content

Commit

Permalink
config: report errors at the EOL with correct line number
Browse files Browse the repository at this point in the history
A section in a config file with a missing "]" reports the next line
as bad, same goes to a value with a missing end quote.

This happens because the error is not detected until the end of the
line, when line number is already increased. Fix this by decreasing
line number by one for these cases.

Signed-off-by: Martin Stenberg <martin@gnutiken.se>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Martin Stenberg authored and Junio C Hamano committed Mar 12, 2012
1 parent d909e07 commit 4b34059
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
13 changes: 9 additions & 4 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ static char *parse_value(void)
for (;;) {
int c = get_next_char();
if (c == '\n') {
if (quote)
if (quote) {
cf->linenr--;
return NULL;
}
return cf->value.buf;
}
if (comment)
Expand Down Expand Up @@ -226,7 +228,7 @@ static int get_extended_base_var(char *name, int baselen, int c)
{
do {
if (c == '\n')
return -1;
goto error_incomplete_line;
c = get_next_char();
} while (isspace(c));

Expand All @@ -238,13 +240,13 @@ static int get_extended_base_var(char *name, int baselen, int c)
for (;;) {
int c = get_next_char();
if (c == '\n')
return -1;
goto error_incomplete_line;
if (c == '"')
break;
if (c == '\\') {
c = get_next_char();
if (c == '\n')
return -1;
goto error_incomplete_line;
}
name[baselen++] = c;
if (baselen > MAXNAME / 2)
Expand All @@ -255,6 +257,9 @@ static int get_extended_base_var(char *name, int baselen, int c)
if (get_next_char() != ']')
return -1;
return baselen;
error_incomplete_line:
cf->linenr--;
return -1;
}

static int get_base_var(char *name)
Expand Down
31 changes: 31 additions & 0 deletions t/t1300-repo-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -928,4 +928,35 @@ test_expect_success 'git -c complains about empty key and value' '
test_must_fail git -c "" rev-parse
'

# malformed configuration files
test_expect_success 'barf on syntax error' '
cat >.git/config <<-\EOF &&
# broken section line
[section]
key garbage
EOF
test_must_fail git config --get section.key >actual 2>error &&
grep " line 3 " error
'

test_expect_success 'barf on incomplete section header' '
cat >.git/config <<-\EOF &&
# broken section line
[section
key = value
EOF
test_must_fail git config --get section.key >actual 2>error &&
grep " line 2 " error
'

test_expect_success 'barf on incomplete string' '
cat >.git/config <<-\EOF &&
# broken section line
[section]
key = "value string
EOF
test_must_fail git config --get section.key >actual 2>error &&
grep " line 3 " error
'

test_done

0 comments on commit 4b34059

Please sign in to comment.