Skip to content

Commit c5a5f12

Browse files
committed
Merge branch 'ef/maint-strbuf-init'
* ef/maint-strbuf-init: config: support values longer than 1023 bytes strbuf: make sure buffer is zero-terminated
2 parents 2a2dbd2 + e96c19c commit c5a5f12

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

config.c

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

134134
static char *parse_value(void)
135135
{
136-
static char value[1024];
137-
int quote = 0, comment = 0, len = 0, space = 0;
136+
static struct strbuf value = STRBUF_INIT;
137+
int quote = 0, comment = 0, space = 0;
138138

139+
strbuf_reset(&value);
139140
for (;;) {
140141
int c = get_next_char();
141-
if (len >= sizeof(value) - 1)
142-
return NULL;
143142
if (c == '\n') {
144143
if (quote)
145144
return NULL;
146-
value[len] = 0;
147-
return value;
145+
return value.buf;
148146
}
149147
if (comment)
150148
continue;
151149
if (isspace(c) && !quote) {
152-
if (len)
150+
if (value.len)
153151
space++;
154152
continue;
155153
}
@@ -160,7 +158,7 @@ static char *parse_value(void)
160158
}
161159
}
162160
for (; space; space--)
163-
value[len++] = ' ';
161+
strbuf_addch(&value, ' ');
164162
if (c == '\\') {
165163
c = get_next_char();
166164
switch (c) {
@@ -182,14 +180,14 @@ static char *parse_value(void)
182180
default:
183181
return NULL;
184182
}
185-
value[len++] = c;
183+
strbuf_addch(&value, c);
186184
continue;
187185
}
188186
if (c == '"') {
189187
quote = 1-quote;
190188
continue;
191189
}
192-
value[len++] = c;
190+
strbuf_addch(&value, c);
193191
}
194192
}
195193

strbuf.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ void strbuf_init(struct strbuf *sb, size_t hint)
3030
{
3131
sb->alloc = sb->len = 0;
3232
sb->buf = strbuf_slopbuf;
33-
if (hint)
33+
if (hint) {
3434
strbuf_grow(sb, hint);
35+
sb->buf[0] = '\0';
36+
}
3537
}
3638

3739
void strbuf_release(struct strbuf *sb)

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)