Skip to content

Commit 394affd

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 42d2ad5 commit 394affd

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

line-log.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -899,14 +899,12 @@ static void print_line(const char *prefix, char first,
899899

900900
static char *output_prefix(struct diff_options *opt)
901901
{
902-
char *prefix = "";
903-
904902
if (opt->output_prefix) {
905903
struct strbuf *sb = opt->output_prefix(opt, opt->output_prefix_data);
906-
prefix = sb->buf;
904+
return sb->buf;
905+
} else {
906+
return xstrdup("");
907907
}
908-
909-
return prefix;
910908
}
911909

912910
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
927925
const char *c_context = diff_get_color(opt->use_color, DIFF_CONTEXT);
928926

929927
if (!pair || !diff)
930-
return;
928+
goto out;
931929

932930
if (pair->one->oid_valid)
933931
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
10021000
c_context, c_reset, opt->file);
10031001
}
10041002

1003+
out:
10051004
free(p_ends);
10061005
free(t_ends);
1006+
free(prefix);
10071007
}
10081008

10091009
/*
@@ -1012,7 +1012,11 @@ static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *rang
10121012
*/
10131013
static void dump_diff_hacky(struct rev_info *rev, struct line_log_data *range)
10141014
{
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+
10161020
while (range) {
10171021
dump_diff_hacky_one(rev, range);
10181022
range = range->next;

0 commit comments

Comments
 (0)