Skip to content

Commit 4b34059

Browse files
Martin Stenberggitster
authored andcommitted
config: report errors at the EOL with correct line number
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d909e07 commit 4b34059

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

config.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,10 @@ static char *parse_value(void)
135135
for (;;) {
136136
int c = get_next_char();
137137
if (c == '\n') {
138-
if (quote)
138+
if (quote) {
139+
cf->linenr--;
139140
return NULL;
141+
}
140142
return cf->value.buf;
141143
}
142144
if (comment)
@@ -226,7 +228,7 @@ static int get_extended_base_var(char *name, int baselen, int c)
226228
{
227229
do {
228230
if (c == '\n')
229-
return -1;
231+
goto error_incomplete_line;
230232
c = get_next_char();
231233
} while (isspace(c));
232234

@@ -238,13 +240,13 @@ static int get_extended_base_var(char *name, int baselen, int c)
238240
for (;;) {
239241
int c = get_next_char();
240242
if (c == '\n')
241-
return -1;
243+
goto error_incomplete_line;
242244
if (c == '"')
243245
break;
244246
if (c == '\\') {
245247
c = get_next_char();
246248
if (c == '\n')
247-
return -1;
249+
goto error_incomplete_line;
248250
}
249251
name[baselen++] = c;
250252
if (baselen > MAXNAME / 2)
@@ -255,6 +257,9 @@ static int get_extended_base_var(char *name, int baselen, int c)
255257
if (get_next_char() != ']')
256258
return -1;
257259
return baselen;
260+
error_incomplete_line:
261+
cf->linenr--;
262+
return -1;
258263
}
259264

260265
static int get_base_var(char *name)

t/t1300-repo-config.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,4 +928,35 @@ test_expect_success 'git -c complains about empty key and value' '
928928
test_must_fail git -c "" rev-parse
929929
'
930930

931+
# malformed configuration files
932+
test_expect_success 'barf on syntax error' '
933+
cat >.git/config <<-\EOF &&
934+
# broken section line
935+
[section]
936+
key garbage
937+
EOF
938+
test_must_fail git config --get section.key >actual 2>error &&
939+
grep " line 3 " error
940+
'
941+
942+
test_expect_success 'barf on incomplete section header' '
943+
cat >.git/config <<-\EOF &&
944+
# broken section line
945+
[section
946+
key = value
947+
EOF
948+
test_must_fail git config --get section.key >actual 2>error &&
949+
grep " line 2 " error
950+
'
951+
952+
test_expect_success 'barf on incomplete string' '
953+
cat >.git/config <<-\EOF &&
954+
# broken section line
955+
[section]
956+
key = "value string
957+
EOF
958+
test_must_fail git config --get section.key >actual 2>error &&
959+
grep " line 3 " error
960+
'
961+
931962
test_done

0 commit comments

Comments
 (0)