Skip to content

Commit db93c66

Browse files
peffdscho
authored andcommitted
diff: modify output_prefix function pointer
The uses of the output_prefix function pointer in the diff_options struct is currently difficult to work with by returning a pointer to a strbuf. There is only one use that cares about the length of the string, which appears to be the only justification of the return type. We already noticed confusing memory issues around this return type, so use a const char * return type to make it clear that the caller does not own this string buffer. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Derrick Stolee <[email protected]>
1 parent eff0ca0 commit db93c66

File tree

6 files changed

+12
-14
lines changed

6 files changed

+12
-14
lines changed

diff-lib.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ int index_differs_from(struct repository *r,
701701
return (has_changes != 0);
702702
}
703703

704-
static struct strbuf *idiff_prefix_cb(struct diff_options *opt UNUSED, void *data)
704+
static const char *idiff_prefix_cb(struct diff_options *opt UNUSED, void *data)
705705
{
706706
return data;
707707
}
@@ -716,7 +716,7 @@ void show_interdiff(const struct object_id *oid1, const struct object_id *oid2,
716716
opts.output_format = DIFF_FORMAT_PATCH;
717717
opts.output_prefix = idiff_prefix_cb;
718718
strbuf_addchars(&prefix, ' ', indent);
719-
opts.output_prefix_data = &prefix;
719+
opts.output_prefix_data = prefix.buf;
720720
diff_setup_done(&opts);
721721

722722
diff_tree_oid(oid1, oid2, "", &opts);

diff.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,12 +2317,10 @@ const char *diff_get_color(int diff_use_color, enum color_diff ix)
23172317

23182318
const char *diff_line_prefix(struct diff_options *opt)
23192319
{
2320-
struct strbuf *msgbuf;
2321-
if (!opt->output_prefix)
2322-
return "";
2320+
if (opt->output_prefix)
2321+
return opt->output_prefix(opt, opt->output_prefix_data);
23232322

2324-
msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
2325-
return msgbuf->buf;
2323+
return "";
23262324
}
23272325

23282326
static unsigned long sane_truncate_line(char *line, unsigned long len)

diff.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ typedef void (*add_remove_fn_t)(struct diff_options *options,
9494
typedef void (*diff_format_fn_t)(struct diff_queue_struct *q,
9595
struct diff_options *options, void *data);
9696

97-
typedef struct strbuf *(*diff_prefix_fn_t)(struct diff_options *opt, void *data);
97+
typedef const char *(*diff_prefix_fn_t)(struct diff_options *opt, void *data);
9898

9999
#define DIFF_FORMAT_RAW 0x0001
100100
#define DIFF_FORMAT_DIFFSTAT 0x0002

graph.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ struct git_graph {
314314
unsigned short default_column_color;
315315
};
316316

317-
static struct strbuf *diff_output_prefix_callback(struct diff_options *opt, void *data)
317+
static const char *diff_output_prefix_callback(struct diff_options *opt, void *data)
318318
{
319319
struct git_graph *graph = data;
320320
static struct strbuf msgbuf = STRBUF_INIT;
@@ -327,7 +327,7 @@ static struct strbuf *diff_output_prefix_callback(struct diff_options *opt, void
327327
opt->line_prefix_length);
328328
if (graph)
329329
graph_padding_line(graph, &msgbuf);
330-
return &msgbuf;
330+
return msgbuf.buf;
331331
}
332332

333333
static const struct diff_options *default_diffopt;

log-tree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -923,10 +923,10 @@ int log_tree_diff_flush(struct rev_info *opt)
923923
*/
924924
int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
925925
if (opt->diffopt.output_prefix) {
926-
struct strbuf *msg = NULL;
926+
const char *msg;
927927
msg = opt->diffopt.output_prefix(&opt->diffopt,
928928
opt->diffopt.output_prefix_data);
929-
fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
929+
fwrite(msg, strlen(msg), 1, opt->diffopt.file);
930930
}
931931

932932
/*

range-diff.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ static void patch_diff(const char *a, const char *b,
480480
diff_flush(diffopt);
481481
}
482482

483-
static struct strbuf *output_prefix_cb(struct diff_options *opt UNUSED, void *data)
483+
static const char *output_prefix_cb(struct diff_options *opt UNUSED, void *data)
484484
{
485485
return data;
486486
}
@@ -508,7 +508,7 @@ static void output(struct string_list *a, struct string_list *b,
508508
opts.flags.suppress_hunk_header_line_count = 1;
509509
opts.output_prefix = output_prefix_cb;
510510
strbuf_addstr(&indent, " ");
511-
opts.output_prefix_data = &indent;
511+
opts.output_prefix_data = indent.buf;
512512
diff_setup_done(&opts);
513513

514514
/*

0 commit comments

Comments
 (0)