Skip to content

Commit 76c7e70

Browse files
pks-tgitster
authored andcommitted
diff: fix leaking orderfile option
The `orderfile` diff option is being assigned via `OPT_FILENAME()`, which assigns an allocated string to the variable. We never free it though, causing a memory leak. Change the type of the string to `char *` and free it to plug the leak. This also requires us to use `xstrdup()` to assign the global config to it in case it is set. This leak is being hit in t7621, but plugging it alone does not make the test suite pass. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 49af1b7 commit 76c7e70

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

combine-diff.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,9 +1393,8 @@ static struct combine_diff_path *find_paths_generic(const struct object_id *oid,
13931393
{
13941394
struct combine_diff_path *paths = NULL;
13951395
int i, num_parent = parents->nr;
1396-
13971396
int output_format = opt->output_format;
1398-
const char *orderfile = opt->orderfile;
1397+
char *orderfile = opt->orderfile;
13991398

14001399
opt->output_format = DIFF_FORMAT_NO_OUTPUT;
14011400
/* tell diff_tree to emit paths in sorted (=tree) order */

diff.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,10 @@ int git_diff_ui_config(const char *var, const char *value,
441441
}
442442
if (!strcmp(var, "diff.wordregex"))
443443
return git_config_string(&diff_word_regex_cfg, var, value);
444-
if (!strcmp(var, "diff.orderfile"))
444+
if (!strcmp(var, "diff.orderfile")) {
445+
FREE_AND_NULL(diff_order_file_cfg);
445446
return git_config_pathname(&diff_order_file_cfg, var, value);
447+
}
446448

447449
if (!strcmp(var, "diff.ignoresubmodules")) {
448450
if (!value)
@@ -4775,7 +4777,7 @@ void repo_diff_setup(struct repository *r, struct diff_options *options)
47754777
if (diff_indent_heuristic)
47764778
DIFF_XDL_SET(options, INDENT_HEURISTIC);
47774779

4778-
options->orderfile = diff_order_file_cfg;
4780+
options->orderfile = xstrdup_or_null(diff_order_file_cfg);
47794781

47804782
if (!options->flags.ignore_submodule_set)
47814783
options->flags.ignore_untracked_in_submodules = 1;
@@ -6727,6 +6729,7 @@ void diff_free(struct diff_options *options)
67276729
FREE_AND_NULL(options->objfind);
67286730
}
67296731

6732+
FREE_AND_NULL(options->orderfile);
67306733
for (size_t i = 0; i < options->anchors_nr; i++)
67316734
free(options->anchors[i]);
67326735
FREE_AND_NULL(options->anchors);

diff.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ enum diff_submodule_format {
235235
* diffcore library with.
236236
*/
237237
struct diff_options {
238-
const char *orderfile;
238+
char *orderfile;
239239

240240
/*
241241
* "--rotate-to=<file>" would start showing at <file> and when

0 commit comments

Comments
 (0)