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 <martin@gnutiken.se> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Martin Stenberg committed
Mar 9, 2012 at 22:57 UTC
4b340593551217904d794cc0a8db55db89b5b066
2 files changed
+40
-4
config.c
+9
-4
@@ -135,8 +135,10 @@ static char *parse_value(void)
135
for (;;) {
136
int c = get_next_char();
137
if (c == '\n') {
138
- if (quote)
138
+ if (quote) {
139
+ cf->linenr--;
140
return NULL;
141
+ }
142
return cf->value.buf;
143
}
144
if (comment)
@@ -226,7 +228,7 @@ static int get_extended_base_var(char *name, int baselen, int c)
228
{
229
do {
230
if (c == '\n')
229
- return -1;
231
+ goto error_incomplete_line;
232
c = get_next_char();
233
} while (isspace(c));
234
@@ -238,13 +240,13 @@ static int get_extended_base_var(char *name, int baselen, int c)
240
for (;;) {
241
int c = get_next_char();
242
if (c == '\n')
241
- return -1;
243
+ goto error_incomplete_line;
244
if (c == '"')
245
break;
246
if (c == '\\') {
247
c = get_next_char();
248
if (c == '\n')
247
- return -1;
249
+ goto error_incomplete_line;
250
}
251
name[baselen++] = c;
252
if (baselen > MAXNAME / 2)
@@ -255,6 +257,9 @@ static int get_extended_base_var(char *name, int baselen, int c)
257
if (get_next_char() != ']')
258
return -1;
259
return baselen;
260
+error_incomplete_line:
261
+ cf->linenr--;
262
+ return -1;
263
}
264
265
static int get_base_var(char *name)
t/t1300-repo-config.sh
+31
@@ -928,4 +928,35 @@ test_expect_success 'git -c complains about empty key and value' '
928
test_must_fail git -c "" rev-parse
929
'
930
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
+
962
test_done