Skip to content

Commit 8736c90

Browse files
committed
Merge branch 'mh/maint-parse-dirstat-fix'
Cleans up some code and avoids a potential bug. * mh/maint-parse-dirstat-fix: parse_dirstat_params(): use string_list to split comma-separated string
2 parents f07e555 + 02e8ca0 commit 8736c90

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

diff.c

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "sigchain.h"
1616
#include "submodule.h"
1717
#include "ll-merge.h"
18+
#include "string-list.h"
1819

1920
#ifdef NO_FAST_WORKING_DIRECTORY
2021
#define FAST_WORKING_DIRECTORY 0
@@ -69,26 +70,30 @@ static int parse_diff_color_slot(const char *var, int ofs)
6970
return -1;
7071
}
7172

72-
static int parse_dirstat_params(struct diff_options *options, const char *params,
73+
static int parse_dirstat_params(struct diff_options *options, const char *params_string,
7374
struct strbuf *errmsg)
7475
{
75-
const char *p = params;
76-
int p_len, ret = 0;
76+
char *params_copy = xstrdup(params_string);
77+
struct string_list params = STRING_LIST_INIT_NODUP;
78+
int ret = 0;
79+
int i;
7780

78-
while (*p) {
79-
p_len = strchrnul(p, ',') - p;
80-
if (!memcmp(p, "changes", p_len)) {
81+
if (*params_copy)
82+
string_list_split_in_place(&params, params_copy, ',', -1);
83+
for (i = 0; i < params.nr; i++) {
84+
const char *p = params.items[i].string;
85+
if (!strcmp(p, "changes")) {
8186
DIFF_OPT_CLR(options, DIRSTAT_BY_LINE);
8287
DIFF_OPT_CLR(options, DIRSTAT_BY_FILE);
83-
} else if (!memcmp(p, "lines", p_len)) {
88+
} else if (!strcmp(p, "lines")) {
8489
DIFF_OPT_SET(options, DIRSTAT_BY_LINE);
8590
DIFF_OPT_CLR(options, DIRSTAT_BY_FILE);
86-
} else if (!memcmp(p, "files", p_len)) {
91+
} else if (!strcmp(p, "files")) {
8792
DIFF_OPT_CLR(options, DIRSTAT_BY_LINE);
8893
DIFF_OPT_SET(options, DIRSTAT_BY_FILE);
89-
} else if (!memcmp(p, "noncumulative", p_len)) {
94+
} else if (!strcmp(p, "noncumulative")) {
9095
DIFF_OPT_CLR(options, DIRSTAT_CUMULATIVE);
91-
} else if (!memcmp(p, "cumulative", p_len)) {
96+
} else if (!strcmp(p, "cumulative")) {
9297
DIFF_OPT_SET(options, DIRSTAT_CUMULATIVE);
9398
} else if (isdigit(*p)) {
9499
char *end;
@@ -100,24 +105,21 @@ static int parse_dirstat_params(struct diff_options *options, const char *params
100105
while (isdigit(*++end))
101106
; /* nothing */
102107
}
103-
if (end - p == p_len)
108+
if (!*end)
104109
options->dirstat_permille = permille;
105110
else {
106-
strbuf_addf(errmsg, _(" Failed to parse dirstat cut-off percentage '%.*s'\n"),
107-
p_len, p);
111+
strbuf_addf(errmsg, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
112+
p);
108113
ret++;
109114
}
110115
} else {
111-
strbuf_addf(errmsg, _(" Unknown dirstat parameter '%.*s'\n"),
112-
p_len, p);
116+
strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p);
113117
ret++;
114118
}
115119

116-
p += p_len;
117-
118-
if (*p)
119-
p++; /* more parameters, swallow separator */
120120
}
121+
string_list_clear(&params, 0);
122+
free(params_copy);
121123
return ret;
122124
}
123125

0 commit comments

Comments
 (0)