Skip to content

Commit ed6ce43

Browse files
committed
Merge branch 'ms/maint-config-error-at-eol-linecount' into maint
* ms/maint-config-error-at-eol-linecount: config: report errors at the EOL with correct line number
2 parents a12c6b0 + 4b34059 commit ed6ce43

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
@@ -960,4 +960,35 @@ test_expect_success 'git -c complains about empty key and value' '
960960
test_must_fail git -c "" rev-parse
961961
'
962962

963+
# malformed configuration files
964+
test_expect_success 'barf on syntax error' '
965+
cat >.git/config <<-\EOF &&
966+
# broken section line
967+
[section]
968+
key garbage
969+
EOF
970+
test_must_fail git config --get section.key >actual 2>error &&
971+
grep " line 3 " error
972+
'
973+
974+
test_expect_success 'barf on incomplete section header' '
975+
cat >.git/config <<-\EOF &&
976+
# broken section line
977+
[section
978+
key = value
979+
EOF
980+
test_must_fail git config --get section.key >actual 2>error &&
981+
grep " line 2 " error
982+
'
983+
984+
test_expect_success 'barf on incomplete string' '
985+
cat >.git/config <<-\EOF &&
986+
# broken section line
987+
[section]
988+
key = "value string
989+
EOF
990+
test_must_fail git config --get section.key >actual 2>error &&
991+
grep " line 3 " error
992+
'
993+
963994
test_done

0 commit comments

Comments
 (0)