Skip to content

Commit f9c1989

Browse files
pks-tgitster
authored andcommitted
diff: refactor code to clarify memory ownership of prefixes
The source and destination prefixes are tracked in a `const char *` array, but may at times contain allocated strings. The result is that those strings may be leaking because we never free them. Refactor the code to always store allocated strings in those variables, freeing them as required. This requires us to handle the default values a bit different compared to before. But given that there is only a single callsite where we use the variables to `struct diff_options` it's easy to handle the defaults there. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6073b3b commit f9c1989

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

diff.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ static char *diff_order_file_cfg;
6262
int diff_auto_refresh_index = 1;
6363
static int diff_mnemonic_prefix;
6464
static int diff_no_prefix;
65-
static const char *diff_src_prefix = "a/";
66-
static const char *diff_dst_prefix = "b/";
65+
static char *diff_src_prefix;
66+
static char *diff_dst_prefix;
6767
static int diff_relative;
6868
static int diff_stat_name_width;
6969
static int diff_stat_graph_width;
@@ -411,10 +411,12 @@ int git_diff_ui_config(const char *var, const char *value,
411411
return 0;
412412
}
413413
if (!strcmp(var, "diff.srcprefix")) {
414-
return git_config_string(&diff_src_prefix, var, value);
414+
FREE_AND_NULL(diff_src_prefix);
415+
return git_config_string((const char **) &diff_src_prefix, var, value);
415416
}
416417
if (!strcmp(var, "diff.dstprefix")) {
417-
return git_config_string(&diff_dst_prefix, var, value);
418+
FREE_AND_NULL(diff_dst_prefix);
419+
return git_config_string((const char **) &diff_dst_prefix, var, value);
418420
}
419421
if (!strcmp(var, "diff.relative")) {
420422
diff_relative = git_config_bool(var, value);
@@ -3433,8 +3435,8 @@ void diff_set_noprefix(struct diff_options *options)
34333435

34343436
void diff_set_default_prefix(struct diff_options *options)
34353437
{
3436-
options->a_prefix = diff_src_prefix;
3437-
options->b_prefix = diff_dst_prefix;
3438+
options->a_prefix = diff_src_prefix ? diff_src_prefix : "a/";
3439+
options->b_prefix = diff_dst_prefix ? diff_dst_prefix : "b/";
34383440
}
34393441

34403442
struct userdiff_driver *get_textconv(struct repository *r,
@@ -5371,8 +5373,8 @@ static int diff_opt_default_prefix(const struct option *opt,
53715373

53725374
BUG_ON_OPT_NEG(unset);
53735375
BUG_ON_OPT_ARG(optarg);
5374-
diff_src_prefix = "a/";
5375-
diff_dst_prefix = "b/";
5376+
FREE_AND_NULL(diff_src_prefix);
5377+
FREE_AND_NULL(diff_dst_prefix);
53765378
diff_set_default_prefix(options);
53775379
return 0;
53785380
}

0 commit comments

Comments
 (0)