Skip to content

Commit e96c19c

Browse files
kusmagitster
authored andcommitted
config: support values longer than 1023 bytes
parse_value in config.c has a static buffer of 1024 bytes that it parse the value into. This can sometimes be a problem when a config file contains very long values. It's particularly amusing that git-config already is able to write such files, so it should probably be able to read them as well. Fix this by using a strbuf instead of a fixed-size buffer. Signed-off-by: Erik Faye-Lund <[email protected]> Acked-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5e7a5d9 commit e96c19c

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

config.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,21 @@ static int get_next_char(void)
4646

4747
static char *parse_value(void)
4848
{
49-
static char value[1024];
50-
int quote = 0, comment = 0, len = 0, space = 0;
49+
static struct strbuf value = STRBUF_INIT;
50+
int quote = 0, comment = 0, space = 0;
5151

52+
strbuf_reset(&value);
5253
for (;;) {
5354
int c = get_next_char();
54-
if (len >= sizeof(value) - 1)
55-
return NULL;
5655
if (c == '\n') {
5756
if (quote)
5857
return NULL;
59-
value[len] = 0;
60-
return value;
58+
return value.buf;
6159
}
6260
if (comment)
6361
continue;
6462
if (isspace(c) && !quote) {
65-
if (len)
63+
if (value.len)
6664
space++;
6765
continue;
6866
}
@@ -73,7 +71,7 @@ static char *parse_value(void)
7371
}
7472
}
7573
for (; space; space--)
76-
value[len++] = ' ';
74+
strbuf_addch(&value, ' ');
7775
if (c == '\\') {
7876
c = get_next_char();
7977
switch (c) {
@@ -95,14 +93,14 @@ static char *parse_value(void)
9593
default:
9694
return NULL;
9795
}
98-
value[len++] = c;
96+
strbuf_addch(&value, c);
9997
continue;
10098
}
10199
if (c == '"') {
102100
quote = 1-quote;
103101
continue;
104102
}
105-
value[len++] = c;
103+
strbuf_addch(&value, c);
106104
}
107105
}
108106

t/t1303-wacky-config.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ LONG_VALUE=$(printf "x%01021dx a" 7)
4444
test_expect_success 'do not crash on special long config line' '
4545
setup &&
4646
git config section.key "$LONG_VALUE" &&
47-
check section.key "fatal: bad config file line 2 in .git/config"
47+
check section.key "$LONG_VALUE"
4848
'
4949

5050
test_done

0 commit comments

Comments
 (0)