Skip to content

Commit eff7c32

Browse files
committed
Merge branch 'jk/maint-config-param' into maint
* jk/maint-config-param: config: use strbuf_split_str instead of a temporary strbuf strbuf: allow strbuf_split to work on non-strbufs config: avoid segfault when parsing command-line config config: die on error in command-line config fix "git -c" parsing of values with equals signs strbuf_split: add a max parameter
2 parents 7baf32a + f77bcca commit eff7c32

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

config.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ void git_config_push_parameter(const char *text)
4242
static int git_config_parse_parameter(const char *text,
4343
config_fn_t fn, void *data)
4444
{
45-
struct strbuf tmp = STRBUF_INIT;
4645
struct strbuf **pair;
47-
strbuf_addstr(&tmp, text);
48-
pair = strbuf_split(&tmp, '=');
46+
pair = strbuf_split_str(text, '=', 2);
47+
if (!pair[0])
48+
return error("bogus config parameter: %s", text);
4949
if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=')
5050
strbuf_setlen(pair[0], pair[0]->len - 1);
5151
strbuf_trim(pair[0]);
@@ -856,7 +856,7 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
856856

857857
switch (git_config_from_parameters(fn, data)) {
858858
case -1: /* error */
859-
ret--;
859+
die("unable to parse command-line config");
860860
break;
861861
case 0: /* found nothing */
862862
break;

strbuf.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,24 +103,27 @@ void strbuf_ltrim(struct strbuf *sb)
103103
sb->buf[sb->len] = '\0';
104104
}
105105

106-
struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
106+
struct strbuf **strbuf_split_buf(const char *str, size_t slen, int delim, int max)
107107
{
108108
int alloc = 2, pos = 0;
109-
char *n, *p;
109+
const char *n, *p;
110110
struct strbuf **ret;
111111
struct strbuf *t;
112112

113113
ret = xcalloc(alloc, sizeof(struct strbuf *));
114-
p = n = sb->buf;
115-
while (n < sb->buf + sb->len) {
114+
p = n = str;
115+
while (n < str + slen) {
116116
int len;
117-
n = memchr(n, delim, sb->len - (n - sb->buf));
117+
if (max <= 0 || pos + 1 < max)
118+
n = memchr(n, delim, slen - (n - str));
119+
else
120+
n = NULL;
118121
if (pos + 1 >= alloc) {
119122
alloc = alloc * 2;
120123
ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
121124
}
122125
if (!n)
123-
n = sb->buf + sb->len - 1;
126+
n = str + slen - 1;
124127
len = n - p + 1;
125128
t = xmalloc(sizeof(struct strbuf));
126129
strbuf_init(t, len);

strbuf.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,22 @@ extern void strbuf_rtrim(struct strbuf *);
4444
extern void strbuf_ltrim(struct strbuf *);
4545
extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);
4646

47-
extern struct strbuf **strbuf_split(const struct strbuf *, int delim);
47+
extern struct strbuf **strbuf_split_buf(const char *, size_t,
48+
int delim, int max);
49+
static inline struct strbuf **strbuf_split_str(const char *str,
50+
int delim, int max)
51+
{
52+
return strbuf_split_buf(str, strlen(str), delim, max);
53+
}
54+
static inline struct strbuf **strbuf_split_max(const struct strbuf *sb,
55+
int delim, int max)
56+
{
57+
return strbuf_split_buf(sb->buf, sb->len, delim, max);
58+
}
59+
static inline struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
60+
{
61+
return strbuf_split_max(sb, delim, 0);
62+
}
4863
extern void strbuf_list_free(struct strbuf **);
4964

5065
/*----- add data in your buffer -----*/

t/t1300-repo-config.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,4 +904,22 @@ test_expect_success 'git -c works with aliases of builtins' '
904904
test_cmp expect actual
905905
'
906906

907+
test_expect_success 'git -c does not split values on equals' '
908+
echo "value with = in it" >expect &&
909+
git -c core.foo="value with = in it" config core.foo >actual &&
910+
test_cmp expect actual
911+
'
912+
913+
test_expect_success 'git -c dies on bogus config' '
914+
test_must_fail git -c core.bare=foo rev-parse
915+
'
916+
917+
test_expect_success 'git -c complains about empty key' '
918+
test_must_fail git -c "=foo" rev-parse
919+
'
920+
921+
test_expect_success 'git -c complains about empty key and value' '
922+
test_must_fail git -c "" rev-parse
923+
'
924+
907925
test_done

0 commit comments

Comments
 (0)