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 <kusmabite@gmail.com>
Acked-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Erik Faye-Lund 2011-04-10 22:54:18 +02:00 committed by Junio C Hamano
parent 5e7a5d97f8
commit e96c19c50f
2 changed files with 9 additions and 11 deletions

View File

@ -46,23 +46,21 @@ static int get_next_char(void)
static char *parse_value(void) static char *parse_value(void)
{ {
static char value[1024]; static struct strbuf value = STRBUF_INIT;
int quote = 0, comment = 0, len = 0, space = 0; int quote = 0, comment = 0, space = 0;
strbuf_reset(&value);
for (;;) { for (;;) {
int c = get_next_char(); int c = get_next_char();
if (len >= sizeof(value) - 1)
return NULL;
if (c == '\n') { if (c == '\n') {
if (quote) if (quote)
return NULL; return NULL;
value[len] = 0; return value.buf;
return value;
} }
if (comment) if (comment)
continue; continue;
if (isspace(c) && !quote) { if (isspace(c) && !quote) {
if (len) if (value.len)
space++; space++;
continue; continue;
} }
@ -73,7 +71,7 @@ static char *parse_value(void)
} }
} }
for (; space; space--) for (; space; space--)
value[len++] = ' '; strbuf_addch(&value, ' ');
if (c == '\\') { if (c == '\\') {
c = get_next_char(); c = get_next_char();
switch (c) { switch (c) {
@ -95,14 +93,14 @@ static char *parse_value(void)
default: default:
return NULL; return NULL;
} }
value[len++] = c; strbuf_addch(&value, c);
continue; continue;
} }
if (c == '"') { if (c == '"') {
quote = 1-quote; quote = 1-quote;
continue; continue;
} }
value[len++] = c; strbuf_addch(&value, c);
} }
} }

View File

@ -44,7 +44,7 @@ LONG_VALUE=$(printf "x%01021dx a" 7)
test_expect_success 'do not crash on special long config line' ' test_expect_success 'do not crash on special long config line' '
setup && setup &&
git config section.key "$LONG_VALUE" && git config section.key "$LONG_VALUE" &&
check section.key "fatal: bad config file line 2 in .git/config" check section.key "$LONG_VALUE"
' '
test_done test_done