Skip to content

Commit ab91f6b

Browse files
committed
Merge branch 'rs/diff-parseopts'
The way the diff machinery prepares the options array for the parse_options API has been refactored to avoid resource leaks. * rs/diff-parseopts: diff: remove parseopts member from struct diff_options diff: use add_diff_options() in diff_opt_parse() diff: factor out add_diff_options()
2 parents 995916e + 189e97b commit ab91f6b

File tree

4 files changed

+11
-15
lines changed

4 files changed

+11
-15
lines changed

builtin/range-diff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
4747

4848
repo_diff_setup(the_repository, &diffopt);
4949

50-
options = parse_options_concat(range_diff_options, diffopt.parseopts);
50+
options = add_diff_options(range_diff_options, &diffopt);
5151
argc = parse_options(argc, argv, prefix, options,
5252
builtin_range_diff_usage, PARSE_OPT_KEEP_DASHDASH);
5353

diff-no-index.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,7 @@ int diff_no_index(struct rev_info *revs,
255255
};
256256
struct option *options;
257257

258-
options = parse_options_concat(no_index_options,
259-
revs->diffopt.parseopts);
258+
options = add_diff_options(no_index_options, &revs->diffopt);
260259
argc = parse_options(argc, argv, revs->prefix, options,
261260
diff_no_index_usage, 0);
262261
if (argc != 2) {

diff.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4615,8 +4615,6 @@ static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
46154615
builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
46164616
}
46174617

4618-
static void prep_parse_options(struct diff_options *options);
4619-
46204618
void repo_diff_setup(struct repository *r, struct diff_options *options)
46214619
{
46224620
memcpy(options, &default_diff_options, sizeof(*options));
@@ -4662,8 +4660,6 @@ void repo_diff_setup(struct repository *r, struct diff_options *options)
46624660

46634661
options->color_moved = diff_color_moved_default;
46644662
options->color_moved_ws_handling = diff_color_moved_ws_default;
4665-
4666-
prep_parse_options(options);
46674663
}
46684664

46694665
static const char diff_status_letters[] = {
@@ -4821,8 +4817,6 @@ void diff_setup_done(struct diff_options *options)
48214817
options->filter = ~filter_bit[DIFF_STATUS_FILTER_AON];
48224818
options->filter &= ~options->filter_not;
48234819
}
4824-
4825-
FREE_AND_NULL(options->parseopts);
48264820
}
48274821

48284822
int parse_long_opt(const char *opt, const char **argv,
@@ -5419,7 +5413,8 @@ static int diff_opt_rotate_to(const struct option *opt, const char *arg, int uns
54195413
return 0;
54205414
}
54215415

5422-
static void prep_parse_options(struct diff_options *options)
5416+
struct option *add_diff_options(const struct option *opts,
5417+
struct diff_options *options)
54235418
{
54245419
struct option parseopts[] = {
54255420
OPT_GROUP(N_("Diff output format options")),
@@ -5689,22 +5684,25 @@ static void prep_parse_options(struct diff_options *options)
56895684
OPT_END()
56905685
};
56915686

5692-
ALLOC_ARRAY(options->parseopts, ARRAY_SIZE(parseopts));
5693-
memcpy(options->parseopts, parseopts, sizeof(parseopts));
5687+
return parse_options_concat(opts, parseopts);
56945688
}
56955689

56965690
int diff_opt_parse(struct diff_options *options,
56975691
const char **av, int ac, const char *prefix)
56985692
{
5693+
struct option no_options[] = { OPT_END() };
5694+
struct option *parseopts = add_diff_options(no_options, options);
5695+
56995696
if (!prefix)
57005697
prefix = "";
57015698

5702-
ac = parse_options(ac, av, prefix, options->parseopts, NULL,
5699+
ac = parse_options(ac, av, prefix, parseopts, NULL,
57035700
PARSE_OPT_KEEP_DASHDASH |
57045701
PARSE_OPT_KEEP_UNKNOWN_OPT |
57055702
PARSE_OPT_NO_INTERNAL_HELP |
57065703
PARSE_OPT_ONE_SHOT |
57075704
PARSE_OPT_STOP_AT_NON_OPTION);
5705+
free(parseopts);
57085706

57095707
return ac;
57105708
}
@@ -6513,7 +6511,6 @@ void diff_free(struct diff_options *options)
65136511
diff_free_file(options);
65146512
diff_free_ignore_regex(options);
65156513
clear_pathspec(&options->pathspec);
6516-
FREE_AND_NULL(options->parseopts);
65176514
}
65186515

65196516
void diff_flush(struct diff_options *options)

diff.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ struct diff_options {
394394
unsigned color_moved_ws_handling;
395395

396396
struct repository *repo;
397-
struct option *parseopts;
398397
struct strmap *additional_path_headers;
399398

400399
int no_free;
@@ -539,6 +538,7 @@ int git_diff_ui_config(const char *var, const char *value, void *cb);
539538
#define diff_setup(diffopts) repo_diff_setup(the_repository, diffopts)
540539
#endif
541540
void repo_diff_setup(struct repository *, struct diff_options *);
541+
struct option *add_diff_options(const struct option *, struct diff_options *);
542542
int diff_opt_parse(struct diff_options *, const char **, int, const char *);
543543
void diff_setup_done(struct diff_options *);
544544
int git_config_rename(const char *var, const char *value);

0 commit comments

Comments
 (0)