line-log: always allocate the output prefix
The returned string by `output_prefix()` is sometimes a string constant and sometimes an allocated string. This has been fine until now because we always leak the allocated strings, and thus we never tried to free the string constant. Fix the code to always return an allocated string and free the returned value at all callsites. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Jun 7, 2024 at 08:38 UTC
394affd46dc9b0c805df6e999837aa297f844fc0
1 file changed
+11
-7
line-log.c
+11
-7
@@ -899,14 +899,12 @@ static void print_line(const char *prefix, char first,
899
900
static char *output_prefix(struct diff_options *opt)
901
{
902
- char *prefix = "";
903
-
902
if (opt->output_prefix) {
903
struct strbuf *sb = opt->output_prefix(opt, opt->output_prefix_data);
906
- prefix = sb->buf;
904
+ return sb->buf;
905
+ } else {
906
+ return xstrdup("");
907
}
908
-
909
- return prefix;
908
}
909
910
static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *range)
@@ -927,7 +925,7 @@ static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *rang
925
const char *c_context = diff_get_color(opt->use_color, DIFF_CONTEXT);
926
927
if (!pair || !diff)
930
- return;
928
+ goto out;
929
930
if (pair->one->oid_valid)
931
fill_line_ends(rev->diffopt.repo, pair->one, &p_lines, &p_ends);
@@ -1002,8 +1000,10 @@ static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *rang
1000
c_context, c_reset, opt->file);
1001
}
1002
1003
+out:
1004
free(p_ends);
1005
free(t_ends);
1006
+ free(prefix);
1007
}
1008
1009
/*
@@ -1012,7 +1012,11 @@ static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *rang
1012
*/
1013
static void dump_diff_hacky(struct rev_info *rev, struct line_log_data *range)
1014
{
1015
- fprintf(rev->diffopt.file, "%s\n", output_prefix(&rev->diffopt));
1015
+ char *prefix = output_prefix(&rev->diffopt);
1016
+
1017
+ fprintf(rev->diffopt.file, "%s\n", prefix);
1018
+ free(prefix);
1019
+
1020
while (range) {
1021
dump_diff_hacky_one(rev, range);
1022
range = range->next;