From 5e7a5d97f8a74181634a70e0e7b1464855c5af2d Mon Sep 17 00:00:00 2001 From: Erik Faye-Lund Date: Sun, 10 Apr 2011 22:54:17 +0200 Subject: [PATCH 1/2] strbuf: make sure buffer is zero-terminated strbuf_init does not zero-terminate the initial buffer when hint is non-zero. Fix this so we can rely on the string to be zero-terminated even if we haven't filled it with anything yet. Signed-off-by: Erik Faye-Lund Acked-by: Jeff King Signed-off-by: Junio C Hamano --- strbuf.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/strbuf.c b/strbuf.c index bc3a0802ea..73e0400596 100644 --- a/strbuf.c +++ b/strbuf.c @@ -30,8 +30,10 @@ void strbuf_init(struct strbuf *sb, size_t hint) { sb->alloc = sb->len = 0; sb->buf = strbuf_slopbuf; - if (hint) + if (hint) { strbuf_grow(sb, hint); + sb->buf[0] = '\0'; + } } void strbuf_release(struct strbuf *sb) From e96c19c50fb0807570b85cb5b8aae6dfcfa9b9ec Mon Sep 17 00:00:00 2001 From: Erik Faye-Lund Date: Sun, 10 Apr 2011 22:54:18 +0200 Subject: [PATCH 2/2] 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 Acked-by: Jeff King Signed-off-by: Junio C Hamano --- config.c | 18 ++++++++---------- t/t1303-wacky-config.sh | 2 +- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/config.c b/config.c index 6963fbea43..5a1db4ff0b 100644 --- a/config.c +++ b/config.c @@ -46,23 +46,21 @@ static int get_next_char(void) static char *parse_value(void) { - static char value[1024]; - int quote = 0, comment = 0, len = 0, space = 0; + static struct strbuf value = STRBUF_INIT; + int quote = 0, comment = 0, space = 0; + strbuf_reset(&value); for (;;) { int c = get_next_char(); - if (len >= sizeof(value) - 1) - return NULL; if (c == '\n') { if (quote) return NULL; - value[len] = 0; - return value; + return value.buf; } if (comment) continue; if (isspace(c) && !quote) { - if (len) + if (value.len) space++; continue; } @@ -73,7 +71,7 @@ static char *parse_value(void) } } for (; space; space--) - value[len++] = ' '; + strbuf_addch(&value, ' '); if (c == '\\') { c = get_next_char(); switch (c) { @@ -95,14 +93,14 @@ static char *parse_value(void) default: return NULL; } - value[len++] = c; + strbuf_addch(&value, c); continue; } if (c == '"') { quote = 1-quote; continue; } - value[len++] = c; + strbuf_addch(&value, c); } } diff --git a/t/t1303-wacky-config.sh b/t/t1303-wacky-config.sh index 080117c6bc..46103a1591 100755 --- a/t/t1303-wacky-config.sh +++ b/t/t1303-wacky-config.sh @@ -44,7 +44,7 @@ LONG_VALUE=$(printf "x%01021dx a" 7) test_expect_success 'do not crash on special long config line' ' setup && 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