Skip to content

Commit e3a7b15

Browse files
author
Linus Arver
committed
trailer: use offsets for trailer_start/trailer_end
Previously these fields in the trailer_info struct were of type "const char *" and pointed to positions in the input string directly (to the start and end positions of the trailer block). Use offsets to make the intended usage less ambiguous. We only need to reference the input string in format_trailer_info(), so update that function to take a pointer to the input. While we're at it, rename trailer_start to trailer_block_start to be more explicit about these offsets (that they are for the entire trailer block including other trailers). Ditto for trailer_end. Reported-by: Glen Choo <glencbz@gmail.com> Signed-off-by: Linus Arver <linusa@google.com>
1 parent ce25420 commit e3a7b15

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

sequencer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
345345
if (ignore_footer)
346346
sb->buf[sb->len - ignore_footer] = saved_char;
347347

348-
if (info.trailer_start == info.trailer_end)
348+
if (info.trailer_block_start == info.trailer_block_end)
349349
return 0;
350350

351351
for (i = 0; i < info.trailer_nr; i++)

trailer.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ static size_t find_end_of_log_message(const char *input, int no_divider)
859859
* Return the position of the first trailer line or len if there are no
860860
* trailers.
861861
*/
862-
static size_t find_trailer_start(const char *buf, size_t len)
862+
static size_t find_trailer_block_start(const char *buf, size_t len)
863863
{
864864
const char *s;
865865
ssize_t end_of_title, l;
@@ -1075,7 +1075,6 @@ void process_trailers(const char *file,
10751075
LIST_HEAD(head);
10761076
struct strbuf sb = STRBUF_INIT;
10771077
struct trailer_info info;
1078-
size_t trailer_end;
10791078
FILE *outfile = stdout;
10801079

10811080
ensure_configured();
@@ -1086,11 +1085,10 @@ void process_trailers(const char *file,
10861085
outfile = create_in_place_tempfile(file);
10871086

10881087
parse_trailers(&info, sb.buf, &head, opts);
1089-
trailer_end = info.trailer_end - sb.buf;
10901088

10911089
/* Print the lines before the trailers */
10921090
if (!opts->only_trailers)
1093-
fwrite(sb.buf, 1, info.trailer_start - sb.buf, outfile);
1091+
fwrite(sb.buf, 1, info.trailer_block_start, outfile);
10941092

10951093
if (!opts->only_trailers && !info.blank_line_before_trailer)
10961094
fprintf(outfile, "\n");
@@ -1112,7 +1110,7 @@ void process_trailers(const char *file,
11121110

11131111
/* Print the lines after the trailers as is */
11141112
if (!opts->only_trailers)
1115-
fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile);
1113+
fwrite(sb.buf + info.trailer_block_end, 1, sb.len - info.trailer_block_end, outfile);
11161114

11171115
if (opts->in_place)
11181116
if (rename_tempfile(&trailers_tempfile, file))
@@ -1124,7 +1122,7 @@ void process_trailers(const char *file,
11241122
void trailer_info_get(struct trailer_info *info, const char *str,
11251123
const struct process_trailer_options *opts)
11261124
{
1127-
int end_of_log_message, trailer_start;
1125+
size_t end_of_log_message = 0, trailer_block_start = 0;
11281126
struct strbuf **trailer_lines, **ptr;
11291127
char **trailer_strings = NULL;
11301128
size_t nr = 0, alloc = 0;
@@ -1133,10 +1131,10 @@ void trailer_info_get(struct trailer_info *info, const char *str,
11331131
ensure_configured();
11341132

11351133
end_of_log_message = find_end_of_log_message(str, opts->no_divider);
1136-
trailer_start = find_trailer_start(str, end_of_log_message);
1134+
trailer_block_start = find_trailer_block_start(str, end_of_log_message);
11371135

1138-
trailer_lines = strbuf_split_buf(str + trailer_start,
1139-
end_of_log_message - trailer_start,
1136+
trailer_lines = strbuf_split_buf(str + trailer_block_start,
1137+
end_of_log_message - trailer_block_start,
11401138
'\n',
11411139
0);
11421140
for (ptr = trailer_lines; *ptr; ptr++) {
@@ -1157,9 +1155,9 @@ void trailer_info_get(struct trailer_info *info, const char *str,
11571155
strbuf_list_free(trailer_lines);
11581156

11591157
info->blank_line_before_trailer = ends_with_blank_line(str,
1160-
trailer_start);
1161-
info->trailer_start = str + trailer_start;
1162-
info->trailer_end = str + end_of_log_message;
1158+
trailer_block_start);
1159+
info->trailer_block_start = trailer_block_start;
1160+
info->trailer_block_end = end_of_log_message;
11631161
info->trailers = trailer_strings;
11641162
info->trailer_nr = nr;
11651163
}
@@ -1174,6 +1172,7 @@ void trailer_info_release(struct trailer_info *info)
11741172

11751173
static void format_trailer_info(struct strbuf *out,
11761174
const struct trailer_info *info,
1175+
const char *msg,
11771176
const struct process_trailer_options *opts)
11781177
{
11791178
size_t origlen = out->len;
@@ -1183,8 +1182,8 @@ static void format_trailer_info(struct strbuf *out,
11831182
if (!opts->only_trailers && !opts->unfold && !opts->filter &&
11841183
!opts->separator && !opts->key_only && !opts->value_only &&
11851184
!opts->key_value_separator) {
1186-
strbuf_add(out, info->trailer_start,
1187-
info->trailer_end - info->trailer_start);
1185+
strbuf_add(out, msg + info->trailer_block_start,
1186+
info->trailer_block_end - info->trailer_block_start);
11881187
return;
11891188
}
11901189

@@ -1238,7 +1237,7 @@ void format_trailers_from_commit(struct strbuf *out, const char *msg,
12381237
struct trailer_info info;
12391238

12401239
trailer_info_get(&info, msg, opts);
1241-
format_trailer_info(out, &info, opts);
1240+
format_trailer_info(out, &info, msg, opts);
12421241
trailer_info_release(&info);
12431242
}
12441243

trailer.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ int trailer_set_if_missing(enum trailer_if_missing *item, const char *value);
3232
struct trailer_info {
3333
/*
3434
* True if there is a blank line before the location pointed to by
35-
* trailer_start.
35+
* trailer_block_start.
3636
*/
3737
int blank_line_before_trailer;
3838

3939
/*
40-
* Pointers to the start and end of the trailer block found. If there
41-
* is no trailer block found, these 2 pointers point to the end of the
42-
* input string.
40+
* Offsets to the trailer block start and end positions in the input
41+
* string. If no trailer block is found, these are both set to the
42+
* "true" end of the input (find_end_of_log_message()).
4343
*/
44-
const char *trailer_start, *trailer_end;
44+
size_t trailer_block_start, trailer_block_end;
4545

4646
/*
4747
* Array of trailers found.

0 commit comments

Comments
 (0)